Sub topic 9
Validating pull requests with check-only deployments
This chapter covers the pull request check in the trunk-based flow. It assumes you can read a GitHub Actions workflow and run sf project deploy start. It also assumes you know the Apex test levels and have a feature branch workflow like the one in the previous chapter.
Why validate before merge
In ordinary application code, a pull request check compiles the code and runs unit tests on the runner. Salesforce can't do that. Apex compiles only inside an org, and metadata is valid only against the org it is deployed to. A branch that looks fine in Git can still fail in the org because of a missing field, a dependency on an unreleased component, or a failing test.
A check-only deployment fixes this. The org runs the full deployment process, including compilation, dependency checks, and tests, and then discards the result. Nothing is saved. The pull request gets a pass or fail signal before main changes.
The goal is that main always deploys. If every merged change passed a check-only deployment against an org that matches production closely, the next promotion is much less likely to break.
How a check-only deployment works
Add --dry-run to the deploy command to get a check-only deployment. The Metadata API validates the components and runs the tests you request. It then rolls back instead of committing.
sf project deploy start \
--source-dir force-app \
--dry-run \
--test-level RunLocalTests \
--target-org ci-org \
--wait 60Four things matter here:
--source-dirlimits what is sent. Usingforce-appsends the whole project directory.--dry-runmakes the run check-only.--test-levelchooses which Apex tests run.--wait 60makes the command block for up to 60 minutes and exit non-zero on failure. Without a wait, the command returns before the result is known and the CI job would pass without checking anything.
Test levels
| Test level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Sandbox only. Not allowed for production deployments that include Apex. |
| `RunSpecifiedTests` | Tests you name with `--tests` | Fast checks on small, well-understood changes. |
| `RunLocalTests` | All tests in your org except those from installed managed packages | The default choice for pull request checks. |
| `RunAllTestsInOrg` | Every test, including managed packages | Rare. Slow and often noisy. |
RunLocalTests is the safe default because production deployments require it for Apex changes, so a pull request that passes it matches what production will demand. In a large org it can take a long time. Treat that as a signal to speed up tests, use delta deploys, or move to modules, which later chapters cover.
What a check-only run proves
A check-only run proves that the components compile, the dependencies resolve, and the tests pass in the validation org's current state. It does not prove that the change is correct in production. The result depends on how closely the validation org matches your target.
If you use a sandbox, it must stay refreshed and drift-free. If the validation org has components that production lacks, a deployment can pass the check and then fail in production. The org drift chapter covers how to prevent this.
The validation flow
The pull request check runs on each push to the branch. A reviewer then sees the result next to the diff.
The workflow file
This GitHub Actions workflow runs the check on every pull request into main.
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 60Authentication
The runner has no browser, so it can't use the normal web login. Instead it logs in with a stored auth URL. You get one by running the following against an org you already logged in to:
sf org display --target-org my-validation-org --verbose --jsonCopy the value of sfdxAuthUrl from the output and store it as the repository secret SFDX_AUTH_URL. The workflow writes it to a temporary file, logs in, and deletes the file.
Treat that URL as a password. It contains a refresh token that grants access to the org. Store it only in the secrets store, never in the repository or in logs. Use a dedicated integration user with the permissions deployment needs, and nothing more.
The CLI can also use a JWT bearer flow with a connected app and certificate. This avoids a long-lived refresh token in a secret, and is a better fit for production-adjacent orgs. The auth URL is simpler to set up and is fine for a sandbox.
Which org to validate against
The validation org should be as close to production as you can get. Common choices:
- A dedicated full or partial copy sandbox. Closest to production and most likely to catch data-dependent test failures. It is a shared resource, so refresh cycles and concurrent runs matter.
- A developer sandbox kept in sync with production. Cheaper, but it has less data and needs regular refreshes.
- A fresh scratch org per run. Clean and isolated, but slow to create and it lacks your production configuration unless the project and definition file recreate it.
Concurrency
Two pull requests that validate at the same time against one org can interfere. Deployments to the same org queue, and tests that touch shared data can fail intermittently. Add a concurrency group so runs against one org happen one at a time:
concurrency:
group: validate-ci-org
cancel-in-progress: falsePlace this block at the top level of the workflow, next to on:. Use cancel-in-progress: false so queued runs are not dropped. If the queue becomes a bottleneck, add more validation orgs and distribute runs, or switch to scratch orgs.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Job passes but nothing was checked | Missing `--wait`, or the job ignored the exit code | Set `--wait` and let the step fail on a non-zero exit |
| `Unknown test level` or no tests run | Misspelled `--test-level` value | Use the exact value from the table above |
| Login step fails with an invalid grant error | The auth URL's refresh token was revoked, or the user is locked | Generate a new auth URL and update the secret |
| Tests pass locally, fail in CI | The validation org has different data or settings | Make tests create their own data, and compare org configuration |
| Fails with `Entity of type X does not exist` | The change depends on metadata that is not in the branch or the org | Add the dependency to the branch or to the validation org |
| Intermittent failures with `UNABLE_TO_LOCK_ROW` | Parallel runs touch the same records | Serialize runs with a concurrency group |
| Check takes too long | `RunLocalTests` on a large org | Use specified tests or delta deploys, covered later |
Make the check required
A check that isn't required is advice. Mark the validate job as a required status check in branch protection for main, so the merge button stays disabled until it passes. The next chapter covers that configuration.
Quick validation, then deploy
A passing check-only deployment can later be promoted without re-running tests. Salesforce records the result as a validation ID, and a quick deploy of that ID commits the same package without running the tests again. The ID expires after 10 days, and it applies only to the exact validated package in the same org. This is useful for production releases, and the release promotion chapter returns to it.
Exercise
A pull request check runs sf project deploy start --source-dir force-app --dry-run --test-level RunLocalTests --target-org ci-org without --wait. The job shows green even when an Apex test fails. Why, and what do you change?
Show answer
Without --wait, the command starts the deployment and returns right away, before the org reports the result, so the job exits with success. Add --wait 60 (or another suitable number of minutes) so the command blocks until the deployment finishes and exits non-zero when it fails.
Exercise
Why can a branch pass a check-only deployment against a sandbox and still fail when deployed to production?
Show answer
A check-only run validates against the current state of the validation org only. If the sandbox contains components, settings, or data that production lacks, or if production has changes the sandbox doesn't, the result can differ. Keeping the validation org refreshed and free of drift reduces this risk.