I merged five pull requests to main in about two minutes. Five pushes, five deploy runs. Two days later a teammate asked why the worker was still running last week's code.
The Actions tab told the story: run 1 green, run 5 green, and runs 2, 3 and 4 grey with a little "cancelled" badge. I had never set cancel-in-progress. I had specifically left it off because I wanted every deploy to run.
That's the part of GitHub Actions concurrency that bites people: a concurrency group cancels queued runs by default. cancel-in-progress only controls whether the running one dies too.
TL;DR
- A GitHub Actions concurrency group holds one running and one pending job or workflow. That's it. There is no queue behind them.
- When a new run joins a group that already has a pending run, the older pending run is canceled, with or without
cancel-in-progress. -
cancel-in-progress: trueadds one more behavior: it also cancels the run that's currently executing. - This is harmless for "deploy whatever is latest" jobs and dangerous for anything that only processes its own changes (diff-based deploys, per-input dispatches, matrix legs sharing a group).
- Fix it by scoping the group key (
github.ref,matrix.*,inputs.*) and by making deploys diff against the last successful deploy, notgithub.event.before.
How does GitHub Actions concurrency actually work?
GitHub Actions concurrency gives each group exactly two slots: one running, one pending. A third arrival doesn't wait in line. It takes the pending slot and the run that was sitting there gets canceled.
Here's the workflow I had:
name: deploy
on:
push:
branches: [main]
concurrency:
group: production
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Work out which services changed
id: changed
run: |
git diff --name-only ${{ github.event.before }} ${{ github.sha }} > changed.txt
cat changed.txt
- name: Deploy changed services
run: ./scripts/deploy-changed.sh changed.txt
In my head, concurrency: production meant "one deploy at a time, the rest wait their turn." What it actually means is "one deploy at a time, and at most one waiting."
The timeline of those five merges:
| Push | Commit touched | What happened to its run |
|---|---|---|
| 1 | api/ |
Started immediately, deployed api |
| 2 | worker/ |
Pending, then canceled by push 3 |
| 3 | web/ |
Pending, then canceled by push 4 |
| 4 | docs/ |
Pending, then canceled by push 5 |
| 5 | api/ |
Ran after push 1 finished, deployed api |
Three of five runs canceled. Nobody asked for that.
Why didn't the cancellation matter for my code but matter for my deploy?
Commit 5 contains everything from commits 2, 3 and 4, so the code on main was fine. The bug was in how each run decided what to deploy.
github.event.before on a push event is the head of the branch before that specific push. Run 5's diff covered commit 4 to commit 5 and nothing else. It saw api/ changed, deployed api, and reported success.
The worker change from push 2 and the web change from push 3 lived only in runs that were canceled. Their diffs never ran anywhere. Green checkmark, stale production.
If your deploy rebuilds everything from the latest commit, skipped runs cost you nothing. That's why most people never notice this. The moment a run only handles "my changes," the one-pending-slot rule silently drops work.
How do I reproduce the GitHub Actions concurrency cancel behavior?
Push several commits faster than one run takes to finish, each as its own push. You'll see every run between the first and last canceled.
# .github/workflows/concurrency-test.yml
name: concurrency-test
on: push
concurrency:
group: test-group
jobs:
slow:
runs-on: ubuntu-latest
steps:
- run: echo "run for ${{ github.sha }}" && sleep 120
for i in 1 2 3 4 5; do
git commit --allow-empty -m "concurrency test $i"
git push
done
Within a couple of minutes you'll have run 1 in progress, runs 2 through 4 canceled, and run 5 pending. Open one of the canceled runs and the annotation says something like "Canceling since a higher priority waiting request for 'test-group' exists." That message is the whole mechanic in one line.
To find this in a real repo:
gh run list --workflow deploy.yml --status cancelled --limit 50
I ran that on my project and found canceled deploy runs going back months. Most were harmless. Two weren't.
Where else does the pending slot eat runs?
Diff-based deploys are the classic case, but the same rule shows up in three other places I've hit.
1. Matrix jobs with a job-level group. Put concurrency: deploy on a matrix job and every leg lands in the same group. One leg runs, one waits, and with three or more legs the middle ones get canceled. Your "deploy to 4 regions" job deploys to 2.
jobs:
deploy:
strategy:
matrix:
region: [us-east, us-west, eu, ap]
concurrency:
group: deploy-${{ matrix.region }} # not just "deploy"
2. workflow_dispatch with different inputs. Dispatch "deploy service A," then B, then C while something is already running. B gets canceled. The group doesn't care that the inputs differ unless the inputs are in the key.
concurrency:
group: deploy-${{ inputs.service }}
3. A group name shared across branches. A bare group: ci means a pull request push can cancel the pending run on main, and vice versa. Workflows on different branches collide because the string is identical.
How should I scope a concurrency group key?
Put everything that makes two runs non-interchangeable into the group key. If two runs would do different work, they belong in different groups.
A few patterns I now use by default:
# One group per workflow per branch. Newer pushes on a PR replace older ones.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# Cancel superseded PR runs, but give every push to main its own group,
# so main never cancels anything.
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
The second one works because github.head_ref only exists on pull request events. On a push to main it's empty, so the key falls back to the unique run_id and every run gets a private group.
One constraint worth knowing: a workflow-level concurrency key can only read the github, inputs and vars contexts. If you need matrix or needs in the key, move concurrency down to the job.
Can I make GitHub Actions run every queued deploy in order?
Not with concurrency alone. It serializes and replaces; it does not keep a FIFO queue. If every single run must execute, you need the work to be safe to skip or a lock outside Actions.
What I changed:
-
Diff against the last successful deploy, not the previous push. After a deploy succeeds, I move a lightweight tag (
deployed/production) to that SHA. The next run diffsdeployed/production..HEAD. Now a canceled middle run doesn't matter, because the run that does execute covers its changes too.
git fetch --tags --force
BASE=$(git rev-parse deployed/production 2>/dev/null || git rev-list --max-parents=0 HEAD)
git diff --name-only "$BASE" "$GITHUB_SHA" > changed.txt
# ...deploy...
git tag -f deployed/production "$GITHUB_SHA"
git push -f origin deployed/production
Keep the single production group. Two deploys racing each other is still worse than a skipped one. The group stays; the job just stopped assuming it sees every push.
Alert on canceled deploys. A tiny scheduled job runs
gh run list --status cancelledand posts anything from the deploy workflow in the last day. If a skip ever matters again, I'll know in hours, not days.
The general rule: under a concurrency group, write each run as "bring production up to date with this commit," never as "apply this commit's changes."
Does GitHub Actions concurrency cancel pending runs without cancel-in-progress?
Yes. A GitHub Actions concurrency group allows one running and one pending run, and any newer run that joins the group cancels the existing pending one, regardless of cancel-in-progress. That setting only decides whether the currently running job is also canceled. If your workflow processes only its own push (diffing against github.event.before, per-input dispatches, matrix legs in one group), the canceled runs' work is silently lost. Scope the group key to everything that makes runs different, and make each run reconcile against the last successful deploy instead of the previous commit.
Written by the developer behind Preterview, an interview prep platform.
Top comments (0)