Sub topic 10
Validating every pull request
This chapter assumes you can read a GitHub Actions workflow, you know what a Salesforce DX project looks like, and you have already authenticated a runner to an org with an SFDX auth URL. Here you will put those pieces together into a check that runs on every pull request.
What a validation does
A validation is a check-only deployment. Salesforce runs the full deploy process against the target org: it resolves dependencies, compiles Apex, checks metadata rules, and runs the tests you request. Then it rolls everything back. The org does not change.
This catches the problems that otherwise appear after a merge:
- Missing dependencies, such as a field referenced by a flow that is not in the package.
- Apex that compiles locally but fails in the target org.
- Failing tests, or code coverage below the required threshold.
The check runs against the real target org, not a mock. A pull request that passes has been accepted by the same platform that will accept the real deployment.
The validation workflow
This workflow runs when a pull request targets main and changes files under force-app.
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 60Three details matter here.
--dry-runturns the deployment into a validation. Remove it and the same command changes the org.--test-level RunLocalTestsruns every test in your org except tests that belong to managed packages. This is the level production requires for Apex deployments, so a validation at this level predicts what production will do.--wait 60makes the CLI wait up to 60 minutes and stream progress. Without it, the command can return before the result is known, and the job would pass without proof.
The job fails whenever sf exits with a non-zero status. That is how a failed validation turns the pull request check red. You do not need extra scripting for this.
How the check fits into the pull request
The workflow alone does not stop anyone from merging. Blocking requires a branch protection rule (or a repository ruleset) on main that lists the validation job as a required status check. Set this up once the workflow has run at least one time, because GitHub only offers checks it has already seen.
Choosing the target org
A validation needs an org to run against. Common choices:
| Target | Benefit | Cost |
|---|---|---|
| Integration sandbox | Matches your normal metadata and data shape | Concurrent pull requests share one org |
| Full or partial sandbox copy | Closest to production | Slow refresh, limited supply |
| Production (check-only) | Exact production rules | Consumes production deploy capacity |
Check-only deployments do not change data or metadata, but they do take a deployment slot in the org and run tests. Two validations against the same org at the same time can slow each other or hit row locks in tests that touch the same records. Running against an org that is not receiving other deployments avoids most of this.
Test level choices
The test level controls how long the check takes and how much it proves.
NoTestRunruns no tests. Salesforce allows it for sandboxes only, and it validates only that the metadata is deployable.RunSpecifiedTestsruns tests you name with--tests. This is fast, but you must choose the right tests.RunLocalTestsruns all non-managed tests. It is the safe default.RunAllTestsInOrgalso runs managed package tests. It is rarely needed.
If the full suite is slow, a common compromise is to run RunSpecifiedTests on pull requests and RunLocalTests on merge. Be aware of the trade-off: a pull request that passes on a subset can still fail the full run later.
Salesforce also enforces 75% Apex code coverage across the deployed classes for production deployments. A validation at RunLocalTests reports coverage failures the same way it reports test failures.
Edge cases that surprise people
The paths filter and required checks
The paths filter saves runner time by skipping the workflow when no Salesforce files change. However, if the workflow is skipped by a path filter and the job is a required check, GitHub leaves the check in "Expected" and the pull request cannot merge. Two ways out:
- Remove the
pathsfilter and let the job run every time. - Keep the filter, and add a second workflow with the opposite filter (
paths-ignore) and the same job name that finishes immediately.
Pull requests from forks
GitHub does not give repository secrets to workflows triggered by pull requests from forks. The authentication step will fail with an empty value. For a private repository where all contributors have write access, this is rarely a problem. For public repositories, do not switch to pull_request_target to get around it. That trigger runs with secrets and can execute untrusted code from the pull request.
Superseded runs
If a developer pushes three commits in a row, three validations start. Cancel the old ones so only the latest runs. Add this at the top level of the workflow:
concurrency:
group: validate-${{ github.event.pull_request.number }}
cancel-in-progress: trueHere cancel-in-progress: true is correct, because a validation changes nothing and the latest commit is the only one that matters. This is the opposite of the production deploy workflow, where you queue runs instead of cancelling them.
Keeping the validation ID
A successful production validation returns a job ID that a later quick deploy can reuse. If you plan to use quick deploy, capture the result with --json and store the ID. That is covered in a later chapter.
Reading a failure
When the validation fails, open the job log and look at the output of the deploy step. The CLI prints a table of component failures with the file, line, and message, and then a table of failed tests with the stack trace. Fix the first error in each table first, because later errors are often consequences of it.
Exercise
A required check named "validate" never appears as passed on a pull request that only edits a README, and the merge button stays disabled. The workflow uses paths: ['force-app/**']. Why does this happen, and what is one fix?
Show answer
The path filter skips the workflow because no file under force-app changed, so the required check is never reported and stays in the Expected state. One fix is to remove the paths filter so the job always runs. Another is to add a second workflow that uses paths-ignore for the same folder and has a job with the same name that passes immediately.
Exercise
Why is cancel-in-progress: true appropriate for the validation workflow but risky for a production deployment workflow?
Show answer
A validation is a check-only deploy that changes nothing, and only the result for the latest commit matters, so cancelling older runs is safe. Cancelling a production deployment partway through can leave the org partly updated or interrupt a deploy that already started, so production runs should queue instead.