Sub topic 3
How concurrency groups work
This chapter explains what a concurrency group is, how GitHub Actions uses it to decide which runs may proceed, and where the group is declared. It assumes you can read basic workflow syntax (on, jobs, steps), know the common triggers such as push and pull_request, and have seen ${{ }} expressions and the github context. Later chapters cover the pending queue, cancel-in-progress, and group naming in more detail. Here the goal is a working model of the group itself.
What a concurrency group is
A concurrency group is a string label. GitHub Actions uses that label to answer one question: "Is another run or job with the same label already active?"
The rule is simple:
- Within one repository, at most one workflow run or job with a given group label can be in progress at a time.
- Anything else that arrives with the same label has to wait, or it replaces something, depending on your settings.
The group has no other meaning. GitHub does not look at the workflow file, the branch, or the author. Two unrelated workflows with the same label compete. Two runs of the same workflow with different labels do not. Everything you control about overlap comes from choosing the label.
Groups are also case-insensitive. Deploy-Prod and deploy-prod refer to the same group.
The concurrency key
You declare a group with the concurrency key. It accepts two forms.
The short form is just the group label:
concurrency: deploy-productionThe long form is a mapping with a group and, optionally, cancel-in-progress:
concurrency:
group: deploy-production
cancel-in-progress: falseUse the long form whenever you need cancel-in-progress. The short form behaves as if cancel-in-progress were false.
If you omit concurrency entirely, no group exists. Every run starts as soon as a runner is available, and runs overlap freely.
Where you can declare it
You can place concurrency at two levels.
| Level | Placement | What competes |
|---|---|---|
| Workflow | Top level of the file, beside `on` and `jobs` | Whole workflow runs |
| Job | Inside a job, beside `runs-on` and `steps` | Single jobs, possibly from different runs |
A workflow-level group decides whether an entire run may start. A job-level group lets the run start, then holds back only the job that needs the shared resource. The job-level form is covered in its own chapter. This chapter uses the workflow level for examples.
A first example
This workflow groups runs by workflow name and 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"The label is built from two parts joined by a hyphen.
github.workflowis the workflow name, hereCI.github.head_ref || github.run_idis the source branch of a pull request. On apushevent there is nohead_ref, so the expression falls back togithub.run_id, which is unique per run.
For a pull request from branch feature-x, the label is CI-feature-x. Every push to that branch produces a run with the same label, so those runs compete. For a push to main, the label is CI- followed by the run ID. No other run ever has that label, so push runs never compete with each other.
How the label is evaluated
The group string is computed when the run is created, before any job starts. This has three consequences.
- You can use expressions, but only from contexts that exist at that moment. Workflow-level
concurrencycan use thegithub,inputs, andvarscontexts. At job level you can also useneeds,strategy, andmatrix. - You cannot use the
secretscontext, and you cannot use step outputs, because no steps have run yet. - If an expression evaluates to an empty string, you get a group with an empty name. All runs that produce an empty label then share one group. This is a common source of surprising behavior, and the pitfalls chapter returns to it.
Always check what your expression produces for every trigger the workflow listens to. An expression that is useful for pull_request can be empty or constant for push.
What happens when a second run arrives
Suppose run A holds a group and is in progress. Run B arrives with the same label. GitHub then applies these rules.
- B is not started. It enters the
pendingstate. - If
cancel-in-progressistrue, GitHub cancels A, and B starts. - If
cancel-in-progressisfalseor unset, B waits until A finishes.
The diagram shows the decision for one arriving run.
The group only ever holds one running entry and one pending entry. If a third run C arrives while A runs and B is pending, B is cancelled and C becomes the pending run. The next chapter covers that behavior in detail.
Groups are scoped to the repository
A group label is only compared with other runs in the same repository. A run in org/repo-one and a run in org/repo-two never compete, even with identical labels.
Within the repository, the label is global across all workflows. This matters because a label is just text. Consider two workflow files:
# .github/workflows/build.yml
concurrency:
group: shared-lock# .github/workflows/docs.yml
concurrency:
group: shared-lockA build run and a docs run now exclude each other. That may be what you want, such as two workflows that both write to the same environment. It is also an easy mistake. Including github.workflow in the label, as the first example does, prevents accidental collisions between workflows.
Serializing with a fixed label
A fixed label with no expressions makes every run in the repository share one group. That fits resources where only one actor may work at a time, such as a production deployment:
name: Deploy
on:
push:
branches: [main]
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploy to production"With cancel-in-progress: false, a running deployment is never interrupted. A newer run waits and takes its turn. This is the basis for the serialization pattern you will see later.
Choosing a label: what should compete?
Before writing a label, decide which runs are interchangeable and which are independent. The table gives common choices.
| Goal | Label shape | Effect | Column | Column |
|---|---|---|---|---|
| One run per workflow | `${{ github.workflow }}` | All runs of this workflow compete | ||
| One run per branch or ref | `${{ github.workflow }}-${{ github.ref }}` | Runs on the same ref compete | ||
| One run per pull request | `${{ github.workflow }}-${{ github.head_ref }}` | Pull request runs compete by source branch | ||
| One run per shared resource | `deploy-production` | Any workflow using that label competes | ||
| No competition for some events | `... | github.run_id` | A unique label means no match |
The github.run_id fallback deserves attention. It is a deliberate way to opt a trigger out of grouping, because a unique label has no peers.
Common mistakes with groups
- Forgetting that the label is shared across workflows. A hard-coded label in two files links them. Add
github.workflowunless you intend the link. - Using
github.reffor pull requests and expecting the branch name. Forpull_requestevents,github.reflooks likerefs/pull/123/merge, so it is stable per pull request and works, but it differs from thepushref of the same branch. Usehead_refif you want push and pull request runs to share a label, noting thathead_refis empty onpush. - Using a context that is empty. An empty or missing value can merge unrelated runs into one group.
- Assuming that grouping is automatic. Without a
concurrencykey, nothing is limited. - Expecting a group to span repositories. It does not.
You can observe grouping in the Actions tab. A run waiting on a group shows a pending status with a message that it is waiting for earlier runs in the group.
Exercise
Two workflow files in the same repository both declare concurrency: { group: release }. A run from the first file is in progress. A run from the second file starts with the default cancel-in-progress. What happens to the second run, and how would you stop the two workflows from affecting each other?
Show answer
The second run waits as pending until the first finishes, because the label release is shared across all workflows in the repository and the default for cancel-in-progress is false. To separate them, include ${{ github.workflow }} in the label, for example group: ${{ github.workflow }}-release, so each workflow gets its own group.
Exercise
A workflow listens to push and pull_request and uses group: ${{ github.head_ref }}. Pushes to main seem to wait for each other. Why, and what is a fix?
Show answer
On push events github.head_ref is empty, so every push run gets an empty label and they all share one group. A fix is to add a fallback, such as group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}, so push runs get unique labels.