Using the GITHUB_TOKEN
in a workflow
You can use the GITHUB_TOKEN
by using the standard syntax for referencing secrets: ${{ secrets.GITHUB_TOKEN }}
. Examples of using the GITHUB_TOKEN
include passing the token as an input to an action, or using it to make an authenticated GitHub API request.
重要
An action can access the GITHUB_TOKEN
through the github.token
context even if the workflow does not explicitly pass the GITHUB_TOKEN
to the action. As a good security practice, you should always make sure that actions only have the minimum access they require by limiting the permissions granted to the GITHUB_TOKEN
. For more information, see GITHUB_TOKEN reference.
Example 1: passing the GITHUB_TOKEN
as an input
此示例工作流程使用 GitHub CLI,该方式需要 GITHUB_TOKEN
作为 GH_TOKEN
输入参数的值:
name: Open new issue on: workflow_dispatch jobs: open-issue: runs-on: ubuntu-latest permissions: contents: read issues: write steps: - run: | gh issue --repo ${{ github.repository }} \ create --title "Issue title" --body "Issue body" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
name: Open new issue
on: workflow_dispatch
jobs:
open-issue:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- run: |
gh issue --repo ${{ github.repository }} \
create --title "Issue title" --body "Issue body"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Example 2: calling the REST API
You can use the GITHUB_TOKEN
to make authenticated API calls. This example workflow creates an issue using the GitHub REST API:
name: Create issue on commit
on: [ push ]
jobs:
create_issue:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Create issue using REST API
run: |
curl --request POST \
--url http://api.github.com/repos/${{ github.repository }}/issues \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"title": "Automated issue for commit: ${{ github.sha }}",
"body": "This issue was automatically created by the GitHub Action workflow **${{ github.workflow }}**. \n\n The commit hash was: _${{ github.sha }}_."
}' \
--fail
Modifying the permissions for the GITHUB_TOKEN
You can modify the permissions for the GITHUB_TOKEN
in individual workflow files. If the default permissions for the GITHUB_TOKEN
are restrictive, you may have to elevate the permissions to allow some actions and commands to run successfully. If the default permissions are permissive, you can edit the workflow file to remove some permissions from the GITHUB_TOKEN
. As a good security practice, you should grant the GITHUB_TOKEN
the least required access.
You can see the permissions that GITHUB_TOKEN
had for a specific job in the "Set up job" section of the workflow run log. For more information, see 使用工作流运行日志.
You can use the permissions
key in your workflow file to modify permissions for the GITHUB_TOKEN
for an entire workflow or for individual jobs. This allows you to configure the minimum required permissions for a workflow or job.
可以使用 permissions
密钥添加和删除分叉存储库的读取权限,但通常不能授予其写入权限。 此行为的例外情况是,管理员用户已在 GitHub Actions 设置中选择了“通过拉取请求向工作流发送写入令牌”选项。 有关详细信息,请参阅“管理存储库的 GitHub Actions 设置”。
The two workflow examples earlier in this article show the permissions
key being used at the job level, as it is best practice to limit the permissions' scope.
For full details of the permissions
key, see GitHub Actions 的工作流语法.
注意
Organization and enterprise owners can prevent you from granting write access to the GITHUB_TOKEN
at the repository level. For more information, see 禁用或限制组织的 GitHub Actions and 在企业中为 GitHub Actions 实施策略.
When the permissions
key is used, all unspecified permissions are set to no access, with the exception of the metadata
scope, which always gets read access.
Granting additional permissions
If you need a token that requires permissions that aren't available in the GITHUB_TOKEN
, you can create a GitHub App and generate an installation access token within your workflow. For more information, see 使用 GitHub Actions 工作流中的 GitHub App 发出经过身份验证的 API 请求. Alternatively, you can create a personal access token, store it as a secret in your repository, and use the token in your workflow with the ${{ secrets.SECRET_NAME }}
syntax. For more information, see 管理个人访问令牌 and 在 GitHub Actions 中使用机密.
Configuring GITHUB_TOKEN
permissions with private repositories
Private repositories can control whether pull requests from forks can run workflows, and can configure the permissions assigned to GITHUB_TOKEN
. For more information, see 管理存储库的 GitHub Actions 设置.