Sub topic 9
Serializing deployments without cancelling them
This chapter assumes you can read a GitHub Actions workflow (on, jobs, steps) and use ${{ }} expressions with the github context. It also assumes you know the concurrency key takes a group and a cancel-in-progress setting. Here you will use those two settings for the opposite goal from pull request CI: deployments should queue up and never be interrupted.
Why deployments should not be cancelled
Cancelling a superseded test run is safe. The tests only read your code, so stopping them halfway leaves nothing behind. A deployment is different. It changes a live system, and killing it midway can leave you with:
- a database migration applied but the application code not yet updated
- half of the files uploaded to a bucket
- a Terraform state lock that was never released
- a release in an unknown state that someone must inspect by hand
You still don't want two deployments to the same target running at the same time, because they would race each other. The goal is to serialize them: one at a time, never interrupted.
The configuration
Set cancel-in-progress to false and give every deployment to the same target the same group name.
name: Deploy
on:
push:
branches: [main]
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploy to production"Because the group name is a fixed string, every run of this workflow joins the group deploy-production. With cancel-in-progress: false, a newer run never stops the one that is already running. It waits instead.
false is also the default. Writing it explicitly documents your intent, and a later edit is less likely to flip it by accident.
What happens to the queue
A concurrency group holds at most one running run and one pending run. The queue is not a long line. Suppose three pushes to main arrive while the first deployment is still running.
The rules are:
- Run A is running. It is never interrupted.
- Run B arrives and becomes pending.
- Run C arrives. B is cancelled, because a group has only one pending slot and the newest run takes it.
- When A finishes, C starts.
Run B never deploys. It shows as cancelled in the Actions tab, even though cancel-in-progress is false. That setting only protects runs that have already started. Pending runs are always replaced by newer ones.
Is it safe to skip intermediate runs?
Usually yes, and the reason is that each run checks out its own commit. Commit 3 contains the changes from commit 2, so deploying commit 3 gives the target the newest state. Skipping B loses no code.
It becomes a problem when a deployment does something per commit rather than "make the target match this commit". Examples:
- a workflow that posts a notification for each commit
- a job that applies only the diff between two commits
- a release pipeline that publishes one artifact version per commit
If every run must execute, a concurrency group is the wrong tool. Consider a different approach, such as a dedicated queueing service, or make the deployment steps idempotent (running them twice or skipping some produces the same final state). Use concurrency only when "latest wins" is acceptable.
Separate groups for separate targets
A single group name for everything makes unrelated deployments wait on each other. Use an expression so that only deployments to the same target compete.
name: Deploy
on:
push:
branches: [main]
workflow_dispatch:
inputs:
environment:
description: Target environment
type: choice
options:
- staging
- production
default: staging
concurrency:
group: deploy-${{ inputs.environment || 'production' }}
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploying to ${{ inputs.environment || 'production' }}"On a manual run, inputs.environment holds the chosen value, so staging and production deployments get different groups and can run at the same time. On a push, inputs.environment is empty, so the || fallback sends the run to deploy-production.
Two details about group names:
- Group names are case-insensitive.
Deploy-Productionanddeploy-productionare the same group. - The group name is evaluated when the run is created, so you can only use contexts that are available at that point, such as
github,inputsandvars. Contexts that exist only while a job runs, such assteps, are not available.
Combining testing and deploying
Workflow-level concurrency applies to the whole run, including the test job. If tests and deployment live in one workflow, a queued run waits before any job starts. Often you want the opposite: tests start immediately, and only the deployment waits for its turn. Put the concurrency block on the deploy job.
name: Test and deploy
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"
deploy:
needs: test
runs-on: ubuntu-latest
concurrency:
group: deploy-production
cancel-in-progress: false
steps:
- uses: actions/checkout@v4
- run: echo "Deploy to production"Now the tests for every push run in parallel. Only the deploy jobs compete for deploy-production. A deploy job that is waiting for the group shows as pending, and a newer waiting deploy job replaces it, the same way whole runs do.
The trade-off is that a run can spend its test time and then have its deploy job replaced. That wastes some compute, but it means a failing test is reported quickly. Choose job-level scope when you want fast feedback, and workflow-level scope when the whole run should wait.
Mixing the two policies
You can cancel outdated runs on pull requests and still protect deployments by using different groups and different settings in the same repository. The pull request workflow uses a group keyed by branch with cancel-in-progress: true. The deployment workflow uses a fixed group with false. They don't interfere because their group names differ. Group names are scoped to the repository, and a workflow's group name is not automatically prefixed with the workflow name. If two workflows use the same literal group, they compete with each other. That is what you want when two workflows deploy to the same target, and a bug when it happens by accident.
Edge cases
- Cancelling by hand. A person can still cancel a running deployment from the UI, the CLI or the API. Concurrency settings only control automatic cancellation.
- Approval gates. If the job targets an environment with required reviewers, the run waits for approval. Check how your queue behaves while a run is waiting, because a waiting run can hold the group's running slot while a newer run takes the pending slot. Test this with a real environment before relying on it.
- Failed deployments. A failed run finishes and frees the slot. The next pending run starts normally.
- Long queues are not possible. If you need to keep a record of every skipped commit, log it elsewhere. The cancelled run entries in the Actions tab are your only trace.
Summary of the settings
| Goal | `group` | `cancel-in-progress` |
|---|---|---|
| Serialize deployments to one target | Fixed name such as `deploy-production` | `false` |
| Serialize per environment | `deploy-${{ inputs.environment }}` | `false` |
| Cancel outdated pull request tests | Workflow plus branch | `true` |
Exercise
Three commits are pushed to main within a minute. The workflow uses group: deploy-production with cancel-in-progress: false, and each deployment takes five minutes. Which runs deploy, which is cancelled, and why?
Show answer
The first run starts immediately and finishes, because a running run is not cancelled when cancel-in-progress is false. The second run becomes pending, then is cancelled when the third run arrives, because a group holds only one pending run and the newest takes it. The third run deploys after the first finishes. Commit 3 contains the changes from commit 2, so the final state is still correct as long as the deployment applies the whole checked-out commit.
Exercise
Your workflow has a test job and a deploy job. You want tests to start for every push immediately, but deployments to production to run one at a time. Where do you put the concurrency block, and which value of cancel-in-progress do you use?
Show answer
Put the concurrency block on the deploy job, with a fixed group such as deploy-production and cancel-in-progress: false. The test job has no concurrency block, so it is never delayed.