npm-trust

管理软件包与 CI/CD 提供商之间的可信发布关系

选择命令行版本:

目录

概要

🌐 Synopsis

注意:此命令不支持工作区。

🌐 Note: This command is unaware of workspaces.

先决条件

🌐 Prerequisites

在使用 npm trust 命令之前,请确保满足以下要求:

🌐 Before using npm trust commands, ensure the following requirements are met:

  • npm 版本:需要 npm@11.10.0 或以上版本。如有需要,请使用 npm install -g npm@^11.10.0 更新。
  • 包上的写入权限:你必须对正在配置的包具有写入权限。
  • 账户已启用双重验证:必须在账户级别启用双重验证。即使当前未启用,也必须启用它才能使用信任命令。
  • 支持的认证方法:不支持带有绕过双因素身份验证选项的细粒度访问令牌(GAT)。传统的基本认证(用户名和密码)凭据无法用于信任命令或端点。
  • 软件包必须存在:你正在配置的软件包必须已经存在于 npm 注册表中。

描述

🌐 Description

使用 OpenID Connect (OIDC) 配置 npm 包与 CI/CD 提供商之间的信任关系。这是管理 npm 网站上受信任发布者配置的命令行等效操作。

🌐 Configure trust relationships between npm packages and CI/CD providers using OpenID Connect (OIDC). This is the command-line equivalent of managing trusted publisher configurations on the npm website.

有关受信任发布者的全面概述,请参阅 npm 受信任发布者文档

🌐 For a comprehensive overview of trusted publishing, see the npm trusted publishers documentation.

[package] 参数指定包名称。如果省略,npm 将使用当前目录中 package.json 的名称。

🌐 The [package] argument specifies the package name. If omitted, npm will use the name from the package.json in the current directory.

每个信任关系都有自己的一套配置选项和标志,这些选项和标志基于该提供商提供的 OIDC 声明。OIDC 声明来自 CI/CD 提供商,并包含如存储库名称、工作流文件或环境等信息。由于每个提供商的声明不同,可用的标志和配置键并非通用——npm 会匹配每个提供商 OIDC 配置中支持的声明。有关特定提供商支持哪些声明和标志的详细信息,请使用 npm trust <provider> --help

所需的选项取决于你正在配置的 CI/CD 提供程序。有关每个选项的详细信息,请参阅 npm 文档中的管理受信任发布者配置部分。如果提供程序是基于存储库的且未提供该选项,npm 将使用你 package.json 中的 repository.url 字段(如果可用)。

🌐 The required options depend on the CI/CD provider you're configuring. Detailed information about each option is available in the managing trusted publisher configurations section of the npm documentation. If a provider is repository-based and the option is not provided, npm will use the repository.url field from your package.json, if available.

目前,注册表每个软件包只支持一个配置。如果你在已有信任关系时尝试创建新的信任关系,将会导致错误。要替换现有配置:

🌐 Currently, the registry only supports one configuration per package. If you attempt to create a new trust relationship when one already exists, it will result in an error. To replace an existing configuration:

  1. 使用 npm trust list [package] 查看现有受信任发布者的 ID
  2. 使用 npm trust revoke --id <id> [package] 删除现有的配置
  3. 然后创建你的新信任关系

批量使用

🌐 Bulk Usage

对于管理大量软件包的维护者,你可以使用 bash 脚本批量配置受信任的发布。创建一个循环,遍历软件包名称及其对应的配置详情,对每个软件包执行带有 --yes 标志的 npm trust <provider> 命令。

🌐 For maintainers managing a large number of packages, you can configure trusted publishing in bulk using bash scripting. Create a loop that iterates through package names and their corresponding configuration details, executing the npm trust <provider> command with the --yes flag for each package.

第一个请求将需要两因素认证。在进行两因素认证时,你将在 npm 网站上看到一个选项,可以跳过接下来的 5 分钟的两因素认证。启用此选项将允许后续的 npm trust <provider> 命令在无需两因素认证的情况下继续,从而简化批量配置过程。

🌐 The first request will require two-factor authentication. During two-factor authentication, you'll see an option on the npm website to skip two-factor authentication for the next 5 minutes. Enabling this option will allow subsequent npm trust <provider> commands to proceed without two-factor authentication, streamlining the bulk configuration process.

我们建议在每次调用之间添加 2 秒的延迟,以避免速率限制。采用这种方法,你可以在 5 分钟的双因素认证跳过窗口内配置大约 80 个软件包。

🌐 We recommend adding a 2-second sleep between each call to avoid rate limiting. With this approach, you can configure approximately 80 packages within the 5-minute two-factor authentication skip window.

配置

🌐 Configuration

npm trust github

在软件包与 GitHub Actions 之间建立可信关系

🌐 Create a trusted relationship between a package and GitHub Actions

概要

🌐 Synopsis

npm trust github [package] --file [--repo|--repository] [--env|--environment] [-y|--yes]

标志

🌐 Flags

标志默认类型描述
--filenull字符串(必填)仓库 .GitHub 文件夹内工作流文件的名称(必须以 yaml 或 yml 结尾)
--repository, --reponull字符串仓库的名称,格式为 owner/repo
--environment--envnull字符串CI 环境名称
--dry-runfalse布尔值表示你不希望 npm 做任何更改,它只应报告它本会做的事情。这个参数可以传递给任何修改本地安装的命令,例如 installupdatededupeuninstall,以及 packpublish。注意:其他与网络相关的命令(例如 dist-tagsowner 等)不会遵守此设置。
--jsonfalse布尔值是否输出 JSON 数据,而不是正常输出。* 在 npm pkg set 中,它允许在将值保存到 package.json 之前使用 JSON.parse() 解析设置的值。并非所有 npm 命令都支持。
--registry"https://registry.npmjs.org/"URLnpm 注册表的基础 URL。
--yes-ynullnull 或 Boolean自动对 npm 可能在命令行上打印的任何提示回答“是”。

npm trust gitlab

在软件包与 GitLab CI/CD 之间建立可信关系

🌐 Create a trusted relationship between a package and GitLab CI/CD

概要

🌐 Synopsis

npm trust gitlab [package] --file [--project|--repo|--repository] [--env|--environment] [-y|--yes]

标志

🌐 Flags

标志默认类型描述
--filenull字符串(必填)流水线文件的名称(例如 .gitlab-ci.yml)
--projectnull字符串项目的名称,格式为 group/project 或 group/subgroup/project
--environment--envnull字符串CI 环境名称
--dry-runfalse布尔值表示你不希望 npm 做任何更改,它只应报告它本会做的事情。这个参数可以传递给任何修改本地安装的命令,例如 installupdatededupeuninstall,以及 packpublish。注意:其他与网络相关的命令(例如 dist-tagsowner 等)不会遵守此设置。
--jsonfalse布尔值是否输出 JSON 数据,而不是正常输出。* 在 npm pkg set 中,它允许在将值保存到 package.json 之前使用 JSON.parse() 解析设置的值。并非所有 npm 命令都支持。
--registry"https://registry.npmjs.org/"URLnpm 注册表的基础 URL。
--yes-ynullnull 或 Boolean自动对 npm 可能在命令行上打印的任何提示回答“是”。

npm trust circleci

在软件包和CircleCI之间建立可信关系

🌐 Create a trusted relationship between a package and CircleCI

概要

🌐 Synopsis

npm trust circleci [package] --org-id <uuid> --project-id <uuid> --pipeline-definition-id <uuid> --vcs-origin <origin> [--context-id <uuid>...] [-y|--yes]

标志

🌐 Flags

标志默认类型描述
--org-idnull字符串(必填)CircleCI 组织 UUID
--project-idnull字符串(必填)CircleCI 项目 UUID
--pipeline-definition-id字符串(必填)CircleCI 流水线定义 UUID
--vcs-originnull字符串(必填)CircleCI 仓库来源,格式为 'provider/owner/repo'
--context-idnullnull 或字符串(可以多次设置)要匹配的 CircleCI 上下文 UUID
--dry-runfalse布尔值表示你不希望 npm 做任何更改,它只应报告它本会做的事情。这个参数可以传递给任何修改本地安装的命令,例如 installupdatededupeuninstall,以及 packpublish。注意:其他与网络相关的命令(例如 dist-tagsowner 等)不会遵守此设置。
--jsonfalse布尔值是否输出 JSON 数据,而不是正常输出。* 在 npm pkg set 中,它允许在将值保存到 package.json 之前使用 JSON.parse() 解析设置的值。并非所有 npm 命令都支持。
--registry"https://registry.npmjs.org/"URLnpm 注册表的基础 URL。
--yes-ynullnull 或 Boolean自动对 npm 可能在命令行上打印的任何提示回答“是”。

npm trust list

列出软件包的受信任关系

🌐 List trusted relationships for a package

概要

🌐 Synopsis

npm trust list [package]

标志

🌐 Flags

标志默认值类型描述
--jsonfalse布尔值是否输出 JSON 数据,而不是正常的输出。* 在 npm pkg set 中,它允许在保存到你的 package.json 之前用 JSON.parse() 解析设置的值。并非所有 npm 命令都支持。
--registry"https://registry.npmjs.org/"URLnpm 注册表的基础 URL。

npm trust revoke

撤销包的受信任关系

🌐 Revoke a trusted relationship for a package

概要

🌐 Synopsis

npm trust revoke [package] --id=<trust-id>

标志

🌐 Flags

标志默认值类型描述
--idnull字符串(必填)要撤销的受信任关系的 ID
--dry-runfalse布尔值表示你不希望 npm 做任何更改,只希望它报告将会做的操作。这可以传递给修改本地安装的任何命令,例如 installupdatededupeuninstall,以及 packpublish。注意:其他与网络相关的命令(例如 dist-tagsowner 等)不会遵守此选项。
--registry"https://registry.npmjs.org/"URLnpm 注册表的基础 URL。

也可以看看

🌐 See Also

目录