Sub topic 6
Choosing group names with contexts
This chapter assumes you can write a basic workflow with on, jobs and steps. It also assumes you know the ${{ }} expression syntax and the github context. You should already know that concurrency takes a group and an optional cancel-in-progress.
The group name decides who competes
A concurrency group is only a string. GitHub compares the string from each new run with the strings of runs that are already running or pending. If two strings match, the runs compete: only one can run at a time. If the strings differ, the runs ignore each other.
So the name you build decides which runs queue behind each other or cancel each other. A name that is too broad cancels unrelated work. A name that is too narrow never cancels anything.
Some properties of group names hold in every case:
- Names are case-insensitive.
Deploy-Prodanddeploy-prodare the same group. - Names are scoped to the repository. Runs in other repositories never collide with yours.
- A literal name such as
deploy-productionis the same for every run, so every run of every workflow that uses it competes. - An expression is evaluated once, when the run is created, and the resulting string is the group.
Which contexts you can use
At the workflow level, the group expression can read the github, inputs and vars contexts. At the job level it can also read needs, strategy and matrix. The secrets context is not available in either place.
These restrictions follow from timing. The group must be known before any job runs, so the expression cannot depend on step outputs or on values computed during the run. At the job level, needs and matrix are available because the job is only queued after those values exist.
The most useful properties for building names are:
| Expression | Meaning | Typical value |
|---|---|---|
| `github.workflow` | Workflow name, or the file path if the workflow has no `name` | `CI` |
| `github.ref` | Full ref that triggered the run | `refs/heads/main` or `refs/pull/42/merge` |
| `github.head_ref` | Source branch of a pull request. Empty for other events | `feature/login` |
| `github.event.pull_request.number` | Pull request number. Only exists on pull request events | `42` |
| `github.run_id` | Unique ID of the run, shared by re-run attempts | `8123456789` |
| `github.event_name` | Event that triggered the run | `push` |
| `inputs.<name>` | Input of a `workflow_dispatch` or `workflow_call` | `production` |
Two details matter here. The first is that github.ref has a different shape for every event. A push to a branch gives refs/heads/<branch>, a push of a tag gives refs/tags/<tag>, and a pull request gives refs/pull/<number>/merge. The second is that github.head_ref is set only on pull_request and pull_request_target events. On a push it is an empty string.
Build the name from three parts
A good name usually has three parts, from broad to narrow:
- The workflow. Without it, your lint workflow and your release workflow share a group and cancel each other.
- The thing being protected. This is often a branch, a pull request, or an environment.
- A tiebreaker when you want some runs never to compete.
Here is the pattern from the spine, with each part marked:
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 || operator returns the first value that is not empty. Follow the two cases:
- On a pull request,
head_refisfeature/login, so the group isCI-feature/login. A second push to the same branch produces the same string, and the older run is cancelled. - On a push to
main,head_refis empty, so the expression falls back torun_id. The group is something likeCI-8123456789. Every run has a different ID, so push runs never match each other and nothing is cancelled.
The fallback to run_id is a deliberate way to opt out. When you want a particular kind of run to be immune to cancellation, give it a name that is unique per run.
Alternative keys for pull requests
head_ref works, but it has a flaw. Two pull requests from different forks can use the same branch name. A common example is two contributors who each open a pull request from a branch called patch-1. Both runs get the group CI-patch-1, and a push to one cancels the other's run.
The pull request number does not have this problem, because it is unique within the repository:
concurrency:
group: ci-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: trueOn a pull request, the group is ci-42. On a push, the pull request number is empty, so the group is ci-refs/heads/main. Now all pushes to main share one group, which is a different behavior from the run_id version. With cancel-in-progress: true, a new push to main would cancel the earlier run. The next chapter section on conditional cancellation shows how to prevent that.
You can also use github.ref alone. For a pull request it is refs/pull/42/merge, which is already unique per pull request and per repository:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}This one key covers branches, tags and pull requests. The cancel-in-progress expression then keeps runs on main from being cancelled. The decision about whether to cancel is separate from the decision about which runs compete.
Using inputs and variables
Manual and reusable workflows have inputs. If a workflow deploys to a chosen environment, you can make each environment its own group:
name: Deploy
on:
workflow_dispatch:
inputs:
environment:
description: Target environment
required: true
type: choice
options: [staging, production]
concurrency:
group: deploy-${{ inputs.environment }}
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to ${{ inputs.environment }}"A deployment to staging can now run at the same time as one to production. Two deployments to production still queue. Repository and organization variables work the same way through vars, for example ${{ vars.DEPLOY_LOCK_NAME }}, which lets you change the group name without editing every workflow file.
Check what your name will actually be
A name that does not evaluate the way you expect fails silently. You get either runs that cancel each other for no reason or runs that never cancel at all. Two habits catch most of these problems:
- Print the values while you design the name. A step that runs
echo "head_ref=${{ github.head_ref }} ref=${{ github.ref }}"shows what each property holds for the event you are testing. - Look at the run page. When a run is cancelled by concurrency, the annotation names the group, and you can compare it against the group of the run that replaced it.
steps:
- name: Show concurrency inputs
run: |
echo "workflow=${{ github.workflow }}"
echo "event=${{ github.event_name }}"
echo "ref=${{ github.ref }}"
echo "head_ref=${{ github.head_ref }}"Common mistakes
- Using only a literal name such as
ciin a workflow that runs on both pull requests and pushes. Every run joins one group, so unrelated pull requests cancel each other. - Omitting
github.workflowwhen several workflows use the same expression. Two workflows that both use${{ github.ref }}collide on every branch. - Relying on
github.head_reffor events other than pull requests. It is empty, so the group becomes a bare-or a bare literal and every such run competes. - Putting a value that changes between runs of the same logical task, such as a timestamp, in the name. The strings never match, so nothing is limited.
- Forgetting that names are case-insensitive when you compare branches that differ only by case.
Summary of decisions
| Goal | Group name pattern | Column | Column |
|---|---|---|---|
| Cancel outdated runs per pull request, never cancel pushes | `${{ github.workflow }}-${{ github.head_ref \ | \ | github.run_id }}` |
| One group per pull request, safe against forks | `ci-${{ github.event.pull_request.number \ | \ | github.ref }}` |
| One group per branch or tag for any event | `${{ github.workflow }}-${{ github.ref }}` | ||
| One group per deployment target | `deploy-${{ inputs.environment }}` | ||
| One global lock | `deploy-production` |
Exercise
A workflow named Lint runs on both push and pullrequest. It uses `group: ${{ github.workflow }}-${{ github.headref }} with cancel-in-progress: true`. A developer pushes twice in quick succession to main. What happens to the first run, and how would you change the group so that it behaves as intended?
Show answer
On a push, github.head_ref is empty, so every push to any branch gets the group Lint-. The second push to main cancels the first run, and it would also cancel a push run on a completely different branch. Add a fallback so push runs get a unique group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}. Alternatively, use ${{ github.workflow }}-${{ github.ref }} and set cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}.
Exercise
Two contributors from different forks each open a pull request from a branch named patch-1. Your group is ci-${{ github.head_ref }}. Why is this a problem, and what expression would you use instead?
Show answer
Both pull requests have head_ref equal to patch-1, so they share the group ci-patch-1. A push to one pull request cancels the other's run. Use a value that is unique per pull request, such as ci-${{ github.event.pull_request.number }}, or use github.ref, which is refs/pull/<number>/merge for pull requests.