設定 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. 如果您在 Google Cloud以外設定 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 的 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,並將 location 替換為遠端存放區的單一區域或多區域位置。
    • PROJECT_ID 替換為 VM 的專案 ID。
    • REMOTE_REPOSITORY_NAME,其中包含您為遠端存放區指定的名稱。
    • UPSTREAM_REPOSITORY_NAME,並將遠端上游存放區的名稱替換為您的名稱。
    • COMPONENTS,並以空白字元分隔元件名稱。

    舉例來說,下列設定會將 VM 設為從專案 my-project 中位置 us-east1 的存放區 my-repo 擷取套件,並使用 stable 做為元件 main、contrib 和 non-free 的上游存放區。

    deb ar+https://us-east1-apt.pkg.dev/remote/my-project/my-repo stable 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.list 或 sources.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 的位置 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 來更新存放區來源。

後續步驟