Sub topic 12
Promoting through sandboxes to production with approvals
This chapter assumes you can write a basic GitHub Actions workflow, authenticate with an SFDX auth URL stored as a secret, and deploy on merge. Here you extend that single deployment into a chain: sandbox first, then production behind a human approval.
Why promote in stages
A change that passes tests in a check-only deployment can still fail in a real org. Data, installed packages, and org settings differ between environments. Deploying to a sandbox first exposes those differences before production sees the change. An approval gate adds a point where a person confirms the release is intended.
Environments hold the gate and the secrets
A GitHub environment is a named deployment target in a repository. It can require reviewers and hold its own secrets. Create two environments, staging and production, under Settings, then Environments. Add required reviewers to production only. Save a secret named SFDX_AUTH_URL in each environment, with the staging value from the sandbox and the production value from production.
Because both environments use the same secret name, the workflow steps stay identical. The environment: key decides which value a job receives.
A two-stage workflow
The needs key makes the production job wait for the staging job. When a job names a protected environment, GitHub pauses it until a reviewer approves.
name: Promote
on:
push:
branches: [main]
concurrency:
group: promote
cancel-in-progress: false
jobs:
staging:
runs-on: ubuntu-latest
environment: staging
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 sandbox
run: >
sf project deploy start
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60
production:
needs: staging
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 60Common errors
- Approval never appears. Required reviewers are set on the wrong environment, or the environment name in the YAML does not match exactly.
- Production deploys with the sandbox org. The secret was saved at repository level, or the environment is missing its own
SFDX_AUTH_URL. Environment secrets override repository secrets of the same name. - Production fails after staging passed. The two orgs have drifted apart. Compare installed packages and org settings.
- Two promotions collide. Without the
concurrencygroup, a second merge can start while the first waits for approval.
Exercise
You added required reviewers to the production environment, but the production job started without waiting. The job already contains environment: production. Name two things to check.
Show answer
Check that the reviewer protection rule was saved on the environment named exactly production, and that the repository plan supports required reviewers for the repository's visibility (private repositories need a paid plan). Also confirm the job does not use a differently spelled environment name.