Sub topic 1
What the GITHUB_TOKEN is and how it works
Prerequisites
This chapter assumes you can:
- Read and write basic GitHub Actions workflow syntax (
on,jobs,steps). - Read YAML.
- Describe repositories, pull requests, and issues.
- Recognize that workflows can run third-party actions.
You don't need to know the permissions key yet. This chapter explains the credential that key controls.
The short version
The GITHUB_TOKEN is a credential that GitHub creates for each job in a workflow run. Steps use it to call the GitHub API as the repository itself. Checking out code, commenting on a pull request, and pushing a container image all work this way. You never create, store, or rotate it.
Three properties matter for everything later in this tutorial:
- It is created automatically at the start of each job.
- It is limited to the repository that contains the workflow.
- It stops working when the job ends.
What the token actually is
When a job starts, GitHub issues an installation access token for a built-in GitHub App called "GitHub Actions". The app is installed on your repository automatically. The token is the app's short-lived credential for that installation.
This has practical consequences:
- API calls made with the token appear as the
github-actions[bot]user. Commits, comments, and labels are attributed to that bot, not to a person. - The token carries a set of permissions, such as
contents: read. GitHub decides that set when the job starts. The rest of this tutorial covers how it is chosen. - The token is not a personal access token (PAT). It has no user behind it and cannot act outside its repository.
Lifecycle
A fresh token is created for every job. Two jobs in the same run get two different tokens, and each token carries the permissions configured for its own job.
The token expires when the job finishes. It also expires after 24 hours at most, which is the maximum job duration for self-hosted runners. After expiry, a leaked copy of the token is useless.
The short life limits the damage from a leak, but it does not remove the risk. A malicious step can use the token at full strength while the job is still running. That is why the permission set matters, and the next chapter covers it.
How steps get the token
The token is not an environment variable by default. You reference it through one of two expressions. Both give the same value:
${{ secrets.GITHUB_TOKEN }}${{ github.token }}
Many actions take a token input whose default is already ${{ github.token }}. actions/checkout is one example, so a plain checkout step needs no extra configuration.
For your own scripts, pass the token explicitly to the step that needs it:
name: list-open-prs
on: workflow_dispatch
jobs:
list:
runs-on: ubuntu-latest
steps:
- name: Show open pull requests
env:
GH_TOKEN: ${{ github.token }}
run: gh pr list --repo "$GITHUB_REPOSITORY" --state openThe gh command-line tool is preinstalled on GitHub-hosted runners. It reads GH_TOKEN from the environment. Setting the variable on one step, not on the whole job, limits which steps can see the token. The sections below explain why that habit is worth keeping.
You can also call the REST API directly:
curl --silent --fail \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues?state=open"In a workflow, set GITHUB_TOKEN: ${{ github.token }} in the step's env block first.
What the token can and cannot reach
The token is scoped to one repository: the one that holds the workflow file. Within that repository, the allowed actions depend on the granted permissions. Outside it, the token has almost nothing.
| Question | Answer |
|---|---|
| Can it read another private repository in your organization? | No. Use a GitHub App token or a PAT. |
| Can it act as a real user? | No. It acts as `github-actions[bot]`. |
| Does it work after the job ends? | No. |
| Is it the same in every job of a run? | No. Each job gets its own token. |
| Is the value hidden in logs? | Yes. GitHub masks it as `***`. |
Masking is a safety net, not a guarantee. GitHub redacts the exact token string. A step that transforms the value, for example by encoding it, can print something that is no longer masked. Treat the token as a secret and never print it.
Behaviors that surprise people
Token-triggered events do not start new runs
Events created with the GITHUB_TOKEN do not trigger new workflow runs. The exceptions are workflow_dispatch and repository_dispatch. GitHub does this to prevent accidental recursive loops.
Suppose workflow A pushes a commit using the token. A push-triggered workflow B will not run for that commit. People often hit this when a bot opens a pull request and the required checks never start. The usual fix is to authenticate with a GitHub App token or a PAT for that one action. That trade-off is covered in the last chapters.
It is bound by rate limits
The token has its own API rate limit: 1,000 requests per hour per repository. Enterprise Cloud accounts get 15,000. Scripts that loop over many issues can hit this, so batch your calls or use GraphQL.
Workflows from forks get a restricted token
For pull requests from forks, GitHub gives the token read-only access and withholds repository secrets. This protects you from arbitrary code in someone else's branch. A later chapter covers forks and Dependabot in detail.
The token does not decide what the workflow can do on its own
Permissions are one layer. Branch protection rules, required reviews, and environment protection rules still apply. A token with contents: write cannot push to a branch that requires pull requests unless the rules allow the bot to do so.
Inspecting the token in a real run
You can confirm that a job has a token and see what it can reach without printing it. This workflow calls the API and prints only the HTTP status code:
name: token-check
on: workflow_dispatch
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Read repository metadata
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
curl --silent --output /dev/null --write-out '%{http_code}\n' \
--header "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/$GITHUB_REPOSITORY"A 200 means the token is valid for this repository. Replace the URL with one for a different private repository, and you should get 404, because the token cannot see it.
Why this matters for the rest of the tutorial
The facts in this chapter set up the later material:
- The token exists in every job, whether or not you use it. Every step in the job can use it, including third-party actions.
- Its capabilities come from permissions, not from the identity of the bot. Narrowing the permissions is the main control you have.
- It is short-lived, but a running job is long enough to do harm if the permissions are broad.
Exercise
A workflow's first job uses the GITHUB_TOKEN to push a commit to a branch. A second workflow listens for push events on that branch but never runs. Why, and what is one way to fix it?
Show answer
Events created with the GITHUBTOKEN do not trigger new workflow runs (except workflowdispatch and repositorydispatch), which prevents recursive loops. To fix it, push with a GitHub App token or a personal access token instead, or have the first workflow start the second one explicitly with workflowdispatch or repository_dispatch.
Exercise
Two jobs in the same workflow run both reference ${{ secrets.GITHUB_TOKEN }}. Do they hold the same token? When does each token stop working?
Show answer
No. GitHub creates a separate token for each job. Each token stops working when its own job finishes, or after 24 hours at most.
Exercise
A teammate says, "The token is short-lived, so broad permissions are fine." Give one reason this reasoning is wrong.
Show answer
The token is valid for the whole job, and every step in the job, including third-party actions, can use it. A compromised step can do everything the permissions allow before the job ends, so the lifetime does not limit the damage within that window.