Sub topic 7
Restricting at workflow level, elevating per job
This chapter assumes you can read basic workflow syntax (on, jobs, steps) and YAML. It also assumes you know what the GITHUB_TOKEN is and what the permissions key looks like. Here you learn where the key can go, how the levels interact, and how to build a workflow where each job holds only the access it needs.
Two places for the permissions key
You can put permissions in two places in a workflow file:
- At the top level of the file, where it applies to every job.
- Inside a single job, where it applies to that job only.
A job-level block replaces the workflow-level block for that job. It does not merge with it. This is the most common source of confusion, so the rest of the chapter returns to it.
Setting a floor at workflow level
A workflow-level block gives every job the same baseline. The simplest useful baseline is read access to repository contents, which is enough to check out code and run tests.
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testWhen you list any scope in a permissions block, every scope you do not list is set to none. Here the token can read contents and nothing else. It cannot write to issues, pull requests, packages, or anything with the write level.
This matters for jobs that you add later. A new job inherits the baseline, so a careless edit cannot silently give it broad access.
Starting from nothing
If a workflow has jobs that need different access, the baseline can be empty. An empty mapping removes every permission:
permissions: {}With an empty block, no job has any access unless it declares its own. Even contents: read is gone, so a job that runs actions/checkout on a private repository needs to ask for it. This is the strictest starting point, and it forces each job to state what it needs.
Use an empty baseline when jobs differ a lot. Use contents: read when nearly every job checks out code.
Elevating per job
A job that needs more than the baseline declares its own block. Because the job block replaces the workflow block, it must list every scope the job needs, including ones that the baseline already granted.
name: label
on: pull_request
permissions: {}
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/labeler@v5The label job lists contents: read and pull-requests: write. The labeler action reads its configuration file from the repository and then writes labels to the pull request, so it needs both. Any other job in this file would have no access at all.
The replacement pitfall
Suppose the workflow baseline is contents: read and a job adds only pull-requests: write:
permissions:
contents: read
jobs:
label:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/labeler@v5You might expect the job to have both scopes. It does not. The job-level block replaces the workflow-level one, so contents becomes none for this job. A step that needs to read files then fails with a permission error. The fix is to repeat contents: read in the job block.
A realistic multi-job example
Most real pipelines have a low-risk job, such as tests, and a high-risk job, such as publishing. Give the risky job the extra scope and keep it away from everything else.
name: publish-image
on:
push:
tags: ['v*']
permissions:
contents: read
jobs:
push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }}The baseline is read-only. The push job adds packages: write so it can push to GitHub Container Registry. It repeats contents: read because of the replacement rule. The checkout step sets persist-credentials: false, so the token is not saved in the local git configuration where later steps could read it. A later chapter covers that setting in detail.
If you add a lint or test job to this file, it gets only contents: read. It cannot push packages, even though the push job can.
What the levels do not change
Some behavior is outside the permissions key. Keep these limits in mind:
- The key can only lower or set scopes the token can have. It cannot grant access beyond what the event and repository allow. For example, a workflow triggered by a pull request from a fork gets a read-only token for most scopes, even if the file asks for
write. - Organization and enterprise policy can cap the result. If an owner restricts the default to read-only, a
permissionsblock can still raise a job towritein the same repository, but an owner can also restrict which actions and workflows run at all. - Steps share their job's token. You cannot give one step in a job more access than another. If a single step needs
writeand the others do not, move that step into its own job.
That last point drives how you structure workflows. Splitting a job costs a little time, because it adds a new runner and may need artifacts passed between jobs. In exchange, the dangerous scope reaches only the steps that need it.
Passing work between jobs
When you split a job for permissions, the jobs no longer share a file system. Use artifacts or job outputs to move results from the low-privilege job to the privileged one.
name: release
on:
push:
tags: ['v*']
permissions: {}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- run: tar -czf dist.tar.gz src/
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist.tar.gz
release:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@v4
with:
name: dist
- run: gh release create "$TAG" dist.tar.gz
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}The build job runs project code with read-only access. The release job holds contents: write, but it never checks out or runs project code. It only downloads the finished artifact and publishes it. If a build step is compromised, it cannot create a release directly.
Artifact upload and download use a separate runtime token, not the GITHUB_TOKEN, so neither job needs an actions scope for them.
Checking the effective permissions
A log line tells you what a job received. Open a run, select a job, and expand the Set up job step. Under GITHUB_TOKEN Permissions you see the final list, after the default, the workflow block, and the job block were applied. Check it whenever a job behaves unexpectedly.
| Situation | Effective token |
|---|---|
| No `permissions` key anywhere | The repository default (read-only or permissive) |
| Workflow `contents: read`, job has no key | `contents: read`, all else `none` |
| Workflow `contents: read`, job `issues: write` | `issues: write` only, `contents` is `none` |
| Workflow `{}`, job `contents: read` | `contents: read` only |
| Workflow `{}`, job has no key | No access |
Common mistakes
- Adding a job-level block with only the new scope and losing
contents: read. - Putting
writescopes at workflow level "to keep things simple". Every job and every action then gets them. - Using
permissions: write-allto silence an error. This grants the widest possible token to all jobs. - Misspelling a scope name. GitHub rejects unknown scope names when it validates the file, so the workflow fails to start. Check the spelling against the documented list, such as
pull-requestswith a hyphen.
Exercise
A workflow has permissions: contents: read at the top. The job comment adds permissions: issues: write and then fails when actions/checkout runs on a private repository. Why does it fail, and how do you fix it?
Show answer
The job-level block replaces the workflow-level block, so contents becomes none for this job and checkout cannot read the repository. Fix it by listing both scopes in the job: contents: read and issues: write.
Exercise
A single job runs the test suite and then pushes a package, so it has packages: write. Describe a safer structure and one cost of it.
Show answer
Split it into two jobs. A test job with only contents: read runs the tests, and a publish job with contents: read and packages: write pushes the package, using needs: test to order them. The cost is an extra runner and the need to pass build output between jobs with artifacts or job outputs, because the jobs do not share a file system.