Artifact Registry に他の形式を保存する

Artifact Registry の汎用形式のリポジトリを設定し、YAML ファイルをアップロードする方法を学習します。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  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 (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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  7. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. Cloud Shell を起動する

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    このクイックスタートでは、Cloud Shell を使用して gcloud コマンドを実行します。

    汎用リポジトリを作成する

    1. ロケーション us-central1Generic repository という説明で quickstart-generic-repo という名前の汎用形式のリポジトリを作成するには、次のコマンドを実行します。

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

      gcloud artifacts repositories list
      
    3. gcloud コマンドを簡単にするために、以下のコマンドを実行して、デフォルトのリポジトリを quickstart-generic-repo に、デフォルトのロケーションを us-central1 に設定します。

      1. デフォルトのリポジトリを quickstart-generic-repo に設定するには、次のコマンドを実行します。

        gcloud config set artifacts/repository quickstart-generic-repo
        
      2. デフォルトのロケーションを us-central1 に設定するには、次のコマンドを実行します。

        gcloud config set artifacts/location us-central1
        

      デフォルト値を設定すると、gcloud コマンドでリポジトリまたはロケーションが必要な場合でも指定の必要はありません。

    アーティファクトをリポジトリにアップロードする

    1. ホーム ディレクトリに、リポジトリにアップロードするファイルを作成します。

      echo "hello world" > hello.yaml
      
    2. ファイルをアーティファクトとしてリポジトリにアップロードするには、次のコマンドを実行します。

      gcloud artifacts generic upload \
          --source=hello.yaml \
          --package=my-package \
          --version=1.0.0
      

      説明:

      • hello.yaml は、アップロードするファイルのパスです。
      • my-package は、アップロードするパッケージです。
      • 1.0.0 は、アーティファクトのバージョンです。リポジトリ内の既存のバージョンは上書きできません。

    リポジトリ内のアーティファクトを表示する

    アーティファクトがリポジトリに追加されたことを確認するには、次のコマンドを実行してすべてのアーティファクトを一覧表示します。

    gcloud artifacts files list
    

    レスポンスには、ファイルの詳細情報が PACKAGE:VERSION:FILE_NAME という形式で含まれます。

    次の例では、hello.yamlFILE_NAME です。

    FILE: my-package:1.0.0:hello.yaml
    CREATE_TIME: 2023-03-09T20:55:07
    UPDATE_TIME: 2023-03-09T20:55:07
    SIZE (MB): 0.000
    OWNER: projects/my-project/locations/us-central1/repositories/quickstart-generic-repo/packages/my-package/versions/1.0.0
    

    汎用アーティファクトをダウンロードする

    リポジトリから汎用アーティファクトをダウンロードするには、次のコマンドを実行します。

    gcloud artifacts generic download \
        --name=hello.yaml \
        --package=my-package \
        --version=1.0.0 \
        --destination=DESTINATION
    

    説明:

    • hello.yaml は、ダウンロードするファイルの名前です。
    • my-package はダウンロードするパッケージです。
    • 1.0.0 はアーティファクトのバージョンです。

    DESTINATION は、ダウンロードしたものを保存するローカル ファイル システムのディレクトリに置き換えます。ダウンロード先フォルダはすでに存在している必要があります。存在しない場合、コマンドは失敗します。

    クリーンアップ

    このチュートリアルで使用したリソースについて Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持してリポジトリを削除します。

    プロジェクトの削除

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    リポジトリを削除する

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

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

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

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

    次のステップ