Sub topic 4
The permissions key and access levels
This chapter assumes you can read workflow YAML (on, jobs, steps) and know that GITHUB_TOKEN is the credential GitHub creates for each job. The permissions key controls what that token can do.
What the key does
permissions is a map from a scope name to an access level. A scope is an area of the GitHub API, such as contents or issues. Each scope takes one of three access levels:
| Level | Meaning |
|---|---|
| `read` | The token can read resources in that scope. |
| `write` | The token can read and modify resources in that scope. |
| `none` | The token has no access to that scope. |
write always includes read, so you never list both for one scope.
Here is a minimal workflow that uses the key:
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testcontents: read lets actions/checkout download the repository. The job cannot push commits, open issues, or publish packages, because those need other scopes or write access.
Where you can put it
You can set permissions in two places:
- At the top level of the workflow, where it applies to every job.
- Inside a single job, where it applies only to that job.
A job-level block replaces the workflow-level block for that job. It does not merge with it. A later chapter covers this in detail.
Shorthand forms
Besides the map, the key accepts two shorthand values and one empty form:
# Grants read access to every scope
permissions: read-all# Grants write access to every scope (avoid this)
permissions: write-all# Grants no access to any scope
permissions: {}read-all is convenient but broader than most jobs need. write-all gives a compromised step the widest possible access, so treat it as a smell in review. {} is the strictest option, and the next chapters build on it.
Rules that cause common errors
- Unlisted scopes become
none. Once you list any scope in a block, every scope you did not list drops tonone. Addingissues: writeto a block that has nocontentsline means checkout can no longer read the repository. - Scope names must be exact. A misspelling such as
pull-request: write(the correct name ispull-requests) makes GitHub reject the workflow file as invalid. - Shorthand cannot be mixed with a map. You write either
permissions: read-allor a map of scopes, never both. - Some scopes have fewer levels.
id-tokenaccepts onlywriteornone. - The key can narrow the token but cannot widen what GitHub allows. Fork pull requests, for example, still get a read-only token for most events.
Exercise
A job has permissions: with only issues: write listed, and its first step is actions/checkout@v4 in a private repository. What happens, and how do you fix it?
Show answer
Checkout fails because contents is no longer listed, so it is set to none. Fix it by adding contents: read alongside issues: write.