你可以使用访问令牌通过持续集成 (CI) 系统测试私有 npm 包,或使用持续部署 (CD) 系统部署它们。
🌐 You can use access tokens to test private npm packages with continuous integration (CI) systems, or deploy them using continuous deployment (CD) systems.
🌐 Recommended: Use trusted publishing for package publishing
对于从 CI/CD 工作流发布软件包,我们建议使用 受信任的发布 而不是访问令牌。受信任的发布使用 OpenID Connect (OIDC) 提供安全的发布,从而消除了与长期存在的令牌相关的安全风险。
🌐 For publishing packages from CI/CD workflows, we recommend using trusted publishing instead of access tokens. Trusted publishing uses OpenID Connect (OIDC) to provide secure publishing that eliminates the security risks associated with long-lived tokens.
可信发布支持以下版本:
🌐 Trusted publishing is supported for:
如果你使用其他 CI/CD 提供商,或者你需要安装私有软件包(而不是发布),则可以按照如下所述使用访问令牌。
🌐 If you use a different CI/CD provider, or if you need to install private packages (not publish), you can use access tokens as described below.
🌐 Create a new access token
创建一个仅用于从 CI/CD 服务器访问 npm 包的新访问令牌。
🌐 Create a new access token that will be used only to access npm packages from a CI/CD server.
🌐 Continuous integration
在生成用于持续集成环境的访问令牌时,我们建议使用具有有限访问权限的粒度访问令牌以提供更高的安全性。
🌐 When generating an access token for use in a continuous integration environment, we recommend using a granular access token with limited access to provide greater security.
对于大多数只安装依赖并运行测试的 CI 工作流来说,只读的细粒度访问令牌是足够的,也是最安全的。
🌐 For most CI workflows that only install dependencies and run tests, a read-only granular access token is sufficient and most secure.
注意: 如果你的 CI 工作流需要写操作(例如发布测试包),你可能需要使用具有读写权限且启用绕过双重认证(2FA)的细粒度访问令牌,以防止自动化工作流被 2FA 提示阻塞。然而,我们强烈建议尽可能使用只读令牌,并仅在部署工作流中使用绕过 2FA 的功能。
警告: 旧版访问令牌将于 2025 年 11 月起被移除。
有关创建细粒度访问令牌(包括 CIDR 白名单令牌)的更多信息,请参见“创建和查看访问令牌”。
🌐 For more information on creating granular access tokens, including CIDR-whitelisted tokens, see "Creating and viewing access tokens".
🌐 Continuous deployment
在持续部署环境中发布软件包时,我们强烈建议在可用时使用受信任的发布,因为它提供了增强的安全性,同时无需管理令牌。
🌐 For publishing packages in continuous deployment environments, we strongly recommend using trusted publishing when available, as it provides enhanced security without requiring token management.
如果你的 CI/CD 提供商无法使用受信任的发布,你必须在网站上创建一个 启用绕过双重认证的细粒度访问令牌。即使你的账户启用了两步验证,这也将允许你在 CI/CD 工作流中进行发布。
🌐 If trusted publishing is not available for your CI/CD provider, you must create a granular access token with bypass 2FA enabled on the website. This will allow you to publish in your CI/CD workflows even if you have two-factor authentication enabled on your account.
绕过双因素认证的安全考虑事项:
🌐 Interactive workflows
如果你的工作流程会生成一个包,但你是在验证后手动发布它,那么你需要创建一个具有读写权限的细粒度访问令牌。有关说明,请参见“创建和查看访问令牌”。
🌐 If your workflow produces a package, but you publish it manually after validation, then you will want to create a granular access token with read and write permissions. See "Creating and viewing access tokens" for instructions.
🌐 CIDR whitelists
为了提高安全性,你可以使用仅允许从特定 IP 地址范围使用的 CIDR 白名单细化访问令牌。你可以在网站上创建细化访问令牌时配置 IP 地址限制。
🌐 For increased security, you may use a CIDR-whitelisted granular access token that can only be used from a certain IP address range. You can configure IP address restrictions when creating your granular access token on the website.
有关更多信息,请参见“创建和查看访问令牌”。
🌐 For more information, see "Creating and viewing access tokens".
🌐 Set the token as an environment variable on the CI/CD server
在 CI/CD 服务器中将你的令牌设置为环境变量或密钥。
🌐 Set your token as an environment variable, or a secret, in your CI/CD server.
例如,在 GitHub Actions 中,你可以将你的令牌添加为密钥。然后,你可以将该密钥提供给工作流使用。
🌐 For example, in GitHub Actions, you would add your token as a secret. Then you can make the secret available to workflows.
如果你将密钥命名为 NPM_TOKEN,那么你会希望从该密钥创建一个名为 NPM_TOKEN 的环境变量。
🌐 If you named the secret NPM_TOKEN, then you would want to create an environment variable named NPM_TOKEN from that secret.
steps:- run: |npm install- env:NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
有关更多详细信息,请参阅 CI/CD 服务器的文档。
🌐 Consult your CI/CD server's documentation for more details.
🌐 Create and check in a project-specific .npmrc file
使用特定于项目的 .npmrc 文件,并在其中使用变量来存储你的令牌,以安全地通过 npm 对 CI/CD 服务器进行身份验证。
🌐 Use a project-specific .npmrc file with a variable for your token to securely authenticate your CI/CD server with npm.
在项目的根目录中,创建一个自定义的 .npmrc 文件,内容如下:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
注意: 你正在指定一个字面值 ${NPM_TOKEN}。npm CLI 会将此值替换为 NPM_TOKEN 环境变量的内容。请 不要 在此文件中放置令牌。
检查 .npmrc 文件。
🌐 Securing your token
你的令牌可能有权限读取私有包、代表你发布新包或更改用户或包设置。请保护好你的令牌。
🌐 Your token may have permission to read private packages, publish new packages on your behalf, or change user or package settings. Protect your token.
不要将你的令牌添加到版本控制或不安全地存储它。请将其存储在密码管理器、云提供商的安全存储或者你的 CI/CD 提供商的安全存储中。
🌐 Do not add your token to version control or store it insecurely. Store it in a password manager, your cloud provider's secure storage, or your CI/CD provider's secure storage.
在可能的情况下,请使用具有最少必要权限的细粒度访问令牌,并为你的令牌设置较短的过期时间。有关更多信息,请参见“关于访问令牌”。
🌐 When possible, use granular access tokens with the minimum permissions necessary, and set short expiration dates for your tokens. For more information, see "About access tokens."