Sub topic 16
Preventing overlapping deployments
This chapter assumes you can read a GitHub Actions workflow and have a deploy-on-merge workflow like the one in the previous chapters. It covers how to stop two deployments from running against the same org at once.
Why overlap is a problem
Two merges to main a few minutes apart start two workflow runs. Both authenticate to the same org and both call sf project deploy start. Salesforce deploys one package at a time per org, and a second deployment queues or fails with a lock error. Even when it succeeds, the order may not match the order of the merges. An older commit can finish last and overwrite newer metadata.
Using a concurrency group
GitHub Actions has a concurrency key for this. All runs that share the same group name are limited to one running at a time.
concurrency:
group: deploy-production
cancel-in-progress: falseWith cancel-in-progress: false, a new run waits for the current run to finish. With true, a new run cancels the one in progress. For deployments, use false. Cancelling a run partway through a deployment can leave the org in a half-updated state or leave a validation incomplete.
Limits of the queue
GitHub keeps at most one running and one pending run per group. If a third run arrives while one is running and one is pending, the older pending run is cancelled and replaced by the newest. The commits are not lost. The newest run deploys the full state of main, so it includes the skipped changes. This is safe for full deployments. It is not safe for delta deployments, because a delta covers only the changes between two revisions, so the skipped changes would never be deployed. If you use sfdx-git-delta, compare against the last successfully deployed commit rather than the previous commit.
Choosing the group name
The group name decides what is serialized. Name it after the thing you are protecting, which is the target org.
| Group name | Effect |
|---|---|
| `deploy-production` | One production deployment at a time. Other orgs are unaffected. |
| `deploy-${{ github.ref }}` | One deployment per branch. Two branches can deploy to the same org at once. |
| `deploy-${{ vars.ORG_NAME }}` | One deployment per org named by a variable. |
Use one group per org, not per branch. The lock you care about is on the org.
Validation runs are different
Pull request validations are check-only, so they do not change the org. They can still compete with a real deployment for the org's deployment slot. A common setup uses cancelling for validations and queueing for deployments:
concurrency:
group: validate-${{ github.event.pull_request.number }}
cancel-in-progress: trueHere a new push to the same pull request cancels the outdated validation. This saves runner time and does not risk org state, since a check-only deployment never commits.
Exercise
Your production workflow uses cancel-in-progress: true. Two merges arrive 30 seconds apart. What can go wrong, and what do you change?
Show answer
The second run cancels the first while it may be partway through a deployment, which can leave the org partly updated. Set cancel-in-progress: false so the second run waits for the first to finish.
Exercise
Why is a queue that drops older pending runs risky when you deploy with sfdx-git-delta?
Show answer
A delta contains only the changes between two revisions. If a pending run is dropped, its changes are not in the next run's delta, so they are never deployed. Compute the delta from the last successfully deployed commit, or deploy the full source.