在 Artifact Registry 儲存 Conda 套件
本快速入門導覽課程說明如何設定私人 Artifact Registry Conda 存放區,然後將套件上傳至該存放區。
事前準備
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
- 如要在 Google Cloud 環境中使用 Conda CLI,請安裝下列其中一個 Conda 套件管理工具:
啟動 Cloud Shell
在本快速入門導覽課程中,您將使用 Cloud Shell,這是一種殼層環境,可用來管理託管於Google Cloud的資源。
Cloud Shell 已預先安裝 Google Cloud CLI 和 Conda。gcloud CLI 提供 Google Cloud的主要指令列介面。
啟動 Cloud Shell:
前往 Google Cloud 控制台。
在 Google Cloud 控制台工具列中,點選「啟用 Cloud Shell」。
此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您可以使用這個殼層來執行 gcloud 指令。
設定驗證機制
Conda 管道是指儲存一或多個 Conda 套件的位置位址。在 Conda 環境中安裝套件前,必須先將該套件的管道新增至 Conda 設定檔。
如要允許 Conda CLI 直接呼叫 Artifact Registry,您必須按照下列步驟,在 Google Cloud 中驗證 Conda 管道:
在 gcloud CLI 中執行下列指令,將
oauth權杖的身分儲存在 TOKEN 變數中:export TOKEN="oauth2accesstoken:$(gcloud auth print-access-token)"在 Conda 設定檔中,將 Conda 存放區新增為管道:
Conda
conda config --add channels \ https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORYMiniforge
conda config --add channels \ https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORYMamba
mamba config --add channels \ https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORYMicromamba
micromamba config --add channels \ https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY其中:
- TOKEN 會儲存執行
oauth2accesstoken:$(gcloud auth print-access-token)產生的oauth權杖值。 - LOCATION 是存放區的區域或多區域位置。
- PROJECT 是存放區所在專案的 ID。
- REPOSITORY 是存放區 ID。
- TOKEN 會儲存執行
更新過期的 oauth 權杖
oauth 權杖會在一小時後失效。如要使用新權杖重新驗證管道,請按照下列步驟操作:
從設定檔中移除頻道:
Conda
conda config --remove channels CHANNEL_NAMEMiniforge
conda config --remove channels CHANNEL_NAMEMamba
mamba config --remove channels CHANNEL_NAMEMicromamba
micromamba config --remove channels CHANNEL_NAME按照上一個步驟建立新的
oauth權杖。使用新的權杖值,將 Conda 存放區重新加入 Conda 設定檔。
建立存放區
建立套件存放區。
執行下列指令,在
us-west1位置中的quickstart-conda-repo當前專案內建立新的 Conda 存放區。gcloud artifacts repositories create quickstart-conda-repo \ --repository-format=conda \ --location=us-west1 \ --description="Conda repository"執行下列指令來確認存放區已建立完成:
gcloud artifacts repositories list如要簡化
gcloud指令,請將預設存放區設為quickstart-conda-repo,並將預設位置設為us-west1。設定值後,您就不需要在需要存放區或位置的gcloud指令中指定這些值。執行下列指令來設定存放區:
gcloud config set artifacts/repository quickstart-conda-repo如要設定位置,請執行下列指令:
gcloud config set artifacts/location us-west1如要進一步瞭解這些指令,請參閱 gcloud config set 說明文件。
將套件上傳至存放區
如要將套件上傳至存放區,請執行下列指令:
gcloud artifacts files upload \
--source=PACKAGE_NAME \
--repository=REPO_NAME\
--project=PROJECT \
--location=LOCATION
其中:
- REPO_NAME 是存放區 ID。
- PROJECT 是存放區所在專案的 ID。
- LOCATION 是存放區的區域或多區域位置。
如要一次上傳多個套件,請按照下列步驟操作:
gcloud artifacts files upload \
--source-directory="DIRECTORY" \
--repository="REPO_NAME"\
--project="PROJECT" \
--location="LOCATION"
其中:
- DIRECTORY 是本機資料夾的位址,其中包含 Conda 套件。
查看存放區中的套件
如要確認套件是否已新增,請列出 quickstart-conda-repo 存放區中的套件。
執行下列指令:
gcloud artifacts packages list --repository=quickstart-conda-repo
如要查看套件的版本,請執行下列指令:
gcloud artifacts versions list --package=PACKAGE_NAME
如要查看套件版本的檔案,請執行下列指令:
gcloud artifacts files list --package=PACKAGE_NAME --version=VERSION
安裝套件
如要安裝剛新增至 Artifact Registry 存放區的套件,請執行下列指令:
Conda
conda install -n ENVIRONMENT_NAME PACKAGE_NAME
Miniforge
conda install -n ENVIRONMENT_NAME PACKAGE_NAME
Mamba
mamba install -n ENVIRONMENT_NAME PACKAGE_NAME
Micromamba
micromamba install -n ENVIRONMENT_NAME PACKAGE_NAME
其中:
- ENVIRONMENT_NAME 是 Conda 環境的名稱。
- PACKAGE_NAME 是要安裝在存放區中的套件名稱。
疑難排解
詳情請參閱「Conda 套件疑難排解」。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
移除存放區之前,請先確認要保留的套件均已存放於其他位置。
如要刪除
quickstart-conda-repo存放區,請執行下列指令:gcloud artifacts repositories delete quickstart-conda-repo如要移除您為使用中的
gcloud設定建立的預設存放區和位置設定,請執行下列指令:gcloud config unset artifacts/repository gcloud config unset artifacts/location
後續步驟
- 進一步瞭解如何設定驗證方法。
- 瞭解如何管理存放區。
- 瞭解如何管理套件。
- 參閱開發運作相關資源,並探索我們的研究計畫。