如何在 Github 操作中设置秘密?

How to set secrets in Github Actions?

官方样板代码注入npm令牌如下

NODE_AUTH_TOKEN: ${{secrets.npm_token}}

如何访问和设置这个变量?我在 GUI 中找不到它。

  1. 转到您在 Github
  2. 中的项目
  3. Select Settings 选项卡
  4. 单击左侧菜单中的 Secrets 部分
  5. Add a new secret 并提供一个名称(例如 npm_token)和一个值。

这个页面很难找到,但它存在于这里的官方文档中:Creating and using secrets (encrypted variables)

为方便起见,从以下文档复制:

Secret names cannot include any spaces. To ensure that GitHub redacts your secret in logs, avoid using structured data as the values of secrets, like JSON or encoded Git blobs.

  1. On GitHub, navigate to the main page of the repository.
  2. Under your repository name, click Settings.
  3. In the left sidebar, click Secrets.
  4. Type a name for your secret in the "Name" input box.
  5. Type the value for your secret.
  6. Click Add secret.

上面的 link 也有更多关于使用秘密的信息。

除了 GUI,您现在(2020 年 1 月)还有 GitHub Actions API(!, still beta though), as announced here

它确实包括 GitHub Actions Secrets API:

Create or update an repository secret:

Creates or updates an organization secret with an encrypted value. Encrypt your secret using LibSodium.

You must authenticate using an access token with the admin:repo scope to use this endpoint.
GitHub Apps must have the secrets organization permission to use this endpoint.

PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}

Get a repository secret

Gets a single secret without revealing its encrypted value.
Anyone with write access to the repository can use this endpoint.
GitHub Apps must have the secrets permission to use this endpoint.

GET /repos/:owner/:repo/actions/secrets/:name

因此 GUI 不再是唯一的选择:您可以通过这个新的 API.

编写脚本和 get/set 一个 Actions 秘密

我创建了一个简单的 CLI,可以帮助您实现这一目标 - https://github.com/unfor19/githubsecrets

本CLI基于官方API。您可以使用 pipDocker 安装它,阅读 README.md 了解更多信息