Sub topic 3
Deploying When a Change Merges
Assume you know Git branches, pull requests, Salesforce metadata, Apex tests, and basic YAML. This chapter focuses on the pipeline stage after review: deploying approved changes safely.
Deploy on merge
A workflow can deploy every push to main. Protect main so changes reach it only through reviewed pull requests. Use a GitHub environment for production approval and credentials.
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
- run: npm install --global @salesforce/cli
- name: Authenticate
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
printf '%s' "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target
rm authurl.txt
- name: Deploy
run: >
sf project deploy start
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60The production environment can require reviewer approval before the job starts. Store its authentication secret in that environment rather than in a general repository secret.
Faster and safer deployments
- Delta deployment:
sfdx-git-deltacompares Git revisions and creates a package containing only changed metadata. This reduces deployment time, but the checkout needs enough history for comparison. - Quick deploy: First validate with
--dry-run. If validation succeeds, use the returned job ID withsf project deploy quickto deploy without running validation again. - Scratch orgs: Create a temporary org, deploy the source, run tests with coverage, and delete the org. This tests changes in an isolated environment before shared sandboxes.
- Concurrency: A concurrency group queues production runs, preventing two deployments from modifying the org at once.
- Quality gates: Add Salesforce Code Analyzer or another static-analysis step before deployment. Fail the job when configured rules find blocking issues.
Troubleshooting failed runs
Read the first meaningful error, not only the final failed step. Authentication errors usually mean an expired, malformed, or incorrectly scoped secret. Test failures require the Apex test name and failure message. Missing metadata often indicates an incorrect source path, package dependency, or delta comparison revision.
Exercise
Why should production deployment use both a protected branch and a production environment?
Show answer
Branch protection controls which changes can merge. The environment controls production credentials and can require approval before deployment.