管理 Debian 软件包

本页介绍了如何在 Apt 代码库中添加、查看和删除 Debian 软件包。

准备工作

  1. 如果目标 Apt 代码库不存在,请创建新代码库
  2. 配置虚拟机以访问代码库。
  3. (可选)为 gcloud 命令配置默认值
  4. 确保元数据中的软件包名称符合 Debian 软件包名称标准

添加软件包

代码库模式:标准

您必须同时拥有对代码库的读取和写入权限,才能添加软件包。

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

导入或上传操作时间过长可能会超出 gcloud CLI 用于调用 API 的令牌的到期期限。如果您要添加的软件包数量非常多,不妨考虑分批添加,以便缩短每次上传或导入操作的完成时间。如需了解详情,请参阅操作系统软件包问题排查文档。

直接上传

使用 gcloud artifacts apt upload 命令将软件包上传到 Apt 代码库:

gcloud artifacts apt upload REPOSITORY \
    --location=LOCATION \
    --source=PACKAGES

替换以下值:

  • REPOSITORY 是 Artifact Registry 制品库名称。
  • LOCATION 是代码库的单区域或多区域位置
  • PACKAGES 是软件包的路径。

例如,如需将软件包 my-package.deb 上传到位置 us-west1 中的 Apt 代码库 my-repo,请运行以下命令:

gcloud artifacts apt upload my-repo \
    --location=us-west1 \
    --source=my-package.deb

Cloud Storage

  1. 请执行以下操作,将软件包上传到 Cloud Storage:
    • 直接上传软件包到 Cloud Storage 存储分区
    • 使用 Cloud Build 构建软件包并将其存储在存储分区中
  2. 运行以下命令:
    gcloud artifacts apt import REPOSITORY \
      --location=LOCATION \
      --gcs-source=PACKAGES
    

    替换以下值:

    • REPOSITORY 是 Artifact Registry 制品库名称。
    • LOCATION 是代码库的单区域或多区域位置
    • PACKAGES 是 Cloud Storage 中软件包的英文逗号分隔列表。如需上传目录中的所有软件包,请使用目录通配符 (*) 或递归目录通配符 (**) 上传所有子目录中的所有软件包。

    例如,如需将软件包 package.debdirectory 目录中的所有软件包从存储分区 my-bucket 上传到位置 us-west1 中的 Apt 代码库 my-repo,请运行以下命令:

    gcloud artifacts apt import my-repo \
      --location=us-west1 \
      --gcs-source=gs://my-bucket/path/to/package.deb,gs://my-bucket/directory*
    

上传或导入操作完成后,您可以使用Google Cloud 控制台或 gcloud CLI 查看代码库中的软件包,并确认它们已成功上传。

如果您有包含大量软件包的大型代码库,则可能需要几分钟才能重新生成软件包索引,以便 Apt 客户端可以看到新软件包。

查看软件包

代码库模式:标准、远程(预览版)

如需查看软件包,您必须拥有 Artifact Registry Reader 角色的权限。Artifact Registry 不会列出软件包内的文件。

如需使用 Google Cloud 控制台或 gcloud 查看软件包和软件包版本,请执行以下操作:

控制台

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

    打开“代码库”页面

  2. 在代码库列表中,点击相应代码库。

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

  3. 点击一个软件包以查看它的版本。

gcloud

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

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

替换以下内容:

  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的单区域或多区域位置。 如果您配置了默认位置,则可以省略此标志以使用默认值。

如需查看软件包的版本,请运行以下命令:

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

替换以下内容:

  • PACKAGE 是软件包的 ID 或软件包的完全限定标识符。
  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是代码库的单区域或多区域位置。 使用此标志可查看特定位置中的代码库。 如果您配置了默认位置,则可以省略此标志以使用默认值。

安装软件包

代码库模式:标准、远程(预览版)

在安装软件包之前,请验证您是否已正确配置软件包管理器和代码库。

如需在代码库中安装软件包,请执行以下操作:

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

    sudo apt update
    
  2. 安装软件包,指定在 Apt 中配置的代码库的名称。

    sudo apt install PACKAGE/REPOSITORY
    

    替换以下值:

    • PACKAGE 是软件包名称。
    • REPOSITORY 是 Artifact Registry 制品库的名称。对于远程仓库,请使用标准上游仓库的名称。

    例如,如需从代码库 my-repo 安装软件包 my-package,请运行以下命令:

    sudo apt install my-package -t my-repo
    

删除软件包

代码库模式:标准

Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    打开“代码库”页面

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Replace the following:

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud console.

    打开“代码库”页面

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Replace the following:

  • VERSION is the name of the version to delete.
  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, then you can omit this flag to use the default.
  • LOCATION is the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.

The --async flag causes the command to return immediately, without waiting for the operation in progress to complete.

对于大型代码库,重新生成 Apt 软件包索引可能需要几分钟时间,以反映删除操作。

后续步骤