在 Artifact Registry 中儲存 Apt 套件

本快速入門導覽課程說明如何設定私人的 Artifact Registry Apt 存放區、在存放區中新增 Debian 套件,以及在執行 Debian 作業系統的 Compute Engine VM 上安裝套件。

如要進一步瞭解如何管理 Debian 套件,請參閱「使用 Debian 套件」。

事前準備

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

    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 role (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

    Cloud Shell 已預先安裝 Google Cloud CLI。gcloud CLI 提供 Google Cloud的主要指令列介面。

    啟動 Cloud Shell:

    1. 前往 Google Cloud 控制台。

      Google Cloud console

    2. 在 Google Cloud 控制台中,按一下「啟用 Cloud Shell」圖示 「啟用 Cloud Shell」按鈕

    此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您將使用這個殼層執行 gcloud 指令,建立 VM 和存放區。

    建立存放區

    建立構件存放區。

    1. 建立存放區:

      控制台

      1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

        開啟「存放區」頁面

      2. 點選「Create Repository」

      3. 指定 quickstart-apt-repo 做為存放區名稱。

      4. 選取「Apt」做為格式。

      5. 在「位置類型」下方,選取「區域」,接著選擇位置 us-west1

      6. 點選「建立」

      存放區會新增至存放區清單。

      gcloud

      1. 在 Cloud Shell 執行下列指令,在目前專案中建立名為 quickstart-apt-repo 的新 Apt 存放區,位置為 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 使用 Google 建構的 Ubuntu 映像檔,而非 Debian。如要在存放區中管理 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 instances」(VM 執行個體) 頁面。

      開啟 VM 執行個體頁面

    2. 在 VM 所在資料列中,按一下「SSH」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 控制台中開啟「Repositories」(存放區) 頁面。

      開啟「存放區」頁面

    2. 在存放區清單中,點選「quickstart-apt-repo」存放區。

      「Packages」頁面會列出存放區中的套件。

    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. 設定 VM 以存取 Artifact Registry 套件:

      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 控制台中開啟「Repositories」(存放區) 頁面。

      開啟「Repositories」(存放區) 頁面

    2. 在存放區清單中,選取 quickstart-apt-repo 存放區。

    3. 按一下「Delete」(刪除)

    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
    

    後續步驟