設定虛擬機器 (VM) 執行個體,以便連線至 Artifact Registry 您可以使用標準 Apt 工具,透過 Artifact Registry Apt 存放區安裝及更新 Debian 套件。您可以為 Compute Engine VM 設定驗證和存放區來源,從私人或公開存放區提取套件。
事前準備
如果目標 Apt 存放區不存在,請建立標準或遠端存放區。您可以建立私人存放區,或是不需要驗證的公開存放區。準備 VM 以存取 Apt 存放區
私人存放區
選擇要用來授予存取權的服務帳戶。
將存放區存取權授予服務帳戶。
準備 VM 以存取存放區。
Compute Engine VM
Debian VM
為 VM 指派
cloud-platformAPI 存取權範圍。如要瞭解如何設定存取權範圍,請參閱「變更執行個體的服務帳戶和存取權範圍」。使用下列指令更新 Apt:
sudo apt update在 VM 上安裝
apt-transport-artifact-registry套件:sudo apt install apt-transport-artifact-registry
Ubuntu VM
使用下列指令安裝 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替換為存放區的區域或多區域位置。為 VM 指派
cloud-platformAPI 存取權範圍。如要瞭解如何設定存取權範圍,請參閱「變更執行個體的服務帳戶和存取權範圍」。使用下列指令,將 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使用下列指令更新 Apt:
sudo apt update在 VM 上安裝
apt-transport-artifact-registry套件:sudo apt install apt-transport-artifact-registry
其他 VM
使用下列指令安裝 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替換為存放區的區域或多區域位置。使用下列指令,將 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使用下列指令更新 Apt:
sudo apt update在 VM 上安裝
apt-transport-artifact-registry套件:sudo apt install apt-transport-artifact-registry在
/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 檔案的路徑。
公開存放區
如果您在 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替換為存放區的區域或多區域位置。使用下列指令更新 Apt:
sudo apt update
設定 VM 來存取標準 Apt 存放區
設定 VM 以存取 Apt 存放區:
執行
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
-
使用
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'
使用下列指令更新存放區來源:
sudo apt update
Apt 現在可以連線至存放區。
如果您建立其他 Apt 存放區,可以將其新增至 artifact-registry.list 檔案,然後重新執行 sudo apt update 來更新存放區來源。
設定 VM 以存取遠端 Apt 存放區
如要設定 VM,只使用遠端 Apt 存放區,而非標準上游 Apt 存放區,請在 VM 的 /etc/apt/sources.list 檔案中,將標準存放區換成遠端存放區。
將專案範圍的 Artifact Registry 寫入權限授予預設的 Compute Engine 服務帳戶:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/artifactregistry.writer在 VM 所在資料列中,按一下「SSH」SSH。
新視窗隨即開啟,顯示 VM 上的終端機工作階段。
開啟 VM 的
/etc/apt/sources.list檔案,找出要以新遠端存放區取代的標準存放區。刪除標準存放區行,並替換為下列內容:
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使用下列指令更新存放區來源:
sudo apt updateApt 現在可以連線至存放區。
使用遠端 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 傳送至可公開讀取的存放區的要求將會遭到拒絕。
如要設定 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
-
使用下列指令更新存放區來源:
sudo apt update
Apt 現在可以連線至存放區。
如果您建立其他 Apt 存放區,可以將其新增至 artifact-registry.list 檔案,然後重新執行 sudo apt update 來更新存放區來源。