Sub topic 16
Preventing overlapping deployments
This chapter assumes you have a working pull request validation workflow and a deploy-on-merge workflow, as built in the earlier chapters. It covers what goes wrong when two runs target the same org at once, and how to stop it with GitHub Actions concurrency groups.
Why overlap is a problem
Two merges close together start two runs of the deploy workflow. Both authenticate to the same org and both deploy.
Several things can go wrong:
- Out-of-order results. Run B (the newer commit) can finish before run A (the older commit). The org then ends up holding older metadata than
main. - Lock errors and failed runs. Salesforce handles deployments to one org one at a time. A second request can wait in the queue, hit a timeout, or fail with a lock or "in progress" error. The exact behavior depends on what each deployment touches, so don't build your design around it.
- Test interference. Apex tests from two deployments can touch the same records, which causes flaky failures such as
UNABLE_TO_LOCK_ROW. - Wasted time. A pull request validation for a commit that has already been replaced by a newer push still uses a runner and org capacity.
The fix is to let GitHub decide which runs may run together before Salesforce is involved.
How concurrency groups work
A concurrency group is a string label. GitHub allows at most one running run (or job) per label at a time. Any other run with the same label waits.
You can set concurrency at two levels:
| Level | Scope | Typical use |
|---|---|---|
| Workflow | The whole run waits or is cancelled | PR validation, simple deploy workflows |
| Job | Only that job waits | One workflow with several jobs that target different orgs |
Two options control the behavior:
group: the label. Expressions such as${{ github.ref }}are allowed, so you can build a label per branch, per pull request, or per target org.cancel-in-progress: iftrue, a new run cancels the one that is running. Iffalse, the new run waits.
Gotcha: only one run can wait
A concurrency group holds one running run and at most one pending run. When a third run arrives, GitHub cancels the pending one, even with cancel-in-progress: false.
For a deployment pipeline this is usually fine. Run 3 contains everything run 2 had, because both deploy the head of main. It stops being fine if your pipeline deploys a delta between two commits. In that case, skipping run 2 can skip its changes. Either deploy from a stable baseline (for example, the last successfully deployed commit) or deploy the full source for production.
Pattern 1: cancel stale validations
Validation runs are disposable. Only the latest commit on a pull request matters, so cancel the old run when a new push arrives.
name: Validate pull request
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
concurrency:
group: validate-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the target org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txt
- name: Check-only deploy with local tests
run: >
sf project deploy start
--dry-run
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60The group name includes the pull request number, so two different pull requests still validate in parallel. Only pushes to the same pull request cancel each other.
Note that pull requests validating against the same sandbox can still collide with each other in the org. If that happens often, use one group for the whole validation org, set cancel-in-progress: false, and accept the queue. Alternatively, validate each pull request in its own scratch org.
Pattern 2: queue deployments per target org
Never cancel a deployment that is already running. A cancelled workflow can leave the org half-way through a change, and a deployment that started in Salesforce can keep going without a runner watching it. Queue instead.
name: Deploy to production
on:
push:
branches: [main]
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to production
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txt
- name: Deploy and run local tests
run: >
sf project deploy start
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60The label deploy-production is a name for the org, not for a branch. Any workflow that deploys to production should use the same label, including a manual hotfix workflow. If two workflows use different labels, GitHub treats them as unrelated and they can still overlap.
Choosing the group name
Use the label to describe what you are protecting.
| Goal | Group label |
|---|---|
| One deployment at a time to production | `deploy-production` |
| One deployment at a time per environment | `deploy-${{ inputs.environment }}` |
| Latest commit only per pull request | `validate-${{ github.event.pull_request.number }}` |
| Latest commit only per branch | `${{ github.workflow }}-${{ github.ref }}` |
Group names are matched exactly, and they are case-insensitive. A typo such as deploy-prod in one file and deploy-production in another silently removes the protection.
When a cancelled run leaves a deployment behind
If you do cancel a run during sf project deploy start, the CLI process stops, but the deployment may continue in the org. Add a cleanup step that runs on cancellation:
- name: Cancel the org deployment if the run is cancelled
if: cancelled()
run: sf project deploy cancel --use-most-recent --target-org targetPlace it after the deploy step. The --use-most-recent flag cancels the last deployment started from this runner's CLI cache. GitHub gives a cancelled job a short window to run if: cancelled() steps, so keep them quick. For production, queueing (Pattern 2) is still safer than cancelling.
Limits of concurrency groups
- They only see GitHub. A colleague deploying from a laptop, or a change set, is invisible to the group. Restrict direct deployment access to the pipeline's integration user where you can.
- They are per repository. Two repositories that deploy to the same org need a different mechanism, such as a single deployment repository or a shared lock in your release process.
- Waiting counts as time. A queued run waits without using runner minutes, but the change reaches the org later. Keep deployments short, using delta deployments or quick deploys, so the queue stays short.
- Approval gates interact with the queue. If a deployment is waiting for reviewer approval, think about whether newer runs should queue behind it. Test this on a non-production environment before depending on it.
Exercise
Your repository has one workflow that deploys to a UAT sandbox on every push to main, and another workflow, started manually, that deploys a hotfix branch to the same UAT sandbox. Each workflow has its own concurrency group, named after the workflow. Two deployments to UAT still overlap. Why, and what would you change? Also say which value of cancel-in-progress you would use.
Show answer
GitHub only serializes runs that share the same group label, and the two workflows use different labels, so GitHub sees them as unrelated. Change both workflows to use the same label tied to the org, for example group: deploy-uat. Use cancel-in-progress: false so a running deployment finishes and later runs queue behind it, because cancelling a deployment can leave the org partly changed.