在 Artifact Registry 中存储 Apt 软件包

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

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

准备工作

  1. In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Trusted Cloud project.

  3. Enable the Artifact Registry API.

    Enable the API

  4. 安装 gcloud CLI

    1. 如果尚未安装 Google Cloud CLI,请安装并初始化。对于现有安装,请使用以下命令更新 Google Cloud CLI 的组件:

      gcloud components update
      

      如需查看 gcloud CLI 的版本,请运行以下命令:

      gcloud version
      

    创建代码库

    为制品创建代码库。

    1. 创建代码库:

      控制台

      1. 在 Trusted Cloud 控制台中打开代码库页面。

        打开“代码库”页面

      2. 点击创建代码库

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

      4. 选择 Apt 作为格式。

      5. 位置类型下,选择区域,然后选择位置 u-france-east1

      6. 点击创建

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

      gcloud

      1. 在 Cloud Shell 中,运行以下命令,在位置 u-france-east1 的当前项目 quickstart-apt-repo 中创建一个新的 Apt 代码库。

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

        gcloud artifacts repositories list
        

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

    向代码库添加软件包

    您可以使用 Google Cloud CLI 将软件包上传到代码库,也可以导入存储在 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,并将默认位置设置为 u-france-east1。设置以上值之后,您无需在需要代码库或位置的 gcloud 命令中指定这些值。

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

      gcloud config set artifacts/repository quickstart-apt-repo
      

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

      gcloud config set artifacts/location u-france-east1
      

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

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

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

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

    查看代码库中的软件包

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

    控制台

    1. 在 Trusted 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://u-france-east1-apt.s3nsregistry.fr/projects/PROJECT quickstart-apt-repo main' | sudo tee -a  /etc/apt/sources.list.d/artifact-registry.list
      

      PROJECT 替换为您的 Trusted Cloud by S3NS项目 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) ...
      

    清理

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

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

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

    控制台

    1. 在 Trusted 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
    

    后续步骤