Sub topic 5
Permission scopes you will use most
This chapter assumes you can read workflow YAML and know that the permissions key sets what the GITHUB_TOKEN may do. Here you learn which scopes appear most often and what each one allows.
How scopes work
Each scope names one area of the GitHub API, such as repository contents or pull requests. Each takes one of three levels: read, write, or none. write includes read. If you list any scope in a permissions block, every scope you leave out is set to none.
The scopes you will meet most
| Scope | `read` allows | `write` adds |
|---|---|---|
| `contents` | Checking out code, reading releases | Pushing commits and tags, creating releases |
| `pull-requests` | Reading pull requests and their comments | Commenting, labeling, requesting reviewers |
| `issues` | Reading issues | Creating, labeling, and commenting on issues |
| `packages` | Pulling from GitHub Packages | Publishing to GitHub Container Registry and other registries |
| `checks` | Reading check runs | Creating and updating check runs |
| `statuses` | Reading commit statuses | Setting commit statuses |
| `actions` | Reading workflow runs and artifacts | Cancelling runs, deleting artifacts |
| `security-events` | Reading code scanning alerts | Uploading code scanning results (SARIF) |
| `id-token` | Not applicable | Requesting an OIDC token (covered in a later chapter) |
Other scopes exist, including deployments, pages, and discussions. Add them only when a job calls the matching API.
Details that cause confusion
contents: readis the baseline. Most jobs need it only soactions/checkoutcan clone the repository. Once you set any scope, you must listcontents: readyourself or checkout fails on private repositories.- Pull request comments can need
issues. GitHub treats every pull request as an issue, so the API call that adds a plain comment may requireissues: write. Review comments and labels on the diff usepull-requests: write. If one scope gives a 403 error, check the endpoint's documentation for the scope it expects. contents: writeis broad. It lets a job push to branches, which can include a workflow file change if no other rule blocks it. Grant it only to release or commit jobs.statusesandchecksare separate. Commit statuses are the older API. Check runs are the newer one. A tool that reports results uses one or the other.
Example: a scanning job
This job runs a code scanner and uploads its results. It asks for security-events: write and nothing more, plus the read access needed to check out code.
name: scan
on: pull_request
permissions: {}
jobs:
analyze:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write
steps:
- uses: actions/checkout@v4
- uses: github/codeql-action/init@v3
with:
languages: javascript
- uses: github/codeql-action/analyze@v3Private repositories may also need actions: read for this action. If the job fails with a permissions error, the log names the missing scope.
Exercise
A workflow sets permissions: { contents: read } and adds a step that applies a label to a pull request with the labeler action. The step fails with a 403 error. Which scope do you add, at what level, and why does the job fail without it?
Show answer
Add pull-requests: write. Once a permissions block exists, every unlisted scope is none. With contents: read alone, the token has no access to pull requests, so labeling is rejected. Add the scope to the job that labels, not necessarily the whole workflow.