Sub topic 13
Validate first, then quick deploy
This chapter assumes you know the four test levels, the 75% coverage rule, and the basic sf project deploy start command from earlier chapters. It shows how to run the slow part of a production release (the tests) ahead of time. You then ship the result in minutes.
The problem: test time inside the release window
A production deployment that contains Apex runs tests at RunLocalTests by default. In an org with thousands of test methods, that run can take an hour or more. If you start the deployment during your release window, everyone waits for the tests. A failure late in the run means you roll back and try again.
A validation deployment moves the test run out of the window. Salesforce runs the full deployment, including the tests, against the target org. It then discards the changes instead of saving them. If the run passes, Salesforce remembers the result for a limited time. A quick deploy then commits the validated changes without running the tests again.
What a validation deployment does
A validation checks that a deployment would succeed. It does the following:
- Compiles the Apex and checks the metadata against the target org.
- Runs the tests for the chosen test level.
- Checks code coverage against the rules for that level.
- Saves nothing. The org is unchanged when it finishes.
You run it with sf project deploy validate:
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunLocalTests --wait 120The command accepts RunLocalTests, RunAllTestsInOrg and RunSpecifiedTests. NoTestRun is not an option, because a validation without tests would produce nothing a quick deploy could reuse. With RunSpecifiedTests, add --tests once for each class.
The command prints a job ID, and the job also appears in Setup > Deployment Status. Keep the ID, because the quick deploy needs it.
What a quick deploy does
A quick deploy takes the ID of a successful validation and commits those exact changes:
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodIf the validation job is the most recent one in your project, you can skip the ID:
sf project deploy quick --use-most-recent --target-org my-prodThe quick deploy does not read your local files again. It deploys what was validated, even if you edited the project afterwards. Salesforce does not re-run the tests, so the deployment usually finishes in a short time. The time still grows with the amount of metadata, because Salesforce saves the components and recompiles.
The full flow
The diagram shows the order of events and where the waiting happens.
Conditions for a quick deploy
A quick deploy is accepted only when all of these hold:
- The validation succeeded. All tests passed and coverage was met.
- The validation ran tests. A validation without tests cannot be used.
- The quick deploy targets the same org as the validation.
- The validation is recent. The window is 10 days after the validation finishes.
- The validated deployment is the one you are committing. You cannot change its contents.
The validation also has to meet the coverage rules for the level it used. With RunLocalTests that means 75% overall coverage across the org's Apex. With RunSpecifiedTests it means 75% for each class and trigger in the deployment, and the named tests must provide it. If you validate with a narrow RunSpecifiedTests list that meets those per-class rules, the quick deploy is still allowed. Check your org's behaviour with a sandbox first, because the supported combinations have changed across Salesforce releases.
What can invalidate a validation
A passing validation describes the org at the moment it ran. Several things make it less trustworthy, even inside the 10-day window:
- Other deployments to the same org. Another team may deploy a change that conflicts with yours. Your validation passed against the old state. The quick deploy can fail, or it can succeed and break something not covered by tests.
- Data and configuration changes. Tests that depend on org data, custom settings, or active flows and validation rules can behave differently later. Well-written tests create their own data, as in
AccountServiceTest, so they are less affected. - A different package. If you rebuild the package from a new commit, the earlier validation does not apply. Run a new validation.
For these reasons, many teams freeze the target org's deployments between validation and quick deploy. They also keep a record of the commit hash that was validated.
Checking the result
Both commands can run in the background with --async. To check a job later, use the report command:
sf project deploy report --job-id <validated-deploy-job-id> --target-org my-prodThe report shows the status, the number of tests run, failures and coverage warnings. Check it before the release window so you know the validation really succeeded.
A release routine that uses both
A typical routine for a production release looks like this:
- After the change is merged and passes its sandbox pipeline, run
sf project deploy validateagainst production. UseRunLocalTests. - Wait for the validation to succeed. If it fails, fix the test or coverage problem and validate again.
- Record the job ID with the release ticket.
- In the release window, run
sf project deploy quick --job-id .... - Confirm the result with
sf project deploy report.
The tests have the same strictness as a direct production deployment. The difference is when they run.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Quick deploy says the job cannot be used | Validation older than 10 days, or it failed | Run a new validation |
| Quick deploy rejected for the org | Job ID came from a different org | Use the ID from the production validation |
| Validate command refuses `NoTestRun` | Validation requires tests | Choose `RunLocalTests` or `RunSpecifiedTests` |
| Validation fails on coverage | Overall coverage below 75%, or a listed class below 75% | Add tests, then validate again |
| Quick deploy lacks a recent fix | Fix was committed after validation | Validate the new package and use its job ID |
Exercise
A team validates a package against production on Monday with RunLocalTests and it passes. On the following Friday, a developer fixes a typo in an Apex class and commits it. The team then runs the quick deploy using Monday's job ID. Is the typo fix in production? What should they do instead?
Show answer
No. A quick deploy commits the validated package from Monday and does not read local files. To include the fix, the team must run a new validation of the updated package with tests, then run a quick deploy with the new job ID within 10 days of that validation.
Exercise
Why does sf project deploy validate not accept NoTestRun, and why is that consistent with how a quick deploy works?
Show answer
A quick deploy skips tests because a successful validation already ran them and met the coverage rules. A validation with no tests would give Salesforce nothing to reuse, so a quick deploy could not safely skip the tests. The validate command therefore requires a level that runs tests: RunLocalTests, RunAllTestsInOrg or RunSpecifiedTests.