Sub topic 1
What the GITHUB_TOKEN is and how it works
Prerequisites
This chapter assumes you can read a basic GitHub Actions workflow (on, jobs, steps) and write YAML. You should know what repositories, pull requests, and issues are. You should also know that workflows can run third-party actions, which is the reason token permissions matter.
What the token is
The GITHUB_TOKEN is a credential that GitHub creates automatically for each workflow job. It lets steps call the GitHub API and push to the repository without you creating or storing a secret. Every job gets its own token.
The token authenticates as the GitHub Actions app, which is installed on your repository. The token is not tied to any user account. Actions taken with it, such as commenting on an issue, show up as github-actions[bot].
Lifecycle
GitHub creates the token when a job starts, and it stops working when the job finishes. It also expires after 24 hours at most, which is the longest a job can run. A leaked token is therefore far less dangerous than a leaked personal access token, but it is still usable while the job is running.
How steps get the token
The token is available in two ways:
- As the
secrets.GITHUB_TOKENcontext value. You can pass it to an action as an input. - As
github.token, which holds the same value and works in places wheresecretsis not available.
Some actions read it for you. For example, actions/checkout uses it as the default for its token input, so a plain checkout works without any setup.
The token is not exported as an environment variable automatically. The GitHub CLI needs it set explicitly:
name: list-issues
on: workflow_dispatch
permissions:
contents: read
issues: read
jobs:
list:
runs-on: ubuntu-latest
steps:
- run: gh issue list --repo "$GITHUB_REPOSITORY"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}GitHub masks the token value in logs, but masking only hides the exact string. Do not rely on it as protection.
What the token can do
The token can only act on the repository where the workflow runs. What it can do within that repository depends on its permissions. Each permission is a scope, such as contents or issues, with an access level of read, write, or none. Two settings decide which permissions the token starts with:
- The default workflow permissions in the repository, organization, or enterprise settings. These can be permissive or restricted.
- Any
permissionskey in the workflow file, which overrides that default.
A later chapter covers both in detail. For now, remember that a token's power is set by configuration you control. Many older repositories, and organizations created before February 2023, still default to read and write access on most scopes. Workflows there get a broad token unless they say otherwise.
Checking what a job received
GitHub prints the token's permissions at the start of each job. Open a run, select a job, and expand the Set up job step, then GITHUB_TOKEN Permissions. The listing shows each scope and its level. Check it whenever you are unsure what a job can do.
Why this matters
Every step in a job shares the same token, including steps from third-party actions. If one step is compromised, it holds everything the token holds. The next chapter looks at that risk. The rest of the tutorial shows how to shrink the token to what each job needs.
Exercise
A job finishes in ten minutes. A step printed the token to a place where it was not masked, and an attacker copies it an hour later. Can they use it? Which two facts from this chapter support your answer?
Show answer
No. The token expires when the job finishes, and in any case it expires after 24 hours at most. An hour after a ten-minute job ended, the token is already revoked. The risk exists only while the job is running, and it grows with the token's permissions.
Exercise
Where would you look to see which permissions a specific job received, and what name does the token's identity appear under in the activity it performs?
Show answer
Open the workflow run, select the job, and expand the "Set up job" step, then "GITHUB_TOKEN Permissions". Activity performed with the token appears as github-actions[bot].