Sub topic 11
Deploying when a change merges
This chapter assumes you have a Salesforce DX project, a working pull request validation workflow, and an org login stored as a GitHub secret. Here you add the second half of the pipeline: a workflow that deploys to an org after a pull request merges.
How a merge triggers a deployment
A merge into main creates a push event on that branch. A workflow with a push trigger filtered to main starts on that event. The validation workflow already proved the change deploys cleanly, so this workflow performs the real deployment.
The deployment workflow
This workflow deploys every push to main.
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 60Three parts of this file matter most.
environment: productionlinks the job to a GitHub environment. The environment holds its own secrets and can require a reviewer to approve before the job runs.concurrencyplaces runs in a queue under one group name. Withcancel-in-progress: false, a second merge waits for the first deployment to finish instead of cancelling it. A cancelled deployment can leave a release half applied, so queue the runs.--test-level RunLocalTestsruns every Apex test in your org except those from managed packages. Production deployments of Apex require tests anyway. Setting the level explicitly keeps the behavior the same in every org.
The last step differs from the validation step in one way: it has no --dry-run flag. Without that flag, a successful run changes the org.
What happens on failure
Salesforce deploys atomically for each request. If a component fails or a test fails, the whole deployment rolls back and the org stays unchanged. The job then fails, and GitHub marks the commit with a red status.
--wait 60 makes the CLI wait up to 60 minutes and return the result. If the wait ends first, the CLI stops waiting but the deployment keeps running in the org. Choose a value larger than your longest normal run.
Common mistakes
- Merged code can differ from validated code. The validation ran against the pull request branch, but another merge may have landed first. Keep the deploy step running tests, and consider requiring branches to be up to date before merging.
- A deploy workflow without a
pathsfilter runs for documentation-only changes. Addpaths: ['force-app/**']if you want to skip those. - Printing the auth URL in a log line exposes the credential. Pass it through
envand delete the file immediately, as shown above.
Exercise
A developer merges two pull requests within a minute of each other. The first deployment takes ten minutes. With the concurrency block shown above, what happens to the second deployment, and why is that safer than cancelling the first?
Show answer
The second run waits in the queue until the first finishes, then runs against the latest commit. Because cancel-in-progress is false, the first deployment is never interrupted. Cancelling a deployment partway can leave the pipeline state unclear, while queuing guarantees that deployments finish one at a time in order.
Exercise
Your deploy workflow succeeds on the test step but the production org shows no changes. You check the command and find --dry-run. What does that flag do, and what should you change?
Show answer
--dry-run makes the command a check-only deployment: it validates the change and runs tests but saves nothing to the org. Remove the flag from the deploy workflow so a successful run commits the changes.