Sub topic 2
The concurrency key
This chapter introduces the concurrency key: where it goes, what it accepts, and what it does to a run. It assumes you can read basic workflow syntax (on, jobs, steps), know common triggers such as push and pull_request, and can use ${{ }} expressions with the github context.
What the key does
The concurrency key tells GitHub Actions that a run, or a single job, belongs to a named group. At any moment, only one run or job in a group can be running. The key does not change what your steps do. It only controls how runs that share a group name interact.
You can set it in two places:
- At the top level of a workflow, where it applies to the whole run.
- Inside a single job, where it applies only to that job.
This chapter focuses on the workflow level. Job-level use comes later in the tutorial.
The two settings
The key takes a mapping with two settings.
| Setting | Purpose | Required |
|---|---|---|
| `group` | The name of the group. Runs with the same name compete. | Yes |
| `cancel-in-progress` | Whether a newer run cancels the run that is already going. | No, defaults to `false` |
The group value is a string. It can be a fixed name or an expression built from contexts. Group names are case-insensitive.
There is also a short form. If you only need a group name, write it as a plain string:
concurrency: deploy-productionThis is the same as setting group: deploy-production with cancel-in-progress left at its default.
A first example
This workflow uses both settings. It builds the group name from the workflow name and the pull request branch.
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"Read the group expression in two parts:
github.workflowis the workflow name, hereCI. Including it keeps this workflow's runs from competing with runs of other workflows.github.head_ref || github.run_idpicks the branch name on pull request events.head_refis only set forpull_requestevents. On apush, it is empty, so the expression falls back togithub.run_id, which is unique to each run.
The result is that two runs for the same pull request branch share a group, and the newer one cancels the older one. Push runs each get a unique group, so they never cancel each other.
How a run enters a group
When a run starts, GitHub evaluates the group expression. It then looks for other runs or jobs already using that name.
- If the group is empty, the run starts normally.
- If another run is in progress in the group, the new run waits as pending. With
cancel-in-progress: true, GitHub cancels the older run instead, and the new run starts.
The details of pending runs are covered in the next chapter.
Common errors
- Forgetting the fallback. Using
${{ github.head_ref }}alone gives an empty name onpushevents. All push runs then land in one group, and they can cancel each other. Add|| github.run_id. - Omitting the workflow name. Two different workflows with the same group name compete with each other. Prefix the name with
github.workflow. - Assuming cancellation is the default. Without
cancel-in-progress: true, a newer run waits instead of replacing the old one.
Exercise
A workflow triggers on both push and pull_request and uses group: ${{ github.workflow }}-${{ github.head_ref }} with cancel-in-progress: true. What goes wrong on a push to main, and how do you fix it?
Show answer
On a push, github.head_ref is empty, so every push run gets the same group name (the workflow name plus a dash). A newer push cancels the older one. Fix it by adding a fallback: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}. Each push run then gets a unique group.