Authenticating with the GITHUB_TOKEN
GitHub provides a token that you can use to authenticate on behalf of GitHub Actions.
GitHub Actions is available with GitHub Free, GitHub Pro, GitHub Team, and GitHub Enterprise Cloud. GitHub Actions is unavailable for per-repository plans, which are legacy billing plans. For more information, see "GitHub's products."
Neste artigo
Anyone with write
access to a repository can create, read, and use secrets.
About the GITHUB_TOKEN
secret
O GitHub cria automaticamente um segredo GITHUB_TOKEN
para uso no fluxo de trabalho. You can use the GITHUB_TOKEN
to authenticate in a workflow run.
When you enable GitHub Actions, GitHub installs a aplicativo GitHub on your repository. The GITHUB_TOKEN
secret is a aplicativo GitHub installation access token. You can use the installation access token to authenticate on behalf of the aplicativo GitHub installed on your repository. As permissões do token são restritas ao repositório do fluxo de trabalho. For more information, see "Permissions for the GITHUB_TOKEN
."
The installation access token expires after 60 minutes. GitHub fetches a token for each job, before the job begins.
Note: When a workflow run or its jobs are queued for more than one hour, the token may expire before the job starts.
Using the GITHUB_TOKEN
in a workflow
To use the GITHUB_TOKEN
secret, you must reference it in your workflow file. Usar um token pode compreender disponibilizar o token como uma entrada para uma ação que o exige ou fazer solicitações de API GitHub autenticadas.
Example passing GITHUB_TOKEN
as an input
This example workflow uses the labeler action, which requires the GITHUB_TOKEN
as the value for the repo-token
input parameter:
name: Pull request labeler
on:
- pull_request
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
Example 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_commit:
runs-on: ubuntu-latest
steps:
- name: Create issue using REST API
run: |
curl --request POST \
--url https://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 }}_."
}'
Permissions for the GITHUB_TOKEN
Para obter informações sobre os pontos de extremidade de API que o Aplicativos do GitHub pode acessar em cada permissão, consulte "Permissões do aplicativo GitHub" no documentação do GitHub Developer.
Permissão | Tipo de acesso | Acesso pelos repositórios bifurcados |
---|---|---|
Verificações | leitura/gravação | leitura |
Conteúdo | leitura/gravação | leitura |
Implantações | leitura/gravação | leitura |
Problemas | leitura/gravação | leitura |
metadados | leitura | leitura |
pacotes | leitura/gravação | leitura |
Pull requests | leitura/gravação | leitura |
Projetos de repositório | leitura/gravação | leitura |
Status | leitura/gravação | leitura |
Se você precisa de um token que exige premissões que não estão disponíveis no GITHUB_TOKEN
, é possível criar um token de acesso pessoal e configurá-lo como um segredo no repositório:
- Use ou crie um token com as permissões adequadas para o repositório. Para obter mais informações, consulte "Criar um token de acesso pessoal para a linha de comando".
- Adicione o token como um segredo no repositório do fluxo de trabalho e refira-se a ele usando a sintaxe
${{ secrets.SECRET_NAME }}
. For more information, see "Creating and using encrypted secrets."