简体   繁体   English

如何在 Github Actions 中设置秘密?

[英]How to set secrets in Github Actions?

The official boilerplate code injects the npm token as follows官方样板代码注入npm token如下

NODE_AUTH_TOKEN: ${{secrets.npm_token}}

How do I access and set this variable?如何访问和设置此变量? I cant find it in the GUI.我在 GUI 中找不到它。

  1. Go to your project in Github转到您在 Github 中的项目
  2. Select the Settings tab选择Settings选项卡
  3. Click the Secrets section in the left hand menu单击左侧菜单中的Secrets部分
  4. Add a new secret and provide a name (eg npm_token ) and a value. Add a new secret并提供一个名称(例如npm_token )和一个值。

如何添加秘密

In addition to the GUI, you now (January 2020) have a GitHub Actions API (!, still beta though), as announced here .除了 GUI 之外,您现在(2020 年 1 月)还有一个GitHub Actions API (!,但仍处于测试阶段),如此处所宣布的

And it does include a GitHub Actions Secrets API :它确实包含一个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.使用 LibSodium 加密您的秘密。

You must authenticate using an access token with the admin:repo scope to use this endpoint.您必须使用具有admin:repo范围的访问令牌进行身份验证才能使用此端点。
GitHub Apps must have the secrets organization permission to use this endpoint. GitHub Apps 必须具有secrets组织权限才能使用此端点。

 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. GitHub 应用程序必须具有使用此端点的机密权限。

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

So the GUI is no longer the sole option: you can script and get/set an Actions secret through this new API.因此 GUI 不再是唯一的选择:您可以通过这个新 API 编写脚本并获取/设置 Actions 秘密。

This page is hard to find, but it exists in the official docs here: Creating and using secrets (encrypted variables) .这个页面很难找到,但它存在于官方文档中: Creating and using secrets (encrypted variables)

Copied from the docs below for convenience:为方便起见,从以下文档中复制:

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.为确保 GitHub 在日志中编辑您的机密,请避免使用结构化数据作为机密的值,例如 JSON 或编码的 Git blob。

  1. On GitHub, navigate to the main page of the repository.在 GitHub 上,导航到存储库的主页。
  2. Under your repository name, click Settings.在您的存储库名称下,单击设置。 存储库设置按钮
  3. In the left sidebar, click Secrets.在左侧边栏中,单击 Secrets。
  4. Type a name for your secret in the "Name" input box.在“名称”输入框中为您的机密输入名称。
  5. Type the value for your secret.键入您的机密的值。
  6. Click Add secret.单击添加机密。

The link above has a bit more info around using secrets as well.上面的链接还有更多关于使用秘密的信息。

I've created a simple CLI that can help you achieve that - https://github.com/unfor19/githubsecrets我创建了一个简单的 CLI 可以帮助您实现这一目标 - https://github.com/unfor19/githubsecrets

This CLI is based on the official API.此 CLI 基于官方 API。 You can install it with pip or use Docker , read the README.md for more information您可以使用pip或使用Docker安装它,阅读README.md以获取更多信息

使用演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM