設定 VM 以安裝 Debian 套件

本頁說明如何設定 VM,以便從 Artifact Registry Apt 存放區安裝 Debian 套件。

事前準備

如果目標 Apt 存放區不存在,請建立標準遠端存放區。您可以建立私人存放區,也可以建立不需要驗證的公開存放區。

準備 VM 以存取 Apt 存放區

私人存放區

  1. 選擇要用來授予存取權的服務帳戶。

    • 對於 Compute Engine VM,這可能會是預設服務帳戶。Artifact Registry 會自動擷取使用服務帳戶的任何 VM 的 Compute Engine VM 服務帳戶憑證。
    • 針對其他 VM,請建立或選擇服務帳戶,以便代表 VM 執行作業。您需要服務帳戶金鑰檔案的位置,才能設定 VM。您可以在「Service Accounts」頁面中查看現有帳戶的金鑰,並建立金鑰。
  2. 將存放區存取權授予服務帳戶

  3. 準備 VM 以存取存放區。

    Compute Engine VM

    Debian VM

    1. cloud-platform API 存取權範圍指派給 VM。如要瞭解如何設定存取範圍,請參閱「變更執行個體的服務帳戶與存取範圍」。

    2. 使用下列指令更新 Apt:

      sudo apt update
      
    3. 在 VM 上安裝 apt-transport-artifact-registry 套件:

      sudo apt install apt-transport-artifact-registry
      

    Ubuntu VM

    1. 請使用下列指令安裝 Apt 存放區簽署金鑰:

      curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add - && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      

      LOCATION 替換為存放區的區域或多區域位置

    2. cloud-platform API 存取權範圍指派給 VM。如要瞭解如何設定存取範圍,請參閱「變更執行個體的服務帳戶與存取範圍」。

    3. 使用下列指令,設定 VM 以存取 Artifact Registry 套件:

      echo 'deb http://packages.cloud.google.com/apt apt-transport-artifact-registry-stable main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
      
    4. 使用下列指令更新 Apt:

      sudo apt update
      
    5. 在 VM 上安裝 apt-transport-artifact-registry 套件:

      sudo apt install apt-transport-artifact-registry
      

    其他 VM

    1. 請使用下列指令安裝 Apt 存放區簽署金鑰:

      curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add - && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      

      LOCATION 替換為存放區的區域或多區域位置

    2. 使用下列指令,設定 VM 以存取 Artifact Registry 套件:

      echo 'deb http://packages.cloud.google.com/apt apt-transport-artifact-registry-stable main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
      
    3. 使用下列指令更新 Apt:

      sudo apt update
      
    4. 在 VM 上安裝 apt-transport-artifact-registry 套件:

      sudo apt install apt-transport-artifact-registry
      
    5. /etc/apt/apt.conf.d/90artifact-registry 檔案中找出 #Service-Account-JSON "/path/to/creds.json"; 行,然後取消註解該行,並新增服務帳戶金鑰的路徑。

      設定檔項目:

      Service-Account-JSON  "PATH_TO_SERVICE_ACCOUNT_KEY";
      

      PATH_TO_SERVICE_ACCOUNT_KEY 替換為服務帳戶金鑰 JSON 檔案的路徑。

公開存放區

  1. 設定存放區的公開存取權

  2. 如果您要在 Google Cloud以外的環境中設定 VM,請準備好機器來存取存放區。如果您要設定 Compute Engine VM,請繼續進行下一個步驟。

    使用下列指令安裝公開簽名驗證金鑰:

    curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo apt-key add -
    

    LOCATION 替換為存放區的區域或多區域位置

  3. 使用下列指令更新 Apt:

    sudo apt update
    

設定 VM 以存取標準 Apt 存放區

設定 VM 以存取 Apt 存放區:

  1. 請執行 gcloud beta artifacts print-settings apt 指令,產生 VM 設定指令:

    gcloud beta artifacts print-settings apt \
        --repository=REPOSITORY \
        --location=LOCATION
    

    更改下列內容:

    • LOCATION 是存放區的地區或多地區位置
    • REPOSITORY 是 Artifact Registry 存放區的名稱。

    輸出結果會與下列內容相似:

    # To configure your package manager with this repository:
    
    # Update Apt:
    sudo apt update
    
    # Install the Apt credential helper:
    sudo apt install apt-transport-artifact-registry
    
    # Configure your VM to access Artifact Registry packages using the following
    # command:
    
    echo "deb ar+https://LOCATION-apt.pkg.dev/projects/PROJECT REPOSITORY main" | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    
    # Update Apt:
    sudo apt update
    
  2. 使用 print-settings 指令的輸出內容中的 echo 指令,設定 Apt 從儲存庫擷取套件。

    舉例來說,如要在專案 my-project 和位置 us-west1 中,從公開存放區 my-repo 安裝套件,請使用下列指令:

    echo 'deb ar+https://us-west1-apt.pkg.dev/projects/my-project my-repo main'
    
  3. 使用下列指令更新存放區來源:

    sudo apt update
    

Apt 現在可以連結至存放區。

如果您建立其他 Apt 存放區,可以將這些存放區新增至 artifact-registry.list 檔案,然後重新執行 sudo apt update 來更新存放區來源。

設定 VM 以存取遠端 Apt 存放區

如要將 VM 設定為只使用遠端 Apt 存放區,而非標準上游 Apt 存放區,請在 VM 的 /etc/apt/sources.list 檔案中,將標準存放區替換為遠端存放區。

  1. 將專案層級的 Artifact Registry 寫入權限授予預設的 Compute Engine 服務帳戶:

     gcloud projects add-iam-policy-binding PROJECT_ID \
       --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
       --role=roles/artifactregistry.writer
    
  2. 前往「VM instances」(VM 執行個體) 頁面

  3. 在 VM 的資料列中,按一下「SSH」SSH

    系統會開啟新視窗,並在 VM 上顯示終端機工作階段。

  4. 開啟 VM 的 /etc/apt/sources.list 檔案,然後找出要用新遠端存放區取代的標準存放區。

  5. 刪除標準存放區行,並替換為以下內容:

    deb ar+https://LOCATION-apt.pkg.dev/remote/PROJECT_ID/REMOTE_REPOSITORY_NAME UPSTREAM_REPOSITORY_NAME COMPONENTS
    

    更改下列內容:

    • LOCATION 與遠端存放區的區域或多區域位置
    • PROJECT_ID 替換為 VM 的專案 ID。
    • REMOTE_REPOSITORY_NAME 替換為您為遠端存放區提供的名稱。
    • UPSTREAM_REPOSITORY_NAME 替換為遠端的上游存放區名稱。
    • COMPONENTS,其中元件名稱清單以空格分隔。

    舉例來說,以下設定會讓 VM 從 us-east1 位置的專案 my-project 中的存放區 my-repo 擷取套件,並使用 buster 做為元件 maincontribnon-free 的上游存放區。

    deb ar+https://us-east1-apt.pkg.dev/remote/my-project/my-repo buster main contrib non-free
    
  6. 使用下列指令更新存放區來源:

    sudo apt update
    

    Apt 現在可以連結至存放區。

使用遠端 Apt 存放區做為備份

如果您想將標準上游做為第一個選項,並將遠端存放區做為備用選項,可以將標準上游保留在 sources.list 檔案中,然後將遠端存放區附加至 sources.list 檔案結尾,或在 sources.list.d 資料夾中建立新清單。

如要在 sources.list.d 資料夾中建立新的 artifact-registry.list 檔案,請執行下列指令:

echo 'deb ar+https://LOCATION-apt.pkg.dev/remote/PROJECT_ID/REMOTE_REPOSITORY_NAME UPSTREAM_REPOSITORY_NAME COMPONENTS' | sudo tee -a  /etc/apt/sources.list.d/artifact-registry.list

更改下列內容:

  • LOCATION 是存放區的地區或多地區位置
  • PROJECT_ID 替換為 VM 的專案 ID。
  • REMOTE_REPOSITORY_NAME 替換成您為遠端存放區提供的名稱。
  • UPSTREAM_REPOSITORY_NAME 替換為遠端上游存放區的名稱。
  • COMPONENTS,其中元件名稱清單以空格分隔。

如果您建立其他 Apt 存放區,可以將這些存放區新增至 artifact-registry.listsources.list 檔案,然後重新執行 sudo apt update 來更新存放區來源。

設定 HTTP 存取 Apt 存放區

如果您使用不支援 SSL 加密功能的舊版用戶端工具,可以設定 Apt 存放區的公開存取權,並透過 HTTP 存取該存放區。如果要求中提供驗證權杖,系統會拒絕傳送至可供公開存取的存放區的 HTTP 要求。

  1. 設定存放區的公開存取權

  2. 如要設定 Apt 使用 HTTP 從存放區擷取套件,請執行下列指令:

     echo 'deb http://LOCATION-apt.pkg.dev/projects/PROJECT_ID \
     REPOSITORY main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    

    更改下列內容:

    • LOCATION 是存放區的地區或多地區位置
    • PROJECT_ID 是存放區的專案 ID。
    • REPOSITORY 是存放區名稱。

    舉例來說,如要使用 HTTP 從專案 my-project 和位置 us-west1 中的公開存放區 my-repo 安裝套件,請使用下列指令:

    echo 'deb http://us-west1-apt.pkg.dev/projects/my-project my-repo main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list
    
  3. 使用下列指令更新存放區來源:

    sudo apt update
    

Apt 現在可以連結至存放區。

如果您建立其他 Apt 存放區,可以將這些存放區新增至 artifact-registry.list 檔案,然後重新執行 sudo apt update 來更新存放區來源。

後續步驟