Sub topic 9
Serializing deployments without cancelling them
This chapter assumes you know basic workflow syntax (on, jobs, steps), common triggers such as push, and the ${{ }} expression syntax. It also assumes you have read the earlier chapters on concurrency groups and cancel-in-progress.
Why deployments need different rules
Cancelling outdated runs suits tests. A newer commit makes an older test result worthless, so stopping it saves time.
Deployments are different. If you cancel a deployment halfway through, it may leave a database migrated but the application not updated, or half of the files uploaded. You want deployments to run one at a time, and you want each one to finish.
Concurrency groups give you this. You set a fixed group name and turn cancel-in-progress off.
The workflow
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"Two details matter here.
- The group name
deploy-productionhas no expressions in it. Every run of this workflow, on any commit, lands in the same group, so only one can run at a time. cancel-in-progress: falseis the default. Writing it out makes your intent clear to the next reader.
What happens when pushes arrive quickly
A group holds at most one running run and one pending run. Suppose three pushes land close together.
- Run 1 starts and holds the group.
- Run 2 arrives. It waits as the pending run.
- Run 3 arrives. It replaces run 2, and run 2 is cancelled.
- When run 1 finishes, run 3 starts.
Run 1 is never interrupted. Run 2 is skipped, but this is usually fine. Run 3 contains run 2's commit, so deploying run 3 deploys the newest code.
When skipping is not acceptable
If every commit must be deployed in order, the one-pending limit is a problem, because intermediate runs are dropped. Concurrency groups cannot queue more than one run. In that case, consider these options:
- Make deployments idempotent, so that deploying the latest commit always gives the right result. Then dropping intermediate runs is harmless.
- Use an external queue, or a deployment tool that tracks ordering.
Separate groups for separate targets
A fixed name serializes everything. If you deploy to several environments, give each its own group so staging does not wait for production.
concurrency:
group: deploy-${{ inputs.environment }}
cancel-in-progress: falseThis works in a workflow triggered by workflow_dispatch that defines an environment input. Runs for staging and production use different groups and can proceed in parallel. Runs for the same environment queue.
Common mistakes
- Including the commit SHA or run ID in the group name. Each run then gets a unique group, so nothing is serialized.
- Sharing a group name with a test workflow that cancels. Group names are shared across workflows in a repository. A cancelling run could cancel your deployment. Keep a distinct prefix such as
deploy-. - Assuming a failed deployment frees nothing. Any finished run, whether it succeeded, failed or was cancelled, releases the group.
Exercise
Runs A, B and C of a workflow use group: deploy-production and cancel-in-progress: false. A is running when B and then C are triggered. Which runs finish, which is cancelled, and why?
Show answer
A finishes because cancel-in-progress: false protects the running run. B is cancelled when C arrives, because a group keeps only one pending run and the newest replaces the older one. C starts after A finishes and runs to completion.
Exercise
A team sets group: deploy-${{ github.sha }} and finds two deployments sometimes run at the same time. What is wrong and how do you fix it?
Show answer
Each commit has a different SHA, so each run gets its own group and nothing is serialized. Use a fixed name such as deploy-production, or a name based on the target environment, with cancel-in-progress: false.