Sub topic 12
Setting minimal GITHUB_TOKEN permissions
This chapter assumes you can read a workflow file, know what a job and a step are, and have seen the uses: syntax for actions. You do not need to have published check runs or pull request comments yet. This chapter prepares you for that.
What the token is
Every workflow run gets a GITHUB_TOKEN. It is a short-lived credential that GitHub creates when a job starts and revokes when the job ends. Actions and gh commands use it to call the GitHub API on behalf of the workflow.
What the token may do depends on its permissions. If a step needs more than the token has, the API call fails with a "Resource not accessible by integration" error (HTTP 403).
Plain test runs need very little. Checking out code reads the repository. Uploading artifacts and writing GITHUB_STEP_SUMMARY use the runner's own services, not the token. Permissions start to matter when a step posts to the GitHub API, for example to create a check run or a pull request comment.
Why restrict it
A repository or organization may give the token broad write access by default. A compromised or buggy third-party action in your job can then use that access. Granting only what the job needs limits the damage.
The permissions: key sets this. When you set it, every permission you do not list becomes none.
Setting permissions
Put permissions: at the top of the workflow to apply it to every job. Put it inside a job to apply it to that job only. A job-level block replaces the workflow-level one for that job. It does not merge with it.
name: tests
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Tests run here"This is the baseline used in the earlier examples. contents: read is enough for actions/checkout.
Permissions for reporting
Choose permissions by what the publishing step does.
| What the step does | Permission needed |
|---|---|
| Check out the repository | `contents: read` |
| Create a check run with test results | `checks: write` |
| Comment on a pull request | `pull-requests: write` |
| Read workflow run data and artifacts through the API | `actions: read` |
Give the extra permission to the job that publishes, not to the whole workflow. Here the test job stays read-only and a separate job gets the write access.
name: tests
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests and upload reports here"
publish:
needs: test
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
pull-requests: write
steps:
- run: echo "Publish results here"The publish block lists contents: read again. Because a job-level block replaces the workflow-level one, leaving it out would set contents to none for that job.
Common errors
- 403 "Resource not accessible by integration". The job lacks a permission the action needs. Check the action's documentation for its required scopes and add them to that job.
- Checkout fails after adding
permissions:. You listed other permissions but notcontents: read. Unlisted permissions arenone. - Write permissions have no effect on pull requests from forks. For those runs, GitHub gives the token read-only access no matter what you request. The fork chapter covers the approach for that case.
Exercise
A workflow sets permissions: contents: read at the top. One job adds its own block with only checks: write. Its checkout step now fails. Why, and how do you fix it?
Show answer
A job-level permissions: block replaces the workflow-level one, and unlisted permissions become none. The job lost contents: read. Fix it by listing both contents: read and checks: write in the job's block.