Sub topic 13
Preventing overlapping deploys
This chapter assumes you can read a GitHub Actions workflow, know what a Deployment rollout is, and have a deploy job like the one built earlier in this tutorial. Here you will stop two deploy jobs from running against the same cluster at the same time.
Why overlapping deploys are a problem
A push-based pipeline starts a new run for every push to main. If two commits land a minute apart, both runs reach the deploy job. Both call kubectl set image on the same Deployment.
The cluster accepts both updates, but the outcome is hard to predict:
- A slow older run can finish after a newer one and set the image back to the older commit.
kubectl rollout statuscan report on a rollout that a second run has just replaced.- Two runs that both edit the Deployment make the logs and rollout history hard to read.
GitHub Actions has a feature built for this: concurrency groups.
Concurrency groups
A concurrency group is a string you choose. GitHub allows only one running job or workflow per group at a time. Anything else that uses the same group string waits.
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
concurrency:
group: deploy-production
cancel-in-progress: false
steps:
- run: echo "deploying"The group value decides which runs compete. Every run that sets deploy-production joins the same queue. Runs with other group names are not affected.
Queue or cancel?
The cancel-in-progress setting controls what happens when a new run arrives while another holds the group.
| Setting | Behavior | Good for |
|---|---|---|
| `false` | The running job finishes. The new job waits. | Deploys, where stopping halfway is risky |
| `true` | The running job is cancelled. The new job starts. | Tests and builds, where only the newest result matters |
Use false for the deploy job. A cancelled kubectl step can leave a rollout half finished. With false, the running deploy completes first.
GitHub keeps only one pending job per group. If three runs arrive while one is deploying, the oldest waiting run is cancelled and only the newest waits. For deploys this is usually what you want. The newest commit is the one that should end up in production.
Where to place the setting
You can set concurrency at the workflow level or on a single job.
- At the workflow level, the whole workflow is serialized, including the build job.
- At the job level, builds run in parallel and only the deploy job queues.
Job level is the better fit here. Builds are safe to run side by side because each image has its own SHA tag.
Common mistakes
- Different group names per branch or environment by accident. If staging and production share one group, they block each other. If you build the name from
github.ref, runs from different refs no longer share a lock. - Using
cancel-in-progress: trueon the deploy job. This can interruptkubectlin the middle of a rollout. - Assuming concurrency orders old runs. The queue holds only the newest pending run, so skipped commits are never deployed on their own. Each deploy sets the image to the commit of its own run, so that is safe.
Exercise
A workflow has a deploy job with group deploy-production and cancel-in-progress: false. Run A is deploying. Runs B and C start, in that order, before A ends. Which runs end up deploying, and in what order?
Show answer
Run A finishes first. Run B is cancelled when run C arrives, because only one run can be pending per group. Run C then deploys. The order is A, then C.
Exercise
Why is it better to put concurrency on the deploy job than on the whole workflow?
Show answer
At workflow level the build job is also serialized, which slows the pipeline for no benefit. Builds are independent because each image is tagged with its own commit SHA. Only the deploy job changes shared cluster state, so only it needs the lock.