設定 VM 以安裝 Debian 套件

Artifact Registry Apt 存放區可讓您控管虛擬機器 (VM) 安裝的 Debian 套件版本。您可以設定 Compute Engine 執行個體或外部 VM,從私人或公開存放區安裝套件。

事前準備

如果目標 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 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. 將 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 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 來更新存放區來源。

後續步驟