Sub topic 9
Validating pull requests with check-only deployments
This chapter assumes you know basic Git and pull requests, can run Salesforce CLI commands, and have a Salesforce DX project with Apex tests. It also assumes you have a sandbox or other org that is safe to use for validation.
What a check-only deployment does
A check-only deployment sends your metadata to an org and runs the full deployment process. It compiles code, checks dependencies, and runs tests. It then discards the result and saves nothing. The Salesforce CLI calls this a dry run.
A pull request check built on this gives reviewers a concrete answer to one question: would this change deploy cleanly? The check reports failures before the merge, while the author still has the context to fix them.
How the check runs
The check runs in your CI system every time a pull request into main is opened or updated.
The workflow file
This GitHub Actions workflow installs the CLI, authenticates, and runs the check.
name: Validate pull request
on:
pull_request:
branches: [main]
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 validation org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias ci-org
rm auth-url.txt
- name: Check-only deploy with local tests
run: sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org ci-org --wait 60Key points in this file:
--dry-runmakes the deployment check-only.--test-level RunLocalTestsruns every Apex test in your project, and skips tests from managed packages.--wait 60makes the command wait up to 60 minutes and fail if the deployment has not finished.- The auth URL comes from a repository secret. Get it with
sf org display --verbose --target-org <alias>run against the validation org, then store it asSFDX_AUTH_URL. The workflow writes it to a temporary file because the CLI reads the URL from a file, and it deletes the file right after login.
Choosing the validation org
The validation org determines what "passes" means. Use a sandbox that mirrors production's metadata and settings, so a green check predicts a successful production deployment. Keep these points in mind:
- Run one validation at a time per org. Concurrent deployments to the same org can queue or fail.
- If the sandbox is stale compared with production, a change can pass here and fail later. Refresh it on a schedule.
- Tests that depend on org data can pass or fail inconsistently. Make Apex tests create their own data.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Login step fails | Secret is missing, expired, or holds the wrong value | Regenerate the auth URL and update the secret |
| Coverage below 75% | New Apex lacks tests | Add tests. Production deployments need 75% overall coverage |
| Check passes, production deploy fails | Validation org differs from production | Refresh the sandbox and compare settings |
| Check times out | Test suite is too slow for the wait limit | Raise `--wait`, or speed up slow tests |
Exercise
Your pull request check is green, but the same change fails when deployed to production. Name two likely causes related to the validation org.
Show answer
The validation org may differ from production in metadata or settings, for example because the sandbox has not been refreshed. Tests may also depend on data that exists in the validation org but not in production. Refreshing the sandbox and making tests create their own data addresses both.