Sub topic 17
Limiting GITHUB_TOKEN Permissions and Controlling Overlapping Runs
This chapter assumes you can read a workflow file, know what jobs and steps are, and have used secrets and env. It covers two safeguards: limiting what the built-in token can do, and stopping runs from overlapping.
Limiting GITHUB_TOKEN permissions
Every workflow run gets a temporary token called GITHUB_TOKEN. GitHub creates it when the run starts and revokes it when the run ends. Steps use it to call the GitHub API, for example to check out code, comment on a pull request, or publish a release.
The token's default permissions depend on repository and organization settings. They can be broad, so a compromised step or third-party action could write to your repository. The permissions key narrows the token to what the workflow needs.
Setting permissions
Put permissions at the top of the file to apply it to every job. Put it inside a job to override the workflow-level value for that job only.
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
comment:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- run: echo "This job may write pull request comments."Each scope takes read, write, or none. Common scopes are contents, pull-requests, issues, packages, and id-token.
Rules to remember
- When you set
permissions, every scope you do not list becomesnone. permissions: {}removes all permissions.- A job-level block replaces the workflow-level block. It does not merge with it.
- Pull requests from forks get a read-only token regardless of your settings.
Controlling overlapping runs with concurrency
Pushing twice in a row starts two runs. For tests this only wastes minutes. For deployments, two runs touching the same environment can leave it in a mixed state.
The concurrency key assigns a run or job to a named group. GitHub allows only one running and one pending run per group at a time.
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: falseThe group name is an expression, so this example creates one group per branch. With cancel-in-progress: false, a new run waits for the current run to finish. If a newer run arrives while another is already waiting, the waiting run is cancelled and replaced by the newer one.
Set cancel-in-progress: true when only the latest run matters, such as tests on a pull request:
concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true| Setting | New run arrives while one is running | Typical use |
|---|---|---|
| `cancel-in-progress: false` | Waits for the running one to finish | Deployments |
| `cancel-in-progress: true` | Cancels the running one | Tests and linting |
Exercise
A workflow sets permissions: contents: read at the top. One job needs to post a pull request comment. What do you add, and where?
Show answer
Add a job-level permissions block to that job with contents: read and pull-requests: write. The job-level block replaces the workflow-level one, so contents: read must be listed again if the job still needs it.
Exercise
Why would you choose cancel-in-progress: false for a production deployment?
Show answer
Cancelling a deployment halfway can leave the environment partly updated. With false, the running deployment finishes and the next one waits its turn.