Sub topic 14
Least-privilege permissions and cancelling superseded runs
This chapter assumes you can already write a test workflow with push and pull_request triggers, and that you know what jobs and steps are. It covers two workflow-level settings: permissions, which limits what the workflow can do, and concurrency, which stops runs that are no longer needed.
Why limit the GITHUB_TOKEN
Every workflow run receives a GITHUB_TOKEN. This is a short-lived credential that GitHub creates for the run and revokes when the run ends. Steps and actions use it to call the GitHub API, for example to read the repository or comment on a pull request.
The default permissions of this token depend on repository and organization settings. In some repositories the default is read and write access to many scopes. A test workflow rarely needs that. If a dependency or a third-party action in your workflow is compromised, it inherits whatever the token allows. Least privilege means granting only what the job needs.
Setting permissions
Add a top-level permissions key. Once you set it, every scope you do not list is set to none.
permissions:
contents: readcontents: read is enough for actions/checkout to download your code. Running tests, caching, and uploading artifacts need no further scopes.
If one job needs more, set permissions on that job. A job-level setting replaces the workflow-level one for that job.
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
comment:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- run: echo "This job may write pull request comments."Here only the comment job can write to pull requests. The test job stays read-only.
Cancelling superseded runs
Suppose you push three commits to a pull request in a minute. Without any setting, three full test runs start, and only the last one matters. The concurrency key fixes this.
concurrency:
group: tests-${{ github.ref }}
cancel-in-progress: trueRuns that share the same group value are limited to one at a time. With cancel-in-progress: true, a new run cancels the one already running. Because the group includes github.ref, only runs on the same branch or pull request compete. A run on another branch is unaffected.
Complete example
Both settings sit at the top of the file, next to name and on.
name: Tests
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: tests-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testCommon errors
- Cancelling runs on main. With the group above, two quick merges to
maincancel the first run, so a commit onmainmay never get a result. If you want everymaincommit tested, cancel only for pull requests:cancel-in-progress: ${{ github.event_name == 'pull_request' }}. - A group that is too broad. A fixed group such as
testsmakes unrelated branches cancel each other. Includegithub.refin the name. - Reading a cancelled run as a failure. A cancelled run shows a gray status, not a red one. If it is the latest run on a pull request, a newer run is usually responsible. Check the run list before debugging.
- Forgetting that
permissionsremoves scopes. Addingcontents: readalso removes defaults such aspull-requests: write. A step that posts comments will then fail until you grant that scope.
Exercise
Two developers push to different branches at the same time. Your workflow uses group: tests-${{ github.ref }} with cancel-in-progress: true. Does either run get cancelled? Why or why not?
Show answer
No. github.ref differs between the branches, so the runs belong to different concurrency groups. Cancellation only happens between runs in the same group, such as two pushes to the same branch.
Exercise
A test workflow has permissions: contents: read at the top. One job must post a comment on the pull request. How do you allow this without giving the other jobs write access?
Show answer
Add a permissions block to that job only, with contents: read and pull-requests: write. The job-level setting replaces the workflow-level one for that job, and the other jobs keep read-only access.