Guide · CI for Agent-Written Code
GitHub Actions concurrency: groups, cancel-in-progress and queues
One running run per group, one waiting, and a choice about the rest. The settings that stop stale runs without cancelling the verdicts you need.
Shipped by Agents Editors · Checked against GitHub's documentation on
What the concurrency key does
concurrency puts workflow runs, or single jobs, into a named group. GitHub then lets at most one run in that group be in progress at a time, across the whole repository. A group name can be a fixed string or an expression, and names are case insensitive: prod and Prod are the same group.
What happens to the next run depends on two settings:
| Setting | Run already in progress | Run already waiting |
|---|---|---|
Default (no cancel-in-progress, queue: single) | Keeps running; the new run waits as pending | Cancelled and replaced by the new run |
cancel-in-progress: true | Cancelled; the new run starts | Cancelled and replaced by the new run |
queue: max | Keeps running | Kept: up to 100 runs wait and start in turn |
The second column surprises people: even without cancel-in-progress, a group holds only one waiting run. Push three commits while one run is going and the middle one is cancelled before it starts. queue: max is the only setting that keeps every run, and it cannot be combined with cancel-in-progress: true; GitHub rejects that workflow as invalid.
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: truePut concurrency at the top of the workflow to group whole runs, or under jobs.<job_id> to group one job. At workflow level the group expression can only read the github, inputs and vars contexts. At job level it can also read needs, strategy and matrix.
Concurrency groups are not the concurrent job limit
"GitHub Actions concurrency" also gets used for a second, unrelated thing: how many jobs your account can run at the same time on GitHub-hosted runners. That limit is set by your plan and runner type, not by anything in a workflow file. Jobs over the limit queue until a slot frees up. GitHub lists the current numbers per plan on its Actions limits page, says they are subject to change, and can raise them through a support ticket.
A third knob sits inside a single matrix: jobs.<job_id>.strategy.max-parallel caps how many legs of that matrix run at once. By default GitHub runs as many as runner availability allows.
concurrency: which runs of the same kind may overlap. Set in the workflow.strategy.max-parallel: how many legs of one matrix run at once. Set in the workflow.- Concurrent job limit: how many jobs the whole account runs at once. Set by the plan.
The three interact. Stale runs that nobody cancels still occupy job slots, so on a busy repository a missing concurrency block can make the run you care about wait in the account queue behind runs for commits that no longer matter.
Pull requests: cancel the superseded run
On a pull request, only the newest commit's verdict decides the merge. A run for a commit that has since been replaced is spending runner time on an answer nobody will read. This is where cancel-in-progress: true belongs, and it matters more when coding agents open the pull requests: an agent that pushes a fix, reads the failure and pushes again can start several runs in the time a person would start one.
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}github.workflowkeeps the group to this workflow. Group names are shared by every workflow in the repository, so a group named only after the branch lets your lint workflow cancel your test workflow.- The pull request number groups every push to one pull request together.
github.refis the fallback for events that have no pull request, such as a push tomain. cancel-in-progressis an expression, so it can be true for pull requests and false everywhere else.
If you group on github.head_ref instead, remember it is only set on pull_request events. Give it a fallback, as in ${{ github.head_ref || github.run_id }}, or runs triggered by other events will fail to evaluate the group.
A cancelled run is not a pass. Required status checks are read from runs on the pull request's latest commit, so cancelling the run for an older commit does not block the merge, but a cancelled run on the latest commit does not count as green either. Re-run it rather than merging past it.
The default branch: decide whether every commit needs a verdict
With the configuration above, pushes to main never cancel a running run, but the default one-waiting-run rule still applies. Merge three pull requests in quick succession and the run for the middle merge commit can be replaced before it starts. That commit then has no verdict of its own, which makes it harder to find which merge broke the build.
If every commit on main should be tested, give each one its own group, so nothing on main waits on or replaces anything else:
concurrency:
group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}If you would rather run them one at a time and in order, keep a single group for main and set queue: max on a workflow that never sets cancel-in-progress: true. Runs leave the queue in the order they started waiting on the group, which GitHub notes is not always the order they were triggered.
Deployments: never cancel halfway
Concurrency was built for deployments: two deploys to the same environment at once can leave it in a state neither intended. Group the deploy job by environment, and do not cancel a deploy that has started.
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
concurrency:
group: deploy-production
queue: max
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.shLeave out queue: max if only the newest deploy matters: the default then replaces any waiting deploy with the latest one, and the running deploy still finishes. Putting the group on the deploy job rather than the workflow lets tests for the next commit run while a deploy is still going.
Common mistakes
- A job-level group in a matrix without the matrix in its name. Every leg of the matrix lands in the same group, so the legs wait on or cancel each other. Add the matrix values, for example
group: test-${{ github.ref }}-${{ matrix.os }}-${{ matrix.node }}, or move the group to workflow level. - Cleanup steps written as
if: always().always()also runs when a run is cancelled, and GitHub warns against it for any step that could suffer a critical failure, because the workflow may hang until it times out. For "run whether the job passed or failed", useif: ${{ !cancelled() }}. - Fixed group names reused across workflows. A group called
ciin two workflow files is one group: each workflow's runs cancel the other's. - Expecting
concurrencyto make a run faster. It removes runs that no longer matter; the run that is left takes as long as it did before. - Reading a cancelled run as a failure. In the run list and on the pull request, cancelled shows as its own state, not a failed test.
A starting configuration
For most repositories that run tests on pull requests and pushes to main, this is a reasonable place to start. Adjust the main part to match whether you need a verdict per commit, and keep deploy jobs in their own group.
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.sha || github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
- name: Upload test report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: test-report
path: reports/Concurrency settles which runs happen. How long the remaining run takes is a separate question of caching, parallelism and runners; the posts below cover those.
Questions
Does cancel-in-progress cancel runs from other branches?
Only if they share a group. Include github.ref or the pull request number in the group name and each branch or pull request gets its own group, so a push to one never cancels another.
Why was my pending run cancelled when I never set cancel-in-progress?
A concurrency group holds one waiting run by default. When a newer run joins the group, the waiting one is cancelled and replaced. Set queue: max to keep up to 100 waiting runs instead.
Can I use queue: max and cancel-in-progress together?
No. GitHub treats that combination as a workflow validation error, because one asks to keep waiting runs and the other to cancel running ones.
Should concurrency go on the workflow or the job?
On the workflow to treat a whole run as one unit, which suits CI on pull requests. On the job when only part of the run must not overlap, such as a deploy, so the rest of the workflow can still run in parallel.
How many jobs can my account run at the same time?
That is the concurrent job limit, set by your GitHub plan and runner type rather than by the concurrency key. Check GitHub's Actions limits page for the current numbers; GitHub Support can raise them.



