在 CI/CD 工作流中使用私有包
See Details
目录
你可以使用访问令 牌通过持续集成 (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:
-
GitHub Actions(GitHub 托管的运行器)
¥GitHub Actions (GitHub-hosted runners)
-
GitLab CI/CD(GitLab.com 共享运行器)
¥GitLab CI/CD (GitLab.com shared runners)
如果你使用其他 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.
如果你改用旧版令牌,则默认情况下,npm token create
将生成具有读写权限的令牌。我们建议创建一个只读令牌:
¥If you use a legacy token instead, by default, npm token create
will generate a token with both read and write permissions. We recommend creating a read-only token:
npm token create --read-only
有关创建访问令牌(包括 CIDR 白名单令牌)的更多信息,请参阅“创建访问令牌”。
¥For more information on creating access tokens, including CIDR-whitelisted tokens, see "Creating an access token".
持续部署
¥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 提供商无法进行可信发布,你可以在网站上创建 自动化令牌。即使你的账户启用了双重身份验证,这也将允许你发布。
¥If trusted publishing is not available for your CI/CD provider, you may create an automation token on the website. This will allow you to publish 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 token with read and write permissions, which are granted with the standard token creation command:
npm token create
CIDR 白名单
¥CIDR whitelists
为了提高安全性,你可以使用只能在特定 IP 地址范围内使用的 CIDR 白名单令牌。你可以将 CIDR 白名单与读取 和发布令牌或只读令牌一起使用:
¥For increased security, you may use a CIDR-whitelisted token that can only be used from a certain IP address range. You can use a CIDR whitelist with a read and publish token or a read-only token:
npm token create --cidr=[list]npm token create --read-only --cidr=[list]
示例:
¥Example:
npm token create --cidr=192.0.2.0/24
欲了解更多信息,请参阅“创建和查看身份验证令牌”。
¥For more information, see "Creating and viewing authentication tokens".
将令牌设置为 CI/CD 服务器上的环境变量
¥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.
创建并签入一个特定于项目的 .npmrc 文件
¥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.