Sub topic 13
Pitfalls: reusable workflows and colliding group names
This chapter assumes you can read workflow YAML, know what concurrency, group and cancel-in-progress do, and can build group names from the github context. Both pitfalls below come from the same cause: two runs end up in one group when you did not intend it.
Group names are shared more widely than you think
A concurrency group is scoped to the repository. Any workflow or job in the repository that resolves to the same string competes for the same slot. GitHub also compares group names without regard to case, so Deploy and deploy are one group.
Three collisions come up often:
- Constant names across workflows. Two workflows both using
group: cicancel or queue each other, even though they test different things. - Names without a ref. A group such as
group: buildmakes every branch and pull request compete. A push to one branch can cancel a run on another. - Workflow-level and job-level overlap. If a workflow sets
group: stagingand one of its jobs also setsgroup: staging, the job competes with its own workflow run.
The cheapest defence is a naming convention. Start the name with what the group protects, then add the ref or other identifier that limits who competes.
Reusable workflows and `github.workflow`
A reusable workflow is a workflow with the workflow_call trigger, which another workflow runs through uses:. Inside it, the github context describes the caller. In particular, github.workflow holds the caller's workflow name, and github.ref holds the caller's ref.
So a common group expression resolves to the same string on both sides:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: trueIf the caller and the called workflow both use this exact block, the called run lands in the group the caller already holds. The called run can then be cancelled, or left pending behind the run that started it. You see a reusable workflow that is cancelled or stuck for no clear reason.
The fix is to give each level its own prefix:
# .github/workflows/ci.yml (caller)
name: CI
on:
pull_request:
concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
uses: ./.github/workflows/build.yml# .github/workflows/build.yml (called)
name: Build
on:
workflow_call:
concurrency:
group: build-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Compile"Another option is to keep concurrency only in the caller. Then the reusable workflow stays free of scheduling rules, and each caller decides how its runs compete.
Checking for collisions
When a run is cancelled and you did not expect it, work through these checks:
- Open the cancelled run and read its resolved group name in the run's logs or annotations.
- Search the repository for other
concurrency:blocks that could produce the same string. - Check whether the run is a called workflow, and whether its group uses
github.workfloworgithub.ref, which come from the caller. - Check for a missing ref. A group without
github.reforgithub.head_refis shared by every branch.
Exercise
A caller workflow and the reusable workflow it calls both declare group: ${{ github.workflow }}-${{ github.ref }}. Why can this cause the called run to be cancelled or stall, and how do you fix it?
Show answer
Inside a reusable workflow, github.workflow and github.ref come from the caller, so both blocks resolve to the same string. The called run then competes with the caller's own run for one group. Fix it by giving each level a distinct prefix, such as ci- in the caller and build- in the called workflow, or by declaring concurrency only in the caller.