Sub topic 2
The concurrency key
This chapter covers the concurrency key: where it goes, what forms it takes, and how GitHub turns it into a group name. It assumes you can read and write basic workflow syntax (on, jobs, steps), know common triggers such as push, pull_request and workflow_dispatch, and have seen ${{ }} expressions and the github context. The next chapters cover how groups queue runs and when to cancel. Here the focus is the key itself.
What the key does
By default, GitHub Actions starts every triggered workflow run right away. Two pushes in a minute give you two runs at the same time. That is fine for tests that touch nothing shared. It is a problem for deployments, shared test environments, and any workflow whose newer run makes the older one pointless.
The concurrency key is how you tell GitHub that certain runs or jobs compete. You give them a shared label, called the group. When a run or job starts, GitHub looks at its group name and compares it with the runs and jobs already using that name.
The key does nothing by itself except attach a group name. The other settings, such as cancel-in-progress, only change what happens when two things carry the same name.
Two syntax forms
The concurrency key accepts a string or a mapping.
The string form sets only the group name:
concurrency: deploy-productionThe mapping form sets the group name and, optionally, cancel-in-progress:
concurrency:
group: deploy-production
cancel-in-progress: trueThe two properties of the mapping are:
| Property | Type | Required | Meaning |
|---|---|---|---|
| `group` | string | yes | Name that decides which runs or jobs compete |
| `cancel-in-progress` | boolean or expression | no | Whether a newer arrival cancels the running one. Defaults to `false` |
If you use the mapping form, group is required. Writing only cancel-in-progress is a syntax error. The string form is the same as the mapping with cancel-in-progress left at its default of false.
Where the key can go
You can place concurrency in two spots:
- At the top level of the workflow file, next to
onandjobs. The whole run gets the group name. - Inside a single job, next to
runs-on. Only that job gets the group name.
A complete workflow with the key at the top level looks like this:
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"You can use both placements in one workflow. They are independent, so a run can hold a workflow-level group while one of its jobs also holds a job-level group. Job-level concurrency gets its own chapter. For now, remember that the same naming rules apply in both places.
How the group name is computed
The group value is a string, and it can contain expressions. GitHub evaluates the expression when the run is created, before any job starts. The resulting plain string is the group name.
Take this group:
concurrency:
group: ci-${{ github.ref }}A push to main produces the name ci-refs/heads/main. A push to feature/login produces ci-refs/heads/feature/login. These are different groups, so those two runs do not compete. Two pushes to feature/login produce the same name, so those two runs do compete.
This is the central idea of the chapter. You choose which runs compete by choosing which part of the name varies. A fixed string makes everything compete. Adding github.ref makes only runs on the same ref compete. Adding github.workflow keeps different workflows apart. The next chapters build practical names from these pieces.
Which contexts you can use
Expressions in group can only read some contexts, and the allowed set depends on where the key sits.
| Placement | Contexts available |
|---|---|
| Workflow level | `github`, `inputs`, `vars` |
| Job level | `github`, `needs`, `strategy`, `matrix`, `inputs`, `vars` |
Two consequences matter in practice:
- You cannot use
secretsorenvin a group name. A secret in a group name would also be visible in the run UI. - At workflow level, nothing from a job exists yet. You cannot use
matrixorneedsthere. A job-level group can usematrix, which lets each matrix leg get its own group.
The inputs context is available for workflow_dispatch and reusable workflows. For other triggers, inputs is empty, so an expression like ${{ inputs.environment }} evaluates to an empty string.
Empty values and fallbacks
An expression that evaluates to nothing leaves a gap in the name. For example, github.head_ref is set only on pull_request events. On a push, ${{ github.head_ref }} is an empty string, so a group of ci-${{ github.head_ref }} becomes ci- for every push. All push runs then share one group.
The || operator gives you a fallback:
concurrency:
group: ci-${{ github.head_ref || github.run_id }}github.run_id is unique to each run. Pull request runs group by branch name, and push runs each get a unique group, so they never compete. You will use this pattern in the pull request chapter.
Rules for group names
A few rules apply to every group name:
- Names are case-insensitive.
Deploy-Prodanddeploy-prodare the same group. - Groups are scoped to the repository. Runs in other repositories never compete with yours, even if the name matches.
- The name is shared across workflows and jobs in that repository. Two different workflow files that use
group: deployare in the same group. A workflow-level group and a job-level group with the same string are also the same group. - The name is only a label. GitHub does not create anything when you pick a new name. The first run to use it creates the group implicitly, and it disappears when no run or job holds it.
Rule 3 is the source of most surprises, because it lets unrelated workflows cancel each other by accident. The pitfalls chapter returns to this.
What happens when two runs share a name
The full queueing rules are the subject of the next chapter. The short version is enough to read the key correctly: for a given group, GitHub allows at most one run or job to be in progress and at most one to be pending. A third arrival replaces the pending one. The cancel-in-progress setting changes this by cancelling the in-progress one instead of making the newer one wait.
Checking your group name
GitHub does not print the computed group name in the run summary by default. When a name misbehaves, print it yourself. Because the group is computed from contexts, you can echo the same contexts in a step and compare them by eye:
name: Show group inputs
on:
pull_request:
push:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print group parts
run: |
echo "workflow: $WORKFLOW"
echo "head_ref: $HEAD_REF"
echo "run_id: $RUN_ID"
env:
WORKFLOW: ${{ github.workflow }}
HEAD_REF: ${{ github.head_ref }}
RUN_ID: ${{ github.run_id }}The values go through env rather than being written into the script text. Branch names are user-controlled, so interpolating them straight into run: can let a crafted branch name inject shell commands.
Common mistakes with the key
- Using a
secretsorenvexpression ingroup. These contexts are not available there, and the workflow fails validation. - Using
matrixat workflow level. Only job-level concurrency can read it. - Forgetting that
github.head_refis empty outside pull requests. The group collapses to a shared name, and unrelated push runs compete. - Expecting case to separate groups.
CI-Mainandci-maincollide. - Putting the key under
steps. Concurrency applies to a workflow or a job, never to a single step.
Exercise
A workflow runs on both push and pull_request. It sets concurrency: group: build-${{ github.head_ref }}. A developer pushes two commits in a row to main, and the second run waits until the first finishes. Why do these runs compete, and what one-line change to the group value keeps push runs from competing with each other?
Show answer
On a push event, github.head_ref is empty, so every push run computes the same group name, build-. Runs in the same group compete, so the second push run waits as pending. Adding a fallback, build-${{ github.head_ref || github.run_id }}, gives each push run a unique group because github.run_id differs per run. Pull request runs still share a group per branch.
Exercise
Which of these is a valid group expression at workflow level, and why are the others not? (a) ${{ github.workflow }}-${{ github.ref }} (b) deploy-${{ secrets.ENV_NAME }} (c) test-${{ matrix.os }}
Show answer
Only (a) is valid. Workflow-level group can read the github, inputs and vars contexts. (b) uses secrets, which is not available in a group name. (c) uses matrix, which exists only inside a job, so it is valid only for job-level concurrency.