Sub topic 8
Finding the minimal set for a job
This chapter assumes you can read a workflow file and know how the permissions key works at workflow and job level. The goal is to work out which permissions a job needs, not to guess them.
Start from nothing and add
Set the workflow to permissions: {}, then give each job only what it proves it needs. Adding permissions one at a time is safer than starting broad and trying to remove them. When you remove them, you can't tell which ones the job still depends on.
Read the steps first
Before running anything, list what each step touches. Most steps fall into a few groups.
| Step | Typical permissions |
|---|---|
| `actions/checkout` on a private repository | `contents: read` |
| `actions/labeler` | `contents: read`, `pull-requests: write` |
| `gh issue comment` | `issues: write` |
| Push an image to ghcr.io | `packages: write` |
| `actions/upload-artifact`, `actions/cache` | none |
Artifacts and caches use a separate runtime credential, not the GITHUB_TOKEN. Public repositories can also check out code without contents: read, but keeping it is harmless and keeps the workflow working if the repository goes private.
Check each third-party action's README. Well-maintained actions list the permissions they need, usually in a "Permissions" section or in their example workflows.
Check what the job actually received
Open a run and expand the Set up job step. Its log includes a GITHUB_TOKEN Permissions list showing the exact scopes the job received. Use it to confirm your permissions block took effect. If a job-level block is missing, you will see the workflow-level or repository default instead.
Remember that listing any scope in a permissions block sets every unlisted scope to none. The one exception is metadata: read, which is always granted.
A worked example
This job posts a comment on a pull request using the GitHub CLI. It starts with no permissions.
name: pr-comment
on: pull_request
permissions: {}
jobs:
comment:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
- 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 "Thanks for the contribution."With an empty block, the comment step fails with Resource not accessible by integration. The failing call is a write to a pull request, so you add pull-requests: write. The checkout step needs contents: read once the repository is private. Nothing else is needed, so the set is finished.
On a pull request from a fork, this write still fails because the token is read-only there. The troubleshooting chapter covers that case.
Find the permission from the call
When an error doesn't name a scope, look at the API call that failed. The REST API documentation for each endpoint has a "Fine-grained access tokens" section listing the permission it requires. Match the endpoint to that list, then grant the lowest level that works.
- Pick
readwhen the step only fetches data, andwriteonly when it creates or changes something. - Add one scope per iteration, so you know which scope fixed which error.
- Re-run the job after every change instead of adding several scopes at once.
Exercise
A job with permissions: {} fails on a step that runs gh issue comment 12 --body "Done" with "Resource not accessible by integration". Which permission should you add, and at what level?
Show answer
Add issues: write at job level. Commenting creates content on an issue, so read access is not enough. Add it to the job that runs the step, not to the whole workflow.