在 Artifact Registry 中存储 Apt 软件包

本快速入门介绍如何设置私有 Artifact Registry Apt 代码库,将 Debian 软件包添加到代码库,以及如何在运行基于 Debian 的操作系统的 Compute Engine 虚拟机上安装该软件包。

您可以使用 Cloud Shell 执行本快速入门中的步骤,也可以在本快速入门中创建的虚拟机上的 shell 中执行这些步骤。

如需详细了解如何管理 Debian 软件包,请参阅使用 Debian 软件包

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Artifact Registry API。

    启用 API

  5. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Artifact Registry API。

    启用 API

启动 Cloud Shell

Cloud Shell 预安装有 Google Cloud CLI。gcloud CLI 提供了 Google Cloud 的主要命令行界面。

启动 Cloud Shell:

  1. 转到 Google Cloud 控制台。

    Google Cloud 控制台

  2. 在 Google Cloud 控制台工具栏中,点击激活 Cloud Shell

控制台下方的框架内会打开一个 Cloud Shell 会话。您将使用此 shell 运行 gcloud 命令以创建虚拟机和代码库。

创建代码库

为工件创建代码库。

  1. 创建代码库:

    控制台

    1. 打开 Google Cloud 控制台中的制品库页面。

      打开“代码库”页面

    2. 点击创建代码库

    3. 指定 quickstart-apt-repo 作为该代码库的名称。

    4. 选择 Apt 作为格式。

    5. 位置类型下,选择区域,然后选择位置 us-central1

    6. 点击创建

    该代码库会被添加到代码库列表中。

    gcloud

    1. 在 Cloud Shell 中运行以下命令,在当前项目中名为 quickstart-apt-repo 的位置 us-central1 中创建新的 Apt 代码库。

      gcloud artifacts repositories create quickstart-apt-repo \
          --repository-format=apt \
          --location=us-central1 \
          --description="Apt repository"
      
    2. 运行以下命令确认已创建代码库:

      gcloud artifacts repositories list
      

您现在可以向代码库添加软件包了。

将软件包添加到代码库

您可以使用 Google Cloud CLI 将软件包上传到代码库,也可以导入存储在 Cloud Storage 中的软件包。如果您使用 Cloud Build 构建软件包,该 build 可以将软件包存储在 Cloud Storage 中,以便您导入。

在本快速入门中,您可以使用 gcloud artifacts apt upload 命令上传示例文件。

  1. 在 Cloud Shell 中,使用以下命令下载 Apt 快速参考表格:

    apt download apt-dpkg-ref
    

    Apt 会从您配置的 Apt 代码库下载最新版本的软件包。

    Get:1 http://deb.debian.org/debian buster/main amd64 apt-dpkg-ref all 5.3.1+nmu2 [113 kB]
    Fetched 113 kB in 0s (428 kB/s)
    

    运行 ls 以获取 Apt 快速参考表格的文件名。文件名类似于 apt-dpkg-ref_5.3.1+nmu2_all.deb

  2. 为了简化 gcloud 命令,请将默认代码库设置为 quickstart-apt-repo,并将默认位置设置为 us-central1。设置以上值之后,您无需在需要代码库或位置的 gcloud 命令中指定这些值。

    如需设置代码库,请运行以下命令:

    gcloud config set artifacts/repository quickstart-apt-repo
    

    如需设置位置,请运行以下命令:

    gcloud config set artifacts/location us-central1
    

    如需详细了解这些命令,请参阅 gcloud config set 文档。

  3. 运行 gcloud artifacts apt upload 以将软件包上传到代码库:

    gcloud artifacts apt upload quickstart-apt-repo \
        --source=FILE_NAME
    

    FILE_NAME 替换为 Apt 快速参考表格的路径。

查看代码库中的软件包

验证您的软件包是否已添加到代码库中。

控制台

  1. 打开 Google Cloud 控制台中的制品库页面。

    打开“代码库”页面

  2. 在代码库列表中,点击 quickstart-apt-repo 代码库。

    软件包页面列出代码库中的软件包。

gcloud

如需列出 quickstart-apt-repo 代码库中的软件包,请运行以下命令:

 gcloud artifacts packages list

如需在 quickstart-apt-repo 中查看软件包的版本,请运行以下命令:

gcloud artifacts versions list --package=apt-dpkg-ref

创建虚拟机

创建一个新的 Compute Engine 虚拟机,您将在其中安装示例软件包。

在 Cloud Shell 中,运行以下命令以创建名为 quickstart-apt-vm 的虚拟机实例:

gcloud compute instances create quickstart-apt-vm \
    --image-family=debian-10 \
    --image-project=debian-cloud \
    --scopes=cloud-platform

默认情况下,虚拟机没有使用代码库所需的访问权限范围--scopes 标志将虚拟机的访问权限范围设置为 cloud-platform

配置软件包管理器

如需在虚拟机上安装软件包,请将您创建的代码库添加到定义软件包代码库的 Apt 配置文件中。

  1. 转到“虚拟机实例”页面。

    打开“虚拟机实例”页面

  2. 在您的虚拟机所在的行中,点击 SSH。系统会打开一个包含虚拟机上的终端会话的新窗口。

  3. 运行 gcloud init 以在您的虚拟机上初始化 Google Cloud CLI。

  4. 更新 Apt:

    sudo apt update
    
  5. 在虚拟机上安装 Apt 凭据帮助程序,以使 Apt 能够执行身份验证:

    sudo apt install apt-transport-artifact-registry
    
  6. 使用以下命令配置虚拟机以访问 Artifact Registry 软件包:

    echo 'deb ar+https://us-central1-apt.pkg.dev/projects/PROJECT quickstart-apt-repo main' | sudo tee -a  /etc/apt/sources.list.d/artifact-registry.list
    

    PROJECT 替换为您的 Google Cloud 项目 ID

安装软件包

安装已添加到代码库的软件包。

  1. 更新可用软件包的列表:

    sudo apt update
    
  2. 在您的代码库中安装软件包。

    sudo apt install apt-dpkg-ref/quickstart-apt-repo
    

    返回的安装信息如以下示例所示:

    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Selected version '5.3.1+nmu2' (Debian:10.7/stable, namespaces/my-repo/repositories/quickstart-apt-repo:quickstart
    -apt-repo [all]) for 'apt-dpkg-ref'
    The following NEW packages will be installed:
      apt-dpkg-ref
    0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
    Need to get 113 kB of archives.
    After this operation, 128 kB of additional disk space will be used.
    Get:1 http://deb.debian.org/debian buster/main amd64 apt-dpkg-ref all 5.3.1+nmu2 [113 kB]
    Fetched 113 kB in 0s (693 kB/s)
    Selecting previously unselected package apt-dpkg-ref.
    (Reading database ... 39506 files and directories currently installed.)
    Preparing to unpack .../apt-dpkg-ref_5.3.1+nmu2_all.deb ...
    Unpacking apt-dpkg-ref (5.3.1+nmu2) ...
    Setting up apt-dpkg-ref (5.3.1+nmu2) ...
    

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

在移除代码库之前,请确保已在其他位置保存了您要保留的任何软件包。

如需删除代码库,请执行以下操作:

控制台

  1. 打开 Google Cloud 控制台中的制品库页面。

    打开“代码库”页面

  2. 在代码库列表中,选择 quickstart-apt-repo 代码库。

  3. 点击删除

gcloud

  1. 如需删除 quickstart-apt-repo 代码库,请运行以下命令:

    gcloud artifacts repositories delete quickstart-apt-repo
    
  2. 如果要移除您为活跃的 gcloud 配置而配置的默认代码库和位置信息设置,请运行以下命令:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

如需删除您创建的虚拟机,请运行以下命令:

gcloud compute instances delete quickstart-apt-vm

后续步骤