Sub topic 10
Validating every pull request
This chapter assumes you can read a GitHub Actions workflow, have a working Salesforce DX project, and know the Apex test levels from the previous chapter. It also assumes you have already stored an SFDX_AUTH_URL secret.
What validation does
A pull request (PR) validation is a check-only deployment. Salesforce runs the same compile, dependency, and test checks as a real deployment, then rolls everything back. The target org does not change. If the check fails, the PR shows a red status and reviewers know the change cannot ship as written.
The workflow
name: Validate pull request
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
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 target org
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: Check-only deploy with local tests
run: >
sf project deploy start
--dry-run
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60How it works
- Trigger:
pull_requestwithbranches: [main]runs on PRs that targetmain. It reruns each time you push new commits to the PR branch. - Path filter:
paths: force-app/**skips runs for changes such as README edits. Do not filter paths if the check is a required status check. A skipped workflow never reports, so the PR can stay blocked. --dry-run: this flag makes the deployment check-only.--test-level RunLocalTests: this runs every test in your org except those from managed packages. Production deployments of Apex require tests, so validating against a production-like org catches problems early.--wait 60: the CLI waits up to 60 minutes and fails the step if the deployment fails.- Secrets: the auth file is deleted right after login so it cannot leak into later steps or artifacts.
Common errors
| Symptom | Likely cause |
|---|---|
| Login step fails | The secret is empty or malformed, or the PR comes from a fork. Fork PRs cannot read secrets. |
| Coverage below 75% | Tests or classes in the PR lower the org's coverage. |
| Missing component error | The PR depends on metadata that exists only in another branch or org. |
| Timeout | The `--wait` value is too small for the test run. |
Making it a gate
The workflow only informs reviewers until you enforce it. In the repository's branch protection settings, require the validate status check before merging.
Exercise
A teammate edits only README.md in a PR, and the required validate check never appears, so the PR cannot be merged. What is the cause, and how can you fix it?
Show answer
The workflow has a paths: force-app/** filter, so it does not run for README-only changes. GitHub then never receives the required status. Fix it by removing the path filter, or by not requiring the check on such PRs. You could also add a lightweight job that always reports a result.