Sub topic 11
Deploying when a change merges
This chapter covers the workflow that runs after a pull request merges into main and deploys the change to an org. It assumes you already know the following:
- You can write a basic GitHub Actions workflow.
- You can install the Salesforce CLI on a runner.
- You can authenticate with an SFDX auth URL stored as a secret.
- You have a pull request validation job that runs a check-only deployment.
What "deploy on merge" means
A merge to main is a push event on that branch. A workflow with on: push: branches: [main] starts when the merge commit lands. It checks out the merged code and runs a real deployment with sf project deploy start.
This differs from pull request validation in three ways:
- The validation job uses
--dry-run, so the org is not changed. The deploy job omits that flag, so the org is changed. - Validation runs against the pull request's merge result. The deploy job runs against the actual commit on
main. - A failed validation blocks a merge. A failed deploy cannot block anything, because the code is already merged. A red deploy run means
mainand the org are out of sync, and someone must fix that.
The third point is why validation matters. If the deploy job is the first time the code meets the org's tests, you find problems after they are in main.
A complete deploy workflow
This workflow deploys on every push to main. It uses a GitHub environment named production and a concurrency group.
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 60How each part works
on: pushtomain. Merges, direct pushes, and reverts all trigger it. If you protectmainand require pull requests, only merges can reach it.environment: production. The job uses the secrets stored on that environment. If the environment has required reviewers, the job pauses until one approves.concurrency. Only one run in the group executes at a time. Withcancel-in-progress: false, a newer run waits instead of cancelling the one in progress. This matters for deployments, because cancelling the CLI client mid-run is a bad idea.--test-level RunLocalTests. This runs all tests in your org except those from managed packages. Production deployments of Apex require tests, and this level is the usual choice.--wait 60. The CLI waits up to 60 minutes for the deployment. If it times out, the job fails, but the deployment may still be running in the org.
Test levels on deploy
Which test level to use depends on the target org.
| Target | Typical level | Reason |
|---|---|---|
| Developer sandbox | `NoTestRun` | Fast feedback. Sandboxes do not require tests for deployment. |
| Integration or UAT sandbox | `RunLocalTests` | Catches regressions before production. |
| Production | `RunLocalTests` | Production requires tests when you deploy Apex, and local tests give you the 75% coverage check. |
| Production, targeted | `RunSpecifiedTests` | Faster, but each class or trigger you deploy needs 75% coverage from the named tests. |
RunSpecifiedTests is the most common source of surprise failures. Salesforce checks coverage per deployed class or trigger, not for the org as a whole. A deployment that passes in a sandbox can fail in production for this reason.
Deploying to a sandbox first
A common step up from a single production deploy is a two-job workflow. The first job deploys to a sandbox. The second runs only if the first succeeds.
name: Deploy pipeline
on:
push:
branches: [main]
concurrency:
group: deploy-pipeline
cancel-in-progress: false
jobs:
deploy-uat:
runs-on: ubuntu-latest
environment: uat
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
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 to UAT
run: >
sf project deploy start
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60
deploy-production:
needs: deploy-uat
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
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 to production
run: >
sf project deploy start
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60Both jobs read a secret named SFDX_AUTH_URL, but each job resolves it from its own environment. The uat environment holds the sandbox's URL and the production environment holds production's. The workflow file does not change between targets. Chapter content on approval gates covers how to require a reviewer on production.
The needs keyword makes the production job wait for the UAT job. If UAT fails, production is skipped.
Edge cases and failure modes
- Partial deployments do not happen by default. Salesforce deployments are all-or-nothing unless you pass
--ignore-errors. Do not use that flag in a pipeline for production, because it can leave the org in a state that does not match any commit. - A timeout is not a failure of the deployment. When
--waitexpires, the CLI exits with an error, but the deployment continues in Salesforce. Check Setup, Deployment Status, or runsf project deploy reportbefore you re-run the workflow. - Deleted components are not deployed. Removing a file from
force-appdoes not delete the component in the org. Deletions require a destructive changes manifest, which delta tooling can generate for you. - Two merges in quick succession. The concurrency group queues them. GitHub keeps at most one pending run per group, so a third run can replace a second pending run. The newest commit still deploys, and because each run deploys the whole source, nothing is lost.
- Re-runs use the original commit. Re-running a failed job deploys the same commit, not the latest
main. After a fix, push a new commit instead. - Direct pushes bypass validation. Turn on branch protection and require the validation check to pass before merging.
Reading the result
The CLI prints a summary of components deployed and tests run. On failure, it lists component errors and failed tests with messages. Look at the first error in the list, since later errors are often caused by it. The troubleshooting chapter covers this in more detail.
Exercise
Your deploy workflow uses --test-level RunLocalTests and the production environment. A teammate wants to add cancel-in-progress: true to the concurrency block so new merges replace the running deployment. Why is that a poor choice for deployments, and what setting should stay in place?
Show answer
With cancel-in-progress: true, GitHub cancels the run that is in progress when a newer one starts. Cancelling the job stops the CLI client in the middle of a deployment, and you can no longer rely on the run's result to know what state the org is in. Keep cancel-in-progress: false so a new run waits for the current deployment to finish. Because each run deploys the full source from its own commit, the queued run then brings the org up to date.
Exercise
A deploy to production fails with a coverage error on one Apex class, although the same change passed in a sandbox. The workflow uses RunSpecifiedTests with one test class. What is the likely cause, and what are two ways to fix it?
Show answer
Salesforce requires 75% coverage for each deployed class or trigger, counted from the tests that ran. The named test class probably does not cover that class enough on its own. One fix is to add the test classes that cover it to the --tests list. The other is to switch to RunLocalTests, which runs every local test.