Sub topic 12
Preventing overlapping deployments
This chapter assumes you can read a workflow file, know what a job and an environment are, and have a deploy job like the one built earlier in this tutorial. It covers how to stop two deployments from running at the same time.
Why overlap is a problem
Two pushes to main a few minutes apart start two workflow runs. If both reach the deploy job together, they can write to the same target at once. An aws s3 sync --delete from an older commit can finish after a newer one and leave the old build live. The deployment can also end up half old and half new.
A concurrency group fixes this. GitHub allows only one running job or workflow per group key at a time.
The concurrency key
You set concurrency on a workflow or on a single job. The group value is a string. Runs that resolve to the same string share a group.
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
concurrency:
group: deploy-production
cancel-in-progress: false
steps:
- run: echo "Deploying"Placing it on the deploy job, not the whole workflow, means builds and tests can still run in parallel. Only the deploy step is serialized.
Queue or cancel
cancel-in-progress decides what happens when a second run arrives while the first is still going.
| Setting | Behavior | Use it for |
|---|---|---|
| `false` (default) | The running job finishes. The new job waits as pending. | Deployments |
| `true` | The running job is cancelled and the new one starts. | Tests and previews |
Cancelling a deployment halfway can leave the target in a broken state, so use false for deploys. Cancelling is fine for CI checks, where only the latest commit matters.
Choosing the group name
The group name controls what is serialized. Use a fixed name for one target, or add a context value so unrelated targets don't block each other.
concurrency:
group: deploy-${{ github.workflow }}-${{ inputs.environment }}
cancel-in-progress: falseWith this key, staging and production deploys run independently, but two production deploys queue. In a reusable workflow, include the environment input in the name. Otherwise a staging run could block a production run.
A common pattern for pull request checks is to group by branch and cancel older runs:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: trueHow it interacts with approvals
A job waiting for environment approval is pending. If the job has a concurrency group, it holds the group's pending slot. A newer run can therefore replace a run that is still waiting for a reviewer. Reviewers then approve the latest commit, not a stale one.
Common mistakes
- Group names that differ by accident. A name containing
${{ github.sha }}is unique for every commit, so nothing ever queues. - Setting
cancel-in-progress: trueon deploys. A cancelledsynccan leave files half uploaded. - Putting concurrency on the whole workflow. Builds then queue behind deployments for no reason.
- Reusing one group name across unrelated targets. Staging deploys end up waiting for production.
Exercise
Your workflow deploys to staging and production using the same group name, deploy, with cancel-in-progress: false. Three pushes arrive in quick succession. What happens to the second push's deploy, and how would you change the group so staging and production don't block each other?
Show answer
The first deploy runs. The second waits as pending, and the third replaces it, so the second is cancelled. Because both environments share the name deploy, a staging run can hold up a production run. Include the environment in the key, for example group: deploy-${{ inputs.environment }}, or use separate fixed names such as deploy-staging and deploy-production.