管理 npm 软件包

本页面介绍了以下任务:

  • 查看和删除软件包与软件包版本
  • 查看、创建、更新和删除标记

软件包管理目前为 Alpha 版。它仅适用于 Alpha 版用户,可能不包含容器管理可用的所有特性。若要申请 Alpha 版,请填写注册表单

准备工作

  1. 如果目标代码库不存在,请创建新代码库
  2. 验证您是否拥有访问代码库所需的权限
  3. 为 npm 配置身份验证
  4. (可选)为 gcloud 命令配置默认值

添加软件包

您只能发布一次特定版本的软件包。这是 npm 限制,用于确保已发布的软件包版本的内容始终相同。因此,您不能:

  • 通过将软件包版本再次发布到代码库来覆盖此版本
  • 从代码库中移除软件包或其版本,然后发布具有相同名称和版本号的软件包

如需添加软件包,请执行以下操作:

  1. 确保 package.json 中的软件包名称包含为您的代码库配置的范围。以下示例展示了范围为 dev-repo 的软件包。

    "name": "@dev-repo/my-package"
    
  2. 请刷新用于连接到代码库的访问令牌。google-artifactregistry-auth 是一个客户端库,用于更新 Artifact Registry 代码库的凭据。

    如需刷新凭据,请使用以下任一方法:

    • 直接使用 npx 来刷新访问令牌。如果您使用的是 npm 5.2.0 或更高版本,则凭据将包含在 npm 中。

      1. 确保用于连接到公共 npm 注册表的凭据位于用户的 npm 配置文件 ~/.npmrc 中。

      2. 在 npm 项目上方的文件夹中运行以下命令。

      npx google-artifactregistry-auth PROJECT-NPMRC
      

      其中 PROJECT-NPMRC 是项目目录中 .npmrc 文件的路径。

      您必须在项目目录之外运行该命令,以便 npx 使用 ~/.npmrc 中的公共 npm 注册表凭据来下载 google-artifactregistry-auth

    • 将脚本添加到项目的 package.json 文件中。

      "scripts": {
        "artifactregistry-login": "npx google-artifactregistry-auth"
      }
      
      npm run artifactregistry-login PROJECT-NPMRC --registry https://registry.npmjs.org/
      

      其中 PROJECT-NPMRC 是项目目录中 .npmrc 文件的路径。

    • 对于低于 5.2.0 的 npm 版本,请执行以下步骤:

      1. 运行以下命令:
      npm install google-artifactregistry-auth --save-dev --registry https://registry.npmjs.org/
      
      1. 将其添加到身份验证脚本中:
      "scripts": {
          "artifactregistry-login": "./node_modules/.bin/artifactregistry-auth",
      }
      

      运行脚本

      npm run artifactregistry-login PROJECT-NPMRC
      

      其中 PROJECT-NPMRC 是项目目录中 .npmrc 文件的路径。

  3. 将软件包添加到代码库中。您可以使用 npmyarn 命令。

    npm publish
    
    yarn publish
    

查看软件包和版本

要使用 npmyarn 获取软件包信息,请运行相应的命令:

npm view
yarn info

To view packages and package versions using the Google Cloud Console or gcloud:

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 the package.

gcloud

To list packages in a repository, run the following command:

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

Where

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

To view versions of a package, run the following command:

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

Where

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

标记软件包

You can view, add, update, and delete tags. Tags can help you manage semantic versions of your packages and streamline installation of packages at a specific stage of development.

For example, you can tag the current release candidate build with rc. Your team can then install the correct version based on the tag instead of a version specifier, and unpublishing unused pre-release versions won't break your dependencies on the release candidate package.

Viewing tags

To view tags for a package:

Console

  1. Open the Repositories page in the Cloud Console.

    打开代码库页面

  2. Click the package to view versions and the associated tags.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the command:

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

Where

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

For example, to view tags for the package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags list --package=my-pkg --repository=my-repo

Creating tags

You can create a tag for a specific version of a package.

To tag an existing image in a repository:

Console

  1. Open the Repositories page in the Cloud Console.

    打开代码库页面

  2. Click the package to view versions of the package.

  3. Select the package version to tag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Type new tags into the field and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags create TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

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

For example, to create the tag release-candidate for version 1.0.0 of package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags create release-candidate --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Updating tags

You can change a tag associated with a package version.

To change an existing tag:

Console

  1. Open the Repositories page in the Cloud Console.

    打开代码库页面

  2. Click the package to view versions of the package.

  3. Select the package version with the tag to change.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Edit the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags update TAG --package=PACKAGE \
    version=VERSION [--location=LOCATION] [--repository=REPOSITORY]

Where

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

For example, to change the tag for version 1.0.0 of package my-package to production in the repository my-repo in the default location, run the command:

gcloud artifacts tags update production --version=1.0.0 \
    --package=my-pkg --repository=my-repo

Untagging package versions

You can remove an existing tag from a package version.

To remove a tag:

Console

  1. Open the Repositories page in the Cloud Console.

    打开代码库页面

  2. Click the image to view versions of the image.

  3. Select the image version to untag.

  4. In the row of the selected version, click More actions (More actions), and then click Edit tags.

  5. Delete the tag and then click SAVE.

gcloud

Run the following command:

gcloud artifacts tags delete TAG --package=PACKAGE \
    [--location=<LOCATION] [--repository=REPOSITORY]

Where

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

For example, to remove the tag release-candidate from package my-package in the repository my-repo in the default location, run the command:

gcloud artifacts tags delete release-candidate --package=my-pkg \
    --repository=my-repo

安装软件包

如需从 npm 代码库安装软件包,请使用 npm installyarn add 命令。

npm install @SCOPE/PACKAGE
yarn add @SCOPE/PACKAGE

其中

  • SCOPE 是与代码库关联的范围
  • PACKAGE 是代码库中的软件包名称

如果您的 npm 代码库没有配置范围,请在命令中省略范围:

npm install PACKAGE
yarn add PACKAGE

package.json 中将软件包指定为依赖项时,请务必添加代码库的范围。以下示例展示了名为 my-package 的软件包的 @dev-repo 范围。

"dependencies": {
  "@dev-repo/my-package": ">=1.0.0"
}

删除软件包

在删除软件包或软件包版本之前,请确认您已在其中告知或解决任何重要依赖项。

软件包版本发布后,即使您删除了版本,也无法重新发布具有相同名称和版本组合的软件包。这是 npm 限制,用于确保已发布的软件包版本的内容始终相同。

如果要鼓励用户安装更新后的软件包版本,请使用 npm deprecate 命令将旧版软件包标记为已弃用。当用户尝试安装已弃用的软件包时,Artifact Registry 会返回弃用警告。

如需删除软件包,请按如下所述操作:

控制台

  1. 在 Google Cloud Console 中打开代码库页面。

    打开代码库页面

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

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

  3. 选择要删除的软件包。

  4. 点击删除

  5. 在确认对话框中,点击删除

gcloud

运行以下命令:

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

其中

  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是单区域或多区域位置。使用此标志可查看特定位置的代码库。如果您配置了默认位置,则可以省略此标志以使用默认值。
  • --async 会立即返回结果,而无需等待正在进行的操作完成。

如需删除软件包的版本,请按如下所述操作:

控制台

  1. 在 Google Cloud Console 中打开代码库页面。

    打开代码库页面

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

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

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

  4. 选择您要删除的版本。

  5. 点击删除

  6. 在确认对话框中,点击删除

gcloud

运行以下命令:

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

其中

  • REPOSITORY 是代码库的名称。如果您配置了默认代码库,则可以省略此标志以使用默认值。
  • LOCATION 是单区域或多区域位置。使用此标志可查看特定位置的代码库。如果您配置了默认位置,则可以省略此标志以使用默认值。
  • --async 会立即返回结果,而无需等待正在进行的操作完成。

后续步骤