Sub topic 10
Concurrency at the job level
This chapter assumes you can write workflow YAML with on, jobs and steps. It also assumes you know the github context and ${{ }} expressions. You should already know how workflow-level concurrency and cancel-in-progress work.
Why scope concurrency to a job
Workflow-level concurrency applies to the whole run. If a run is cancelled or queued, every job in it is cancelled or queued too. That is often too broad. A workflow may have fast lint and unit test jobs that are safe to run in parallel. The same workflow may also have one job that touches a shared resource, such as a staging database or a test environment that supports one user at a time.
Job-level concurrency lets that one job compete for the resource. The rest of the workflow runs without restriction.
Syntax
The concurrency key goes under the job, with the same group and cancel-in-progress fields you use at workflow level.
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Lint runs freely"
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"The lint job has no concurrency setting, so two overlapping runs execute it side by side. The integration job joins the group integration-env-<ref>. If a newer run reaches that job while an older run's integration job is still going, the older job is cancelled.
How it behaves
The group still allows one running and one pending item. The item is now a job instead of a whole run. The diagram shows two runs reaching the same job-level group.
Other jobs in Run 1 are not cancelled by this. A job that depends on integration through needs will not run normally, because its dependency was cancelled. Independent jobs finish as usual.
Choosing workflow or job scope
| Question | Prefer workflow level | Prefer job level |
|---|---|---|
| Is the whole run obsolete after a new push? | Yes | No |
| Does only one job use a shared resource? | No | Yes |
| Should other jobs keep running? | No | Yes |
| Do you want one group name for the whole pipeline? | Yes | No |
Use workflow level for the common "newer push replaces older run" case. Use job level when a single job needs exclusive access to something and the other jobs are harmless to overlap.
Cleanup and cancellation
A cancelled job needs its cleanup to run even though the job was stopped. A step with if: ${{ cancelled() }} runs when the run is cancelled, so it can release the shared environment. Use if: ${{ always() }} if the cleanup must also run on success and failure. Keep cleanup short, because a cancelled job only has a limited window to finish.
Common mistakes
- Using the same group name at workflow and job level. A workflow and its own job would then compete for one group, and the job can be cancelled or left pending by its own run. Give each a distinct name.
- Leaving out a context in the group name. A fixed name such as
integration-envmakes every branch queue behind the others. Add${{ github.ref }}if branches should not block each other. - Forgetting that
needsdependents are affected. A cancelled upstream job stops downstream jobs.
Exercise
A workflow has lint, unit, and integration jobs. Only integration uses a shared environment. Where should you put concurrency, and what happens to lint and unit when a newer run cancels an older run's integration job?
Show answer
Put concurrency under the integration job, with a group name that includes ${{ github.ref }}. When the newer run arrives, only the older run's integration job is cancelled. Its lint and unit jobs are not affected and finish normally.
Exercise
Why add a step with if: ${{ cancelled() }} to the integration job?
Show answer
When the job is cancelled, normal steps stop. The cancelled() condition lets the step run during cancellation, so it can release the shared environment.