Sub topic 3
How concurrency groups work
This chapter assumes you can read and write basic workflow syntax (on, jobs, steps), know the common triggers such as push and pull_request, and can use ${{ }} expressions with the github context.
What a group is
A concurrency group is a string label. When you add a concurrency key to a workflow, GitHub computes that string for each run. Runs that produce the same string belong to the same group, and GitHub allows only one of them to be running at a time. Runs with different strings never affect each other.
The group name is the only thing that links runs. GitHub does not compare branches, authors or file changes. It compares the evaluated string.
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"Reading the group expression
The expression ${{ github.workflow }}-${{ github.head_ref || github.run_id }} has two parts.
github.workflowis the workflow name, so different workflows in the same repository do not share a group by accident.github.head_ref || github.run_idpicks the pull request's source branch when there is one. On apull_requestevent,github.head_refholds the branch name. On apushevent it is empty, so the expression falls back togithub.run_id, which is unique for every run.
The result is that all runs for one pull request branch share a group. Every push run gets its own group and is never cancelled by another run.
| Event | `github.head_ref` | Group string (workflow named CI) |
|---|---|---|
| `pull_request` from `feature-x` | `feature-x` | `CI-feature-x` |
| `pull_request` from `feature-x` (next push) | `feature-x` | `CI-feature-x` (same group) |
| `push` to `main` | empty | `CI-` followed by the run ID |
The two slots in a group
A group has two slots: one running run and one pending run. The cancel-in-progress setting decides what a newly arrived run does when the running slot is taken.
With cancel-in-progress: false, a running run is left alone. The new run waits as pending. If a third run arrives while one is already pending, the older pending run is cancelled and the newest one takes its place. Only the latest waiting run survives, so a burst of five pushes does not queue five runs.
Workflow level and job level
You can place concurrency at the top of a workflow or inside a single job. At the top level, the whole run competes for the group. Inside a job, only that job does, as shown below. Later chapters cover when each fits.
jobs:
integration:
runs-on: ubuntu-latest
concurrency:
group: integration-env-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Run integration tests against a shared environment"Common errors
- A constant group name for everything.
group: cimakes every run in the repository share one group, so unrelated branches cancel or block each other. - Using
github.head_refalone. It is empty onpush, so all push runs land in one group named only by the workflow. Add a fallback such asgithub.run_id. - Expecting a queue. Only one run can be pending. Intermediate runs are dropped, not delayed.
Exercise
A workflow uses group: ${{ github.workflow }}-${{ github.ref }} with cancel-in-progress: false. While run 1 is running on refs/heads/dev, runs 2 and 3 start on the same ref. What state is each run in afterwards?
Show answer
Run 1 keeps running. Run 2 first becomes pending, then is cancelled when run 3 arrives, because a group holds only one pending run. Run 3 stays pending and starts when run 1 finishes.