Sub topic 8
Finding the minimal set for a job
This chapter assumes you can read a workflow file and know that permissions can be set at workflow level and job level. It also assumes you have set the repository default to read-only. The question here is how to work out which scopes a given job needs, instead of guessing or copying write-all.
Start from the job's actual API calls
The GITHUB_TOKEN is only used when something calls the GitHub API or pushes over git with it. Finding the minimal set therefore means listing every step that talks to GitHub with the token, and mapping each call to a scope.
Three kinds of steps use the token:
- Actions that call the API on your behalf, such as
actions/labeleroractions/stale. Their README oraction.ymlusually states the permissions they need. - Commands in
run:steps that usegh,curl, orgit pushwith${{ github.token }}or${{ secrets.GITHUB_TOKEN }}. - Actions that log in or upload somewhere with the token, such as
docker/login-actionfor GitHub Container Registry.
Steps that never touch GitHub's API, such as make test or npm ci, need no scope. Neither do actions/cache and actions/upload-artifact, because they authenticate with a separate runtime credential.
A lookup table for common steps
| Step or call | Scope needed |
|---|---|
| `actions/checkout` on a private repository | `contents: read` |
| `git push` of a commit or tag, `gh release create` | `contents: write` |
| Label or comment on a pull request (`actions/labeler`, `gh pr comment`) | `pull-requests: write` |
| Create or comment on an issue (`gh issue create`) | `issues: write` |
| Close stale issues and pull requests (`actions/stale`) | `issues: write`, `pull-requests: write` |
| Push an image to `ghcr.io` | `packages: write` |
| Create a check run | `checks: write` |
| Set a commit status | `statuses: write` |
| Upload code scanning results | `security-events: write` |
| Request an OIDC token | `id-token: write` |
| Deploy to GitHub Pages | `pages: write`, `id-token: write` |
Two rules make the table work in practice. First, a write level includes read for the same scope. Second, once you declare any scope in a permissions block, every scope you don't list becomes none. The one exception is metadata: read, which is always available. So contents: read also means no pull-requests access. That is the behavior you want, but it surprises people who add one scope and find another step failing.
Public and private repositories differ. On a public repository, actions/checkout can clone without contents: read. On a private repository it cannot. Listing contents: read explicitly keeps the workflow correct if the repository's visibility changes.
The iterative method
Reading documentation gives you a first guess. Running the job confirms it. The loop looks like this:
The steps in detail:
- Begin with
permissions: {}at workflow level, and give the jobcontents: readif it checks out code. - Run the job and read the first failure. Add only the scope that the failing call needs. The next chapter covers how to read these errors.
- Re-run. Each run reveals at most the next failing call, so expect several iterations for a job with many API interactions.
- When the job passes, try removing each scope you added by documentation guess alone. If the job still passes, the scope was unnecessary.
Test on a branch or with workflow_dispatch where you can. A step that only runs on tags, or on a schedule, will not fail until that trigger fires. Cover those paths before you trust the result.
Two sources of evidence in the run itself
The "Set up job" log
Open any run, expand the Set up job step, and look under GITHUB_TOKEN Permissions. It lists the effective permission for each scope after your workflow and job settings, the repository default, and any event-specific restrictions have been applied. This is where you confirm that a fork pull request or a Dependabot run got less than you declared.
The accepted-permissions header
Many REST API responses include an X-Accepted-GitHub-Permissions header. It lists the permissions that the endpoint accepts. When you don't know which scope an endpoint wants, call it and read the header. A probe step you can run temporarily:
name: probe-permissions
on: workflow_dispatch
permissions: {}
jobs:
probe:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- name: Show accepted permissions for the issues endpoint
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api -i "repos/${GITHUB_REPOSITORY}/issues?per_page=1" \
| grep -i -E '^(HTTP/|x-accepted-github-permissions)'The header is not present on every endpoint. When it is missing, fall back to the API reference for that endpoint, which lists the fine-grained permission it requires. Remove the probe job once you have your answer.
A worked example
Suppose a workflow runs tests and then comments the result on the pull request:
name: test-and-comment
on: pull_request
permissions: {}
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
- run: make test
- name: Comment on the pull request
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: gh pr comment "$PR_NUMBER" --body "Tests passed."Walking the steps gives this result:
actions/checkoutreads the repository, so it needscontents: read.make testis a local command and needs nothing.gh pr commentwrites to a pull request, so it needspull-requests: write.
No step needs issues, packages, or checks, so the job gets none of them. If a compromised dependency in make test tried to push code or delete a package, the token would refuse.
Splitting the job is often better than one broad block. If tests run untrusted code and only the comment needs write access, put the comment in a second job with needs: test. Then the job that runs your build tooling holds only contents: read.
Edge cases that change the answer
- Job-level replaces workflow-level. A job's
permissionsblock does not merge with the workflow-level one. If the workflow grantscontents: readand the job declares onlypull-requests: write, the job losescontents. List everything the job needs. - Fork pull requests are read-only. For
pull_requestevents from forks, write scopes you declare are reduced to read, so a comment step will fail there. Dependabot-triggered runs also receive a restricted token by default. A later chapter covers both. - Different triggers, different needs. A step guarded by
if: github.event_name == 'push'might never run on pull requests. Check each trigger path, not only the one you tested. - Third-party actions can ask for more than they use. An action's README may say
contents: write"for convenience" when your configuration only needs read. Prune by testing rather than trusting. - Scopes outside the table.
actions,deployments,discussions,attestations, and others exist. The same method applies: find the failing call, then find the scope that endpoint accepts.
Summary of the method
Map each token-using step to a scope using documentation and the endpoint's accepted-permissions header. Start from nothing and add one scope per observed failure. Then prune scopes that turn out to be unnecessary. Verify the result in the Set up job log, and re-test every trigger path.
Exercise
A workflow has a single job with permissions: { contents: read }. It runs npm test, then uses actions/stale@v9 to close inactive issues and pull requests. The stale step fails with "Resource not accessible by integration". Which scopes should you add, and where, and why does contents: read not cover the stale step?
Show answer
Add issues: write and pull-requests: write to the job that runs actions/stale, keeping contents: read only if a step in that job needs to check out code. contents governs repository files, commits, and releases, not issues or pull requests. Because the job declares a permissions block, every unlisted scope is none, so the stale action's calls to the issues and pull requests APIs are rejected. Better still, move the stale step to its own job (or workflow) with just those two write scopes, so the npm test job keeps read-only access.
Exercise
You add pull-requests: write to a job but leave out contents. The job's first step is actions/checkout@v4 on a private repository, and it now fails. Why did adding one scope break checkout?
Show answer
Declaring any scope in a permissions block sets every scope you don't list to none. Before the change, the job inherited the workflow or repository default, which included contents: read. After the change, contents is none, so checkout cannot read the private repository. The fix is to list contents: read alongside pull-requests: write.