Sub topic 6
Choosing group names with contexts
A concurrency group is just a string. Two runs compete only if their strings match exactly. The group name decides which runs are "related", so choosing it is the main design decision in this feature.
This chapter assumes you know basic workflow syntax (on, jobs, steps), the common triggers, and how ${{ }} expressions read from the github context.
Why the name matters
The concurrency.group value is evaluated as an expression when the run starts. The result is compared with the groups of other runs in the same repository. Matching strings share one slot: one running run and at most one pending run.
Two mistakes are common:
- A name that is too broad, such as a fixed string, makes unrelated runs compete. A push to one branch can cancel a run on another.
- A name that is too narrow, such as one that includes the commit SHA, gives every run its own group. Nothing ever cancels or queues.
Useful contexts
Group names are usually built from these values.
| Expression | What it holds | Typical use |
|---|---|---|
| `github.workflow` | The workflow name | Keep different workflows from colliding |
| `github.ref` | Full ref, such as `refs/heads/main` or `refs/pull/12/merge` | One group per branch or pull request |
| `github.head_ref` | Source branch of a pull request. Empty on other events. | Pull request runs only |
| `github.run_id` | A unique number per run | A fallback that never matches another run |
| `github.event_name` | The trigger, such as `push` | Separate triggers |
Expressions that can be empty need a fallback. The || operator returns the right side when the left side is an empty string.
Worked example
This workflow runs on pushes to main and on pull requests.
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"The group name works out as follows:
- On a pull request event,
github.head_refis the branch name, for examplefeature-login. The group isCI-feature-login. Every new push to that branch lands in the same group, so it replaces the older run. - On a push to
main,github.head_refis empty. The expression falls back togithub.run_id, giving a group such asCI-8123456789. Each push has a different ID, so each run is alone in its group and none of them cancel each other.
The github.workflow prefix means a lint workflow and a test workflow on the same branch do not cancel each other.
Checking your names
Print the group while you design it. This step shows what the expression produces for each trigger:
steps:
- run: echo "group is ${{ github.workflow }}-${{ github.head_ref || github.run_id }}"Exercise
A workflow named Build triggers on pull_request and uses group: Build-${{ github.head_ref }}. A teammate adds a push trigger for main. What group do the main push runs get, and what problem does it cause?
Show answer
On a push, github.head_ref is empty, so every push run gets the group Build-. All pushes to main share one group and cancel or replace each other. Adding || github.run_id gives each push run its own group.