Conda パッケージを Artifact Registry に保存する

このクイックスタートでは、非公開の Artifact Registry Conda リポジトリを設定して、パッケージをそのリポジトリにアップロードする方法について説明します。

始める前に

  1. Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  10. Google Cloud 環境内で Conda CLI を使用するには、次のいずれかの Conda パッケージ マネージャーをインストールします。

Cloud Shell を起動する

このクイックスタートでは、Google Cloudでホストされているリソースを管理するためのシェル環境である Cloud Shell を使用します。

Cloud Shell には、Google Cloud CLI と Conda がプリインストールされています。gcloud CLI は、 Google Cloudの主要なコマンドライン インターフェースを提供します。

Cloud Shell を起動します。

  1. Google Cloud コンソールに移動します。

    Google Cloud コンソール

  2. Google Cloud コンソールのツールバーで、[Cloud Shell をアクティブにする] をクリックします。

コンソールの下部にあるフレーム内で Cloud Shell セッションが開きます。このシェルで gcloud コマンドを実行します。

認証を構成する

Conda チャネルは、1 つ以上の Conda パッケージが保存されている場所のアドレスです。Conda 環境にパッケージをインストールする前に、そのパッケージのチャンネルを Conda 構成ファイルに追加する必要があります。

Conda CLI が Artifact Registry を直接呼び出せるようにするには、次の操作を行って Google Cloud で Conda チャネルを認証する必要があります。

  1. gcloud CLI で次のコマンドを実行して、oauth トークンの ID を TOKEN 変数に保存します。

    export TOKEN="oauth2accesstoken:$(gcloud auth print-access-token)"
    
  2. Conda 構成ファイルに Conda リポジトリをチャネルとして追加します。

    Conda

    conda config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Miniforge

    conda config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Mamba

    mamba config --add channels \
    https://$TOKEN@LOCATION-conda.pkg.dev/PROJECT/REPOSITORY
    

    Micromamba

    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 です。

期限切れの oauth トークンを更新する

oauth トークンは 1 時間後に期限切れになります。新しいトークンでチャネルを再認証するには、次の操作を行います。

  1. 構成ファイルからチャンネルを削除します。

    Conda

    conda config --remove channels CHANNEL_NAME
    

    Miniforge

    conda config --remove channels CHANNEL_NAME
    

    Mamba

    mamba config --remove channels CHANNEL_NAME
    

    Micromamba

    micromamba config --remove channels CHANNEL_NAME
    
  2. 新しい oauth トークンの作成について説明した前の手順に沿って操作します。

  3. 新しいトークン値を使用して、Conda 構成ファイルに Conda リポジトリを再度追加します。

リポジトリを作成する

パッケージのリポジトリを作成します。

  1. 次のコマンドを実行して、現在のプロジェクトでロケーション us-west1 に quickstart-conda-repo という新しい Conda リポジトリを作成します。

    gcloud artifacts repositories create quickstart-conda-repo \
        --repository-format=conda \
        --location=us-west1 \
        --description="Conda repository"
    
  2. 次のコマンドを実行して、リポジトリが作成されたことを確認します。

    gcloud artifacts repositories list
    
  3. 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 アカウントに課金されないようにするには、次の手順を実施します。

リポジトリを削除する前に、保持する必要があるパッケージが別の場所で使用可能であることを確認してください。

  1. quickstart-conda-repo リポジトリを削除するには、次のコマンドを実行します。

    gcloud artifacts repositories delete quickstart-conda-repo
    
  2. アクティブな gcloud 構成のデフォルトのリポジトリとロケーション設定を削除するには、次のコマンドを実行します。

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

次のステップ