Sub topic 10
Concurrency at the job level
This chapter assumes you can read a workflow file (on, jobs, steps), know how workflow-level concurrency with group and cancel-in-progress works, and can write ${{ }} expressions with the github context. Here you will apply the same key to a single job and decide when that is better than workflow scope.
Why scope concurrency to a job
Workflow-level concurrency treats the whole run as one unit. If a newer run enters the group, the older run is cancelled or made to wait, including every job in it. That is often too coarse. A typical workflow has cheap jobs (lint, unit tests) that are safe to run in parallel, and one job that touches a shared resource: a staging environment, a test database, a deploy target, a license-limited service.
Job-level concurrency lets only that job compete. The other jobs in the run are not affected by the group.
Syntax
Put concurrency under the job. It takes the same two keys as at workflow level.
name: CI
on:
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Lint runs for every push, no limits"
integration:
runs-on: ubuntu-latest
concurrency:
group: integration-env-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Run integration tests against a shared environment"
- name: Release environment
if: ${{ cancelled() }}
run: echo "Clean up after a cancelled run"Here lint has no group, so two overlapping runs lint at the same time. integration joins a group named after the branch ref. If a second run for the same ref reaches that job while the first is still executing it, the first job is cancelled and the second takes over.
How the group behaves for a job
The rules are the ones you already know, applied per job:
- A group allows one running job and one pending job.
- A newer pending job replaces an older pending job, which is cancelled.
- With
cancel-in-progress: true, the running job is cancelled when a newer job enters the group. - With
cancel-in-progress: false, the running job finishes and the pending job waits.
A job that is waiting on its group has not started, so it does not occupy a runner and does not run any steps.
Group names are shared across the repository
Concurrency groups are not tied to a workflow file or to a run. They are scoped to the repository, and they are compared without regard to case. Two different workflows that use the group integration-env-main compete with each other, and so do Deploy and deploy.
That is useful when you want a nightly workflow and a pull request workflow to share one test environment. It is a hazard when two unrelated jobs pick the same short name by accident. Use a prefix that names the resource (staging-db-, deploy-production) rather than a generic word like test.
Which contexts are available
In a job-level concurrency, expressions can read more than at workflow level. You can use github, inputs, vars, needs, strategy and matrix. This allows one group per matrix entry:
name: Matrix integration
on:
push:
branches: [main]
pull_request:
jobs:
integration:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [staging-eu, staging-us]
concurrency:
group: ${{ matrix.environment }}-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- run: echo "Testing ${{ matrix.environment }}"Each matrix leg gets its own group, such as staging-eu-refs/heads/main. A new run cancels only the legs targeting the same environment and ref. Without matrix.environment in the name, all legs of the same run would share one group, and they would cancel each other.
Choosing between workflow and job scope
| Question | Workflow-level | Job-level |
|---|---|---|
| What is cancelled or queued? | The entire run | Only the job in the group |
| Good for | Superseded pull request runs, "latest commit only" checks | One job that uses a shared resource |
| Matrix support in the group name | No `matrix` context | Yes, one group per leg |
| Effect on cheap jobs | They are cancelled too | They run normally |
| Risk | Cancelling more than intended | Partial runs where some jobs finished and one was cancelled |
If every job in the workflow would be wasted once a newer commit arrives, use workflow scope. If only one job is guarding a resource, use job scope.
Dependencies and partially cancelled runs
A job-level cancellation leaves the rest of the run in an odd state. Suppose integration is cancelled by a newer run.
- Jobs that do not depend on it keep going and finish normally.
- Jobs that list it in
needsare skipped by default, because their dependency did not succeed. - A downstream job with
if: ${{ always() }}still runs, so use that only when the job is safe to run after a cancellation.
A job-level cancellation can therefore leave a run showing a mix of passed, cancelled and skipped jobs. Plan for this if a branch protection rule requires specific jobs to pass.
Cleaning up after a cancelled job
When a job holds a shared resource, cancellation must release it. Steps are normally skipped once a job is cancelled, but a step with if: ${{ cancelled() }} or if: ${{ always() }} still runs. The earlier example uses cancelled(). The always() condition is broader: it also runs after success or failure, which is usually what you want for releasing a lock.
name: Shared environment tests
on:
workflow_dispatch:
jobs:
integration:
runs-on: ubuntu-latest
concurrency:
group: shared-env
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
- name: Reserve environment
run: echo "Reserve the environment"
- name: Run tests
run: echo "Run tests"
- name: Release environment
if: ${{ always() }}
run: echo "Release the environment whatever happened"Cleanup runs in a limited window after the cancel request, so keep it short and idempotent. Cleanup that makes network calls to a slow service may be cut off. Design the resource so that a leaked reservation expires on its own.
Common errors
- Same group at workflow and job level. If the workflow declares group
ci-mainand one of its jobs declaresci-maintoo, the job competes with its own run. The job can be cancelled by its own workflow, or wait forever on it. Use different names for different scopes. - Group name too coarse.
group: integrationon a job withcancel-in-progress: truecancels the integration job of every branch and pull request. Addgithub.refor a pull request number to the name. - Group name too fine. Including
github.run_idmakes every group unique, so nothing is ever limited. This is the intended fallback in the pull request pattern, but it is a bug anywhere else. - Cancelling a job that must finish. A deployment job should use
cancel-in-progress: false, so a running deployment is never interrupted and a newer one waits its turn. - Expecting a queue of more than one. If three runs reach a job in the same group, only the oldest running job and the newest pending job survive. The middle one is cancelled, even with
cancel-in-progress: false. - Using a context that does not exist in that scope.
matrixworks in a job-level group but not at workflow level. An expression that references an unavailable context makes the workflow file invalid.
Summary of decisions
- Put
concurrencyunder the job when only that job uses a shared resource. - Build the group from the resource name plus a context such as
github.ref, so that only related jobs compete. - Use
cancel-in-progress: truefor disposable test work andfalsefor work that must finish. - Add an
always()orcancelled()cleanup step to release resources. - Check what depends on the job through
needs, because a cancelled job skips its dependents.
Exercise
A workflow has jobs lint, unit and e2e. Only e2e uses a shared test environment. You add concurrency: { group: e2e, cancel-in-progress: true } to the e2e job. Two developers push to different feature branches at nearly the same time. What happens to each developer's e2e job, and how would you change the group so that only pushes to the same branch compete?
Show answer
Both e2e jobs use the group e2e, so the newer one cancels the older one even though the branches are unrelated. The lint and unit jobs are unaffected, because they have no group. To limit competition to one branch, include the ref in the name, for example group: e2e-${{ github.ref }}. If the two branches really share one environment and must not overlap, keep the shared name but set cancel-in-progress: false so the running job finishes first.