将虚拟机 (VM) 实例配置为连接到 Artifact Registry Apt 代码库后,您就可以使用标准 Apt 工具安装和更新 Debian 软件包。您可以为 Compute Engine 虚拟机设置身份验证和代码库来源,以便从私有或公共代码库拉取软件包。
准备工作
如果目标 Apt 代码库不存在,请创建一个新的标准代码库。您可以创建私有代码库,也可以创建不需要身份验证的公共代码库。准备虚拟机以访问 Apt 代码库
私有代码库
选择您将用于授予访问权限的服务账号。
向您的服务账号授予代码库访问权限。
准备虚拟机以访问代码库。
Compute Engine 虚拟机
Debian 虚拟机
为虚拟机分配
cloud-platformAPI 访问权限范围。如需了解如何设置访问权限范围,请参阅更改实例的服务账号和访问权限范围。使用以下命令更新 Apt:
sudo apt update在虚拟机上安装
apt-transport-artifact-registry软件包:sudo apt install apt-transport-artifact-registry
Ubuntu 虚拟机
使用以下命令安装 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替换为相应制品库的区域位置。为虚拟机分配
cloud-platformAPI 访问权限范围。如需了解如何设置访问权限范围,请参阅更改实例的服务账号和访问权限范围。使用以下命令将虚拟机配置为访问 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在虚拟机上安装
apt-transport-artifact-registry软件包:sudo apt install apt-transport-artifact-registry
其他虚拟机
使用以下命令安装 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替换为相应制品库的区域位置。使用以下命令将虚拟机配置为访问 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在虚拟机上安装
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 文件的路径。
公共代码库
如果您要在 Cloud de Confiance之外配置虚拟机,请准备好您的机器以访问该代码库。如果您要配置 Compute Engine 虚拟机,请继续执行下一步。
使用以下命令安装公开签名验证密钥:
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
配置虚拟机以访问标准 Apt 代码库
配置虚拟机以访问 Apt 代码库:
运行
gcloud beta artifacts print-settings apt命令,生成虚拟机配置命令: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和位置u-france-east1中安装来自公共代码库my-repo的软件包,请使用以下命令:echo 'deb ar+https://u-france-east1-apt.pkg.dev/projects/my-project my-repo main'
使用以下命令更新代码库来源:
sudo apt update
Apt 现在可以连接到代码库。
如果您创建了其他 Apt 代码库,可以将其添加到 artifact-registry.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中的位置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
-
使用以下命令更新代码库来源:
sudo apt update
Apt 现在可以连接到代码库。
如果您创建了其他 Apt 代码库,可以将其添加到 artifact-registry.list 文件中,然后重新运行 sudo apt update 以更新代码库来源。