Authenticating with the GITHUB_TOKEN
GitHub provides a token that you can use to authenticate on behalf of Acciones de GitHub.
Acciones de GitHub is available with GitHub gratis, GitHub Pro, Equipo de GitHub, and Nube de GitHub Enterprise. Acciones de GitHub is unavailable for per-repository plans, which are legacy billing plans. For more information, see "GitHub's products."
En este artículo
Anyone with write
access to a repository can create, read, and use secrets.
About the GITHUB_TOKEN
secret
GitHub automáticamente crea un secreto de GITHUB_TOKEN
para utilizar en tu flujo de trabajo. You can use the GITHUB_TOKEN
to authenticate in a workflow run.
When you enable Acciones de GitHub, GitHub installs a App GitHub on your repository. The GITHUB_TOKEN
secret is a App GitHub installation access token. You can use the installation access token to authenticate on behalf of the App GitHub installed on your repository. Los permisos del token están limitados al repositorio que contiene tu flujo de trabajo. 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 un token puede incluir pasar el token como entrada a una acción que lo requiere o hacer llamadas autenticadas de la API GitHub.
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 obtener información sobre los puntos finales de la API GitHub Apps a los que puedes acceder con cada permiso, consulta " Permisos App GitHub" en la documentación del programador de GitHub.
Permiso | Tipo de acceso | Acceso por repositorios bifurcados |
---|---|---|
verificaciones | lectura/escritura | lectura |
contenidos | lectura/escritura | lectura |
implementaciones | lectura/escritura | lectura |
propuestas | lectura/escritura | lectura |
metadatos | lectura | lectura |
paquetes | lectura/escritura | lectura |
solicitudes de extracción | lectura/escritura | lectura |
proyectos de repositorio | lectura/escritura | lectura |
estados | lectura/escritura | lectura |
Si necesitas un token que requiere permisos que no están disponibles en el GITHUB_TOKEN
, puedes crear un token de acceso personal y establecerlo como secreto en tu repositorio:
- Usa o crea un token con los permisos adecuados para ese repositorio. Para obtener más información, consulta "Crear un token de acceso personal para la línea de comando".
- Añade el token como un secreto en el repositorio de tu flujo de trabajo, y refiérete a él usando la sintaxis de
${{ secrets.SECRET_NAME }}
. For more information, see "Creating and using encrypted secrets."