設定 VM 以安裝 Debian 套件

設定虛擬機器 (VM) 執行個體,以便連線至 Artifact Registry 您可以使用標準 Apt 工具,透過 Artifact Registry Apt 存放區安裝及更新 Debian 套件。您可以為 Compute Engine VM 設定驗證和存放區來源,從私人或公開存放區提取套件。

事前準備

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

準備 VM 以存取 Apt 存放區

私人存放區

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

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

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

    Compute Engine VM

    Debian VM

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

    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 gpg --dearmor -o /etc/apt/trusted.gpg.d/artifact-registry.gpg && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/google-cloud-sdk.gpg
      

      將 LOCATION 替換為存放區的區域性位置。

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

    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 gpg --dearmor -o /etc/apt/trusted.gpg.d/artifact-registry.gpg && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/google-cloud-sdk.gpg
      

      將 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. 如果您在 Cloud de Confiance以外設定 VM,請準備好機器以存取存放區。如果正在設定 Compute Engine VM,請繼續下一個步驟。

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

    curl https://LOCATION-apt.pkg.dev/doc/repo-signing-key.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/artifact-registry.gpg
    

    將 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 [signed-by=/etc/apt/trusted.gpg.d/artifact-registry.gpg] 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 的 u-france-east1 位置安裝公開存放區 my-repo 中的套件,請使用下列指令:

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

    sudo apt update
    

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

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

設定 Apt 存放區的 HTTP 存取權

如果使用不支援 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 的位置 u-france-east1 安裝公開存放區 my-repo 中的套件,請使用下列指令:

    echo 'deb http://u-france-east1-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 來更新存放區來源。

後續步驟