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

このクイックスタートでは、限定公開の Artifact Registry Apt リポジトリを設定し、リポジトリに Debian パッケージを追加して、Debian ベースのオペレーティング システムを実行する Compute Engine VM にパッケージをインストールする方法を示します。

Debian パッケージの管理の詳細については、Debian パッケージを操作するをご覧ください。

始める前に

  1. アカウントにログインします。 Google Cloud を初めて使用する場合は、 アカウントを作成して、 実際のシナリオでプロダクトがどのように機能するかを評価してください。 Google Cloud新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

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

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

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

必要なロール

Artifact Registry Apt パッケージ リポジトリの作成と管理に必要な権限を取得するには、プロジェクトに対する次の IAM ロールの付与を管理者に依頼してください。

ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

必要な権限は、カスタム ロールや他の事前定義 ロールから取得することもできます。

Cloud Shell を起動する

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

Cloud Shell を起動します。

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

    Google Cloud コンソール

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

コンソールの下部にあるフレーム内で Cloud Shell セッションが開きます。 このシェルを使用して、gcloud コマンドを実行して VM とリポジトリを作成します。

リポジトリを作成する

アーティファクトのリポジトリを作成します。

  1. リポジトリを作成します。

    コンソール

    1. [リポジトリ] ページを Google Cloud コンソールで開きます。

      [リポジトリ] ページを開く

    2. [リポジトリを作成] をクリックします。

    3. リポジトリ名として quickstart-apt-repo を指定します。

    4. フォーマットとして [Apt] を選択します。

    5. [ロケーション タイプ] で、[リージョン] を選択し、ロケーション us-west1 を選択します。

    6. [作成] をクリックします。

    このリポジトリがリポジトリ リストに追加されます。

    gcloud

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

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

      gcloud artifacts repositories list
      

これで、リポジトリにパッケージを追加できます。Cloud Shell は、 Debian ではなく Google が構築した Ubuntu イメージを使用します。リポジトリ内の Debian パッケージを管理するには、Debian OS イメージを使用する VM を使用します。

VM を作成する

サンプル パッケージをインストールする新しい Compute Engine VM を作成します。

Cloud Shell で次のコマンドを実行して、quickstart-apt-vm という名前の VM インスタンスを作成します。

gcloud compute instances create quickstart-apt-vm \
    --image-family=debian-12 \
    --image-project=debian-cloud \
    --scopes=cloud-platform

デフォルトでは、VM にはリポジトリを操作するために必要な アクセス スコープ が ありません。--scopes フラグを使用して、VM のアクセス スコープを cloud-platform に設定します。

  1. [VM インスタンス] ページに移動します。

    [VM インスタンス] ページを開く

  2. VM がある行で [SSH] をクリックします。VM のターミナル セッションで新しいウィンドウが開きます。

リポジトリにパッケージを追加する

Google Cloud CLI を使用してパッケージをリポジトリにアップロードすることも、Cloud Storage に保存されているパッケージをインポートすることもできます。Cloud Build を使用してパッケージをビルドする場合、このビルドは Cloud Storage にパッケージを保存してインポートできます。

このクイックスタートでは、gcloud artifacts apt upload コマンドを使用してサンプル ファイルをアップロードします。

  1. gcloud init を実行して、VM で Google Cloud CLI を初期化します。

  2. Apt を更新するには:

    sudo apt update
    
  3. 次のコマンドを使用して curl パッケージをダウンロードします。

    apt download curl
    

    Apt は、構成した Apt リポジトリから入手可能な最新バージョンのパッケージをダウンロードします。

    Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B]
    Get:2 https://deb.debian.org/debian bookworm/main amd64 curl amd64 7.88.1-10+deb12u14 [316 kB]
    Fetched 316 kB in 0s (3078 kB/s)
    

    ls を実行して、curl パッケージのファイル名を取得します。ファイル名は curl_7.88.1-10+deb12u14_amd64.deb のようになります。

  4. gcloud コマンドを簡素化するには、デフォルトのリポジトリを quickstart-apt-repo に、デフォルトのロケーションを us-west1 に設定します。デフォルト値を設定すると、gcloud コマンドでリポジトリまたはロケーションが必要な場合でも指定の必要はありません。

    リポジトリを設定するには、次のコマンドを実行します。

    gcloud config set artifacts/repository quickstart-apt-repo
    

    ロケーションを設定するには、次のコマンドを実行します。

    gcloud config set artifacts/location us-west1
    

    これらのコマンドの詳細については、gcloud config set のドキュメントをご覧ください。

  5. gcloud artifacts apt upload を実行して、パッケージをリポジトリにアップロードします。

    gcloud artifacts apt upload quickstart-apt-repo \
        --source=FILE_NAME
    

    FILE_NAME は、curl パッケージのパスに置き換えます。

リポジトリ内のパッケージを表示する

パッケージがリポジトリに追加されたことを確認します。

コンソール

  1. [リポジトリ] ページを Google Cloud コンソールで開きます。

    [リポジトリ] ページを開く

  2. リポジトリ リストで、quickstart-apt-repo リポジトリをクリックします。

    [パッケージ] ページに、リポジトリ内のパッケージが一覧表示されます。

gcloud

quickstart-apt-repo リポジトリ内のパッケージを一覧表示するには、次のコマンドを実行します。

 gcloud artifacts packages list

quickstart-apt-repo のパッケージのバージョンを表示するには、次のコマンドを実行します。

gcloud artifacts versions list --package=apt-dpkg-ref

パッケージ管理システムを構成する

VM にパッケージをインストールするには、作成したリポジトリを、パッケージ リポジトリを定義する Apt 構成ファイルに追加します。

  1. VM に Apt 認証ヘルパーをインストールして、Apt が認証を実行できるようにします。

    sudo apt install apt-transport-artifact-registry
    
  2. Artifact Registry パッケージにアクセスするように VM を構成します。

    echo 'deb ar+https://us-west1-apt.pkg.dev/projects/PROJECT quickstart-apt-repo main' | sudo tee -a  /etc/apt/sources.list.d/artifact-registry.list
    

    PROJECT は、実際の Google Cloudプロジェクト ID に置き換えます。

パッケージをインストールする

リポジトリに追加したパッケージをインストールします。

  1. 使用可能なパッケージのリストを更新するには:

    sudo apt update
    
  2. リポジトリからパッケージをインストールします。

    sudo apt install curl/quickstart-apt-repo
    

    返されるインストール情報は、次の例のようになります。

    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    Selected version '7.88.1-10+deb12u14' (quickstart-apt-repo:quickstart-apt-repo, Debian:12.12/oldstable [amd64]) for 'curl'
    The following additional packages will be installed:
      libcurl3-gnutls libcurl4
    The following packages will be upgraded:
      curl libcurl3-gnutls libcurl4
    3 upgraded, 0 newly installed, 0 to remove and 77 not upgraded.
    Need to get 1093 kB of archives.
    After this operation, 0 B of additional disk space will be used.
    Do you want to continue? [Y/n] y
    Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B]
    Get:2 https://deb.debian.org/debian bookworm/main amd64 libcurl4 amd64 7.88.1-10+deb12u14 [392 kB]
    Get:3 https://deb.debian.org/debian bookworm/main amd64 libcurl3-gnutls amd64 7.88.1-10+deb12u14 [386 kB]
    Get:4 ar+https://us-west1-apt.pkg.dev/projects/PROJECT quickstart-apt-repo/main amd64 curl amd64 7.88.1-10+deb12u14 [316 kB]
    Fetched 1093 kB in 1s (947 kB/s)
    Reading changelogs... Done
    (Reading database ... 72080 files and directories currently installed.)
    Preparing to unpack .../curl_7.88.1-10+deb12u14_amd64.deb ...
    Unpacking curl (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ...
    Preparing to unpack .../libcurl4_7.88.1-10+deb12u14_amd64.deb ...
    Unpacking libcurl4:amd64 (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ...
    Preparing to unpack .../libcurl3-gnutls_7.88.1-10+deb12u14_amd64.deb ...
    Unpacking libcurl3-gnutls:amd64 (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ...
    Setting up libcurl3-gnutls:amd64 (7.88.1-10+deb12u14) ...
    Setting up libcurl4:amd64 (7.88.1-10+deb12u14) ...
    Setting up curl (7.88.1-10+deb12u14) ...
    Processing triggers for man-db (2.11.2-2) ...
    Processing triggers for libc-bin (2.36-9+deb12u10) ...
    

クリーンアップ

このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、 次の手順を実施します。

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

リポジトリを削除するには、次の手順を行います。

コンソール

  1. [リポジトリ] ページを Google Cloud コンソールで開きます。

    [リポジトリ] ページを開く

  2. リポジトリ リストで、quickstart-apt-repo リポジトリを選択します。

  3. [削除] をクリックします。

gcloud

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

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

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

作成した VM を削除するには、次のコマンドを実行します。

gcloud compute instances delete quickstart-apt-vm

次のステップ