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

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry API.

    Enable the API

啟動 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 執行下列指令,在目前專案中於 us-west1 位置建立名為 quickstart-apt-repo 的新 Apt 存放區。

      gcloud artifacts repositories create quickstart-apt-repo \
          --repository-format=apt \
          --location=us-west1 \
          --description="Apt repository"
      
    2. 執行下列指令來確認存放區已建立完成:

      gcloud artifacts repositories list
      

您現在可以將套件新增至存放區。

將套件新增至存放區

您可以使用 Google Cloud CLI 將套件上傳至存放區,也可以匯入儲存在 Cloud Storage 中的套件。如果您使用 Cloud Build 建構套件,建構作業會將套件儲存在 Cloud Storage 中,供您匯入。

在本快速入門導覽課程中,您將使用 gcloud artifacts apt upload 指令上傳範例檔案。

  1. 在 Cloud Shell 中,使用下列指令下載 Apt 快速參考表:

    apt download apt-dpkg-ref
    

    Apt 會從您設定的 Apt 存放區下載最新版本的套件。

    Get:1 http://deb.debian.org/debian buster/main amd64 apt-dpkg-ref all 5.3.1+nmu2 [113 kB]
    Fetched 113 kB in 0s (428 kB/s)
    

    執行 ls 即可取得 Apt 快速參考資料表的檔案名稱。檔案名稱與 apt-dpkg-ref_5.3.1+nmu2_all.deb 類似。

  2. 如要簡化 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 說明文件。

  3. 執行 gcloud artifacts apt upload,將套件上傳至存放區:

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

    FILE_NAME 替換為 Apt 快速參考資料表的路徑。

查看存放區中的套件

確認套件已新增至存放區。

主控台

  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

建立新的 Compute Engine VM,並在其中安裝範例套件。

在 Cloud Shell 中執行下列指令,建立名為 quickstart-apt-vm 的 VM 執行個體:

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

根據預設,VM 沒有使用存放區所需的存取權範圍--scopes 旗標會將 VM 的存取範圍設為 cloud-platform

設定套件管理工具

如要在 VM 上安裝套件,請將您建立的存放區新增至定義套件存放區的 Apt 設定檔。

  1. 前往「VM instances」(VM 執行個體) 頁面。

    開啟「VM instances」(VM 執行個體) 頁面

  2. 在 VM 所在資料列中,按一下「SSH」SSH。新視窗會隨即開啟,顯示 VM 上的終端機工作階段。

  3. 執行 gcloud init,在 VM 上初始化 Google Cloud CLI。

  4. 更新 Apt:

    sudo apt update
    
  5. 在 VM 上安裝 Apt 憑證輔助程式,讓 Apt 執行驗證:

    sudo apt install apt-transport-artifact-registry
    
  6. 設定 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 apt-dpkg-ref/quickstart-apt-repo
    

    傳回的安裝資訊如下列範例所示:

    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Selected version '5.3.1+nmu2' (Debian:10.7/stable, namespaces/my-repo/repositories/quickstart-apt-repo:quickstart
    -apt-repo [all]) for 'apt-dpkg-ref'
    The following NEW packages will be installed:
      apt-dpkg-ref
    0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
    Need to get 113 kB of archives.
    After this operation, 128 kB of additional disk space will be used.
    Get:1 http://deb.debian.org/debian buster/main amd64 apt-dpkg-ref all 5.3.1+nmu2 [113 kB]
    Fetched 113 kB in 0s (693 kB/s)
    Selecting previously unselected package apt-dpkg-ref.
    (Reading database ... 39506 files and directories currently installed.)
    Preparing to unpack .../apt-dpkg-ref_5.3.1+nmu2_all.deb ...
    Unpacking apt-dpkg-ref (5.3.1+nmu2) ...
    Setting up apt-dpkg-ref (5.3.1+nmu2) ...
    

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

移除存放區之前,請先確認要保留的套件均已存放於其他位置。

如要刪除存放區,請按照下列指示操作:

主控台

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

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

  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

後續步驟