在 Artifact Registry 中存储 Apt 软件包
本快速入门介绍了如何设置私有 Artifact Registry Apt 代码库、向该代码库添加 Debian 软件包,以及在运行基于 Debian 的操作系统的 Compute Engine 虚拟机上安装该软件包。
如需详细了解如何管理 Debian 软件包,请参阅使用 Debian 软件包。
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. 前往 Google Cloud 控制台。
在 Google Cloud 控制台中,点击激活 Cloud Shell图标
。创建代码库:
控制台
在 Google Cloud 控制台中打开代码库页面。
点击创建代码库。
指定
quickstart-apt-repo作为该代码库的名称。选择 Apt 作为格式。
在位置类型下,选择区域,然后选择位置
us-west1。点击创建。
该代码库会被添加到代码库列表中。
gcloud
在 Cloud Shell 中运行以下命令,在位置
us-west1的当前项目quickstart-apt-repo中创建一个新的 Apt 代码库。gcloud artifacts repositories create quickstart-apt-repo \ --repository-format=apt \ --location=us-west1 \ --description="Apt repository"运行以下命令确认已创建代码库:
gcloud artifacts repositories list
转到“虚拟机实例”页面。
在您的虚拟机所在的行中,点击 SSH。系统会打开一个新窗口,其中包含虚拟机上的终端会话。
运行
gcloud init以在虚拟机上初始化 Google Cloud CLI。更新 Apt:
sudo apt update使用以下命令下载 curl 软件包:
apt download curlApt 会从您配置的 Apt 代码库下载软件包的最新版本。
Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:2 https://deb.debian.org/debian bookworm/main amd64 curl amd64 7.88.1-10+deb12u14 [316 kB] Fetched 316 kB in 0s (3078 kB/s)运行
ls以获取 curl 软件包的文件名。文件名类似于curl_7.88.1-10+deb12u14_amd64.deb。为了简化
gcloud命令,请将默认代码库设置为quickstart-apt-repo,并将默认位置设置为us-west1。设置以上值之后,您无需在需要代码库或位置的gcloud命令中指定这些值。如需设置代码库,请运行以下命令:
gcloud config set artifacts/repository quickstart-apt-repo如需设置位置,请运行以下命令:
gcloud config set artifacts/location us-west1如需详细了解这些命令,请参阅 gcloud config set 文档。
运行
gcloud artifacts apt upload将软件包上传到代码库:gcloud artifacts apt upload quickstart-apt-repo \ --source=FILE_NAME将
FILE_NAME替换为 curl 软件包的路径。在 Google Cloud 控制台中打开代码库页面。
在代码库列表中,点击 quickstart-apt-repo 代码库。
软件包页面列出代码库中的软件包。
在虚拟机上安装 Apt 凭据帮助程序,以使 Apt 能够执行身份验证:
sudo apt install apt-transport-artifact-registry配置虚拟机以访问 Artifact Registry 软件包:
echo 'deb ar+https://us-west1-apt.pkg.dev/projects/PROJECT quickstart-apt-repo main' | sudo tee -a /etc/apt/sources.list.d/artifact-registry.list将 PROJECT 替换为您的 Google Cloud项目 ID。
更新可用软件包的列表:
sudo apt update从您的代码库安装软件包。
sudo apt install curl/quickstart-apt-repo返回的安装信息类似于以下示例:
Reading package lists... Done Building dependency tree... Done Reading state information... Done Selected version '7.88.1-10+deb12u14' (quickstart-apt-repo:quickstart-apt-repo, Debian:12.12/oldstable [amd64]) for 'curl' The following additional packages will be installed: libcurl3-gnutls libcurl4 The following packages will be upgraded: curl libcurl3-gnutls libcurl4 3 upgraded, 0 newly installed, 0 to remove and 77 not upgraded. Need to get 1093 kB of archives. After this operation, 0 B of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:2 https://deb.debian.org/debian bookworm/main amd64 libcurl4 amd64 7.88.1-10+deb12u14 [392 kB] Get:3 https://deb.debian.org/debian bookworm/main amd64 libcurl3-gnutls amd64 7.88.1-10+deb12u14 [386 kB] Get:4 ar+https://us-west1-apt.pkg.dev/projects/PROJECT quickstart-apt-repo/main amd64 curl amd64 7.88.1-10+deb12u14 [316 kB] Fetched 1093 kB in 1s (947 kB/s) Reading changelogs... Done (Reading database ... 72080 files and directories currently installed.) Preparing to unpack .../curl_7.88.1-10+deb12u14_amd64.deb ... Unpacking curl (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ... Preparing to unpack .../libcurl4_7.88.1-10+deb12u14_amd64.deb ... Unpacking libcurl4:amd64 (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ... Preparing to unpack .../libcurl3-gnutls_7.88.1-10+deb12u14_amd64.deb ... Unpacking libcurl3-gnutls:amd64 (7.88.1-10+deb12u14) over (7.88.1-10+deb12u12) ... Setting up libcurl3-gnutls:amd64 (7.88.1-10+deb12u14) ... Setting up libcurl4:amd64 (7.88.1-10+deb12u14) ... Setting up curl (7.88.1-10+deb12u14) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u10) ...
在 Google Cloud 控制台中打开代码库页面。
在代码库列表中,选择 quickstart-apt-repo 代码库。
点击删除。
如需删除
quickstart-apt-repo代码库,请运行以下命令:gcloud artifacts repositories delete quickstart-apt-repo如果要移除您为活跃的
gcloud配置而配置的默认代码库和位置信息设置,请运行以下命令:gcloud config unset artifacts/repository gcloud config unset artifacts/location- 了解如何使用 Deb 软件包。
- 阅读有关 DevOps 的文章,并了解 DevOps 研究项目。
启动 Cloud Shell
Cloud Shell 预安装有 Google Cloud CLI。gcloud CLI 为 Google Cloud提供了主要命令行界面。
启动 Cloud Shell:
控制台下方的框架内会打开一个 Cloud Shell 会话。您将使用此 shell 运行 gcloud 命令来创建虚拟机和代码库。
创建代码库
为工件创建代码库。
您现在可以向代码库添加软件包。Cloud Shell 使用的是 Google 构建的 Ubuntu 映像,而不是 Debian。如需管理代码库中的 Debian 软件包,您将使用采用 Debian 操作系统映像的虚拟机。
创建虚拟机
创建一个新的 Compute Engine 虚拟机,您将在其中安装示例软件包。
在 Cloud Shell 中,运行以下命令以创建名为 quickstart-apt-vm 的虚拟机实例:
gcloud compute instances create quickstart-apt-vm \
--image-family=debian-12 \
--image-project=debian-cloud \
--scopes=cloud-platform
默认情况下,虚拟机不具备使用代码库所需的访问权限范围。--scopes 标志将虚拟机的访问权限范围设置为 cloud-platform。
向代码库添加软件包
您可以使用 Google Cloud CLI 将软件包上传到代码库,也可以导入存储在 Cloud Storage 中的软件包。如果您使用 Cloud Build 构建软件包,则构建过程可以将软件包存储在 Cloud Storage 中,供您导入。在本快速入门中,您将使用 gcloud artifacts apt upload 命令上传示例文件。
查看代码库中的软件包
验证您的软件包是否已添加到代码库中。
控制台
gcloud
如需列出 quickstart-apt-repo 代码库中的软件包,请运行以下命令:
gcloud artifacts packages list
如需查看 quickstart-apt-repo 中软件包的版本,请运行以下命令:
gcloud artifacts versions list --package=apt-dpkg-ref
配置软件包管理系统
如需在虚拟机上安装软件包,请将您创建的代码库添加到定义软件包代码库的 Apt 配置文件中。
安装软件包
安装您添加到代码库中的软件包。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
在移除代码库之前,请确保已在其他位置保存了您要保留的任何软件包。
如需删除代码库,请执行以下操作:
控制台
gcloud
如需删除您创建的虚拟机,请运行以下命令:
gcloud compute instances delete quickstart-apt-vm