Sub topic 13
Pitfalls: reusable workflows and colliding group names
This chapter assumes you can write workflows with on, jobs and steps. It also assumes you know the concurrency key with group and cancel-in-progress, and that you can read ${{ }} expressions and the github context. Most concurrency bugs come from one cause: two runs or jobs that you consider unrelated end up with the same group name. The result is a cancelled run, a stuck job, or a deployment that never starts.
How group names collide
A concurrency group is identified only by its evaluated name. GitHub does not look at which workflow, job, or event produced the name. Three rules cause most collisions:
- Group names are scoped to the repository. Any workflow or job in the repository that evaluates to the same string joins the same group.
- Group names are case-insensitive.
Prodandprodare the same group. - The name is evaluated at run time. A context value that is empty, or the same for many runs, can merge groups you meant to keep apart.
| Group expression | Collides when | Better choice | Column | Column |
|---|---|---|---|---|
| `deploy` in two workflows | Both workflows run at once, and one cancels or delays the other | Use one name on purpose, or add the workflow name | ||
| `${{ github.head_ref }}` | Forks use the same branch name, such as `patch-1`, or the value is empty on `push` | `${{ github.event.pull_request.number \ | \ | github.run_id }}` |
| `${{ github.workflow }}` inside a reusable workflow | It resolves to the caller's workflow name | A literal prefix such as `build-` | ||
| A job-level name without `matrix` values | Every matrix leg shares one group | Add `${{ matrix.os }}` or similar |
Reusable workflows use the caller's context
A reusable workflow is a workflow with the workflow_call trigger, started from a job in another workflow with uses:. Inside the called workflow, the github context describes the caller's run. github.workflow is the caller's workflow name, not the name of the file you are editing.
That surprises people who copy a concurrency block into a reusable workflow:
# .github/workflows/ci.yml (the caller)
name: CI
on:
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
uses: ./.github/workflows/build.yml# .github/workflows/build.yml (the called workflow)
name: Build
on:
workflow_call:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building"In build.yml, github.workflow evaluates to CI. Both files compute the same group name, for example CI-refs/pull/42/merge. The caller holds the group while it waits for the called workflow to finish. The called workflow asks for the same group. Neither can proceed.
GitHub detects this and cancels the called workflow. The run's annotation reports a deadlock for the concurrency group between the top-level workflow and the called job. If you see that message, compare the group expressions in the caller and the callee.
Fix 1: use a literal prefix in the called workflow
Do not rely on github.workflow in a reusable workflow. Give the group a prefix that belongs to the called workflow:
name: Build
on:
workflow_call:
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building"The group is now build-refs/pull/42/merge. It differs from the caller's group, so there is no deadlock.
Fix 2: let the caller decide
A reusable workflow does not know how many times it will be called. If a caller invokes it twice, both calls evaluate the same group and compete. Pass a distinguishing value through an input:
# .github/workflows/build.yml
name: Build
on:
workflow_call:
inputs:
target:
type: string
required: true
concurrency:
group: build-${{ inputs.target }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building ${{ inputs.target }}"# .github/workflows/ci.yml
name: CI
on:
pull_request:
jobs:
build-linux:
uses: ./.github/workflows/build.yml
with:
target: linux
build-windows:
uses: ./.github/workflows/build.yml
with:
target: windowsThe two calls now use build-linux-... and build-windows-..., so they run side by side. Before this change, one of them would have been cancelled or left pending behind the other.
Branch names are not unique
A common pull request group is ${{ github.workflow }}-${{ github.head_ref }}. github.head_ref is the name of the source branch. Two problems follow:
- Pull requests from different forks can use the same branch name. Many contributors open pull requests from a branch called
mainorpatch-1. Their runs share a group, so one contributor's push cancels another contributor's run. - On
pusheventshead_refis empty. All pushes then share a group namedCI-, and each push cancels the previous one across every branch.
The chapter's first example avoids the second problem with github.run_id as a fallback. The run ID is unique, so push runs never share a group. To avoid the fork problem, key on the pull request number:
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"Pull request numbers are unique within a repository. The || operator returns the first truthy value, so push runs fall back to the run ID.
Shared names across workflows
Using the same group in two workflows is sometimes the goal. A deploy-production group in both a release workflow and a hotfix workflow ensures only one deployment runs. It is a bug when it happens by accident. Typical accidents:
- A short generic name like
deployorbuildcopied into several workflows. - A group with
cancel-in-progress: trueshared with a workflow that should never be cancelled. A cheap lint run can cancel a deployment this way. - Mixed settings in one group. Each run's own
cancel-in-progressvalue applies when it arrives and the group is busy. A run withtruecancels the running one. A run withfalsewaits. The behavior depends on arrival order, which makes it hard to predict.
Keep one rule: every workflow or job that uses a group should use the same cancel-in-progress value for it. Put the workflow name or purpose in the group name, unless you intend to share it.
Pending runs are replaced, even without cancel-in-progress
A group holds at most one running and one pending run. With cancel-in-progress: false, a new run waits, but it replaces any older pending run in that group. The replaced run is cancelled with a message that a higher priority waiting request exists.
This matters when a colliding name puts many runs in one group. If five runs of different kinds all land in deploy, one runs, one waits, and three are cancelled. They were not superseded by a newer version of the same work, so the cancellation is wrong. Collisions turn a safe queue into silent loss of runs.
Job-level collisions
Job-level concurrency has the same issue at a smaller scale. A job group name without a matrix value is identical for every leg:
name: Matrix tests
on:
pull_request:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
concurrency:
group: test-${{ matrix.os }}-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Testing on ${{ matrix.os }}"With test-${{ github.ref }} alone, the three legs would start in the same group. Only one would run, one would wait, and the third would be cancelled. Adding matrix.os gives each leg its own group.
Job-level groups may use the github, inputs, vars, needs, strategy and matrix contexts. Workflow-level groups may use github, inputs and vars. You cannot use env or secrets in a group name. If you need a value from a previous job, put the group on the job and read it through needs.
A short checklist
Run through these questions when you review a concurrency block:
- Does the group expression evaluate to a different string for runs that should not affect each other?
- Does the group evaluate to the same string only for runs that should replace each other?
- Could any context value in the expression be empty on some events?
- If this is a reusable workflow, does the name avoid
github.workflowand include something specific to this workflow or its inputs? - Does any other workflow or job in the repository evaluate to the same name, ignoring case?
- Do all users of the group agree on
cancel-in-progress?
Exercise
A reusable workflow lint.yml has concurrency: group: ${{ github.workflow }}-${{ github.ref }} with cancel-in-progress: true. The caller ci.yml (named CI) uses the same expression at workflow level. Every call to lint.yml is cancelled with a deadlock message. Why does this happen, and how do you fix it without removing concurrency from the caller?
Show answer
Inside a called workflow, github.workflow is the caller's name, so lint.yml evaluates to the same group as ci.yml (for example CI-refs/pull/42/merge). The caller holds the group while waiting for the called job, and the called job requests it too, so GitHub detects a deadlock and cancels the called workflow. Fix it by giving lint.yml its own group, such as lint-${{ github.ref }}, or by adding an input like inputs.target to the name.
Exercise
Your pull request workflow uses group: ${{ github.workflow }}-${{ github.head_ref }}. A contributor reports that their runs are cancelled whenever another contributor pushes. Both contributors open pull requests from a branch named patch-1 in their own forks. What is wrong, and what group expression avoids the problem on both pull_request and push events?
Show answer
github.head_ref is only the branch name, so pull requests from different forks with the same branch name share a group and cancel each other. On push events it is empty, so those runs would also share one group. Use ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}. The pull request number is unique per repository, and push runs fall back to the unique run ID.