Sub topic 7
Restricting at workflow level, elevating per job
This chapter assumes you can read workflow YAML (on, jobs, steps) and know that the GITHUB_TOKEN is a short-lived credential created for each job. It also assumes you know the permissions key accepts scopes such as contents and pull-requests, each set to read, write, or none.
Two places to set permissions
You can write permissions in two places:
- At the top of the workflow file, 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. Any scope the job block leaves out becomes none. This is the most common source of confusion, so keep it in mind as you read the examples.
Setting a read-only baseline
The simplest pattern is a workflow-level block that grants only what every job needs. For a test workflow, that is read access to repository contents so that checkout works.
name: ci
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make testEvery job in this file gets contents: read and nothing else. If someone adds a job later, it starts with the same small token.
Removing everything, then elevating
When a workflow has several jobs with different needs, start from an empty baseline. The block permissions: {} sets every scope to none. Each job then declares exactly what it needs.
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 as well as pull-requests: write. Because the job block replaces the workflow block, the job must restate every scope it uses, including the read access the labeler needs to read its config file.
Elevating only the job that publishes
Write access should sit in the one job that needs it. Here a tag push publishes a container image to GitHub Container Registry. The workflow default stays at contents: read, and only the push job adds packages: write.
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 persist-credentials: false setting tells checkout not to leave the token in the local git config. This job never runs git push, so it does not need that credential on disk.
Common mistakes
- Granting
writeat workflow level "to make it work". Every job and every third-party action in the workflow then holds that access. - Forgetting to restate
contents: readin a job block that needs checkout. - Putting
permissionsundersteps. It is valid only at workflow or job level.
Exercise
A workflow has permissions: { contents: read } at the top. One job adds permissions: { issues: write } and runs actions/checkout. What happens, and how do you fix it?
Show answer
The job block replaces the workflow block, so the job's token has issues: write and contents: none. Checkout fails on a private repository. Fix it by adding contents: read to the job's block.