Sub topic 11
Validating first, then quick-deploying
This chapter assumes you can deploy a manifest with sf project deploy start, you know the Apex test levels, and you have authorized a production org or a sandbox you can treat as one. It covers how to run the slow part of a release (the Apex tests) ahead of time, and how to finish the release in minutes.
Why split a deployment in two
A production deployment that includes Apex must run tests. In a large org, RunLocalTests can take tens of minutes or hours. While the deployment runs, Salesforce locks the metadata it touches, and a failed test at the end means you have spent the whole release window for nothing.
Validate-then-quick-deploy separates two questions:
- Will this change set deploy and pass its tests? Answer this early, with no effect on the org.
- Do we want it live now? Answer this during the release window, without repeating the tests.
What validation does
sf project deploy validate runs a full deployment with the "check only" option. Salesforce compiles the components, runs the tests, and checks code coverage, but it saves nothing to the org. If everything passes, Salesforce keeps the result as a validated deployment and gives it a job ID. Job IDs for deployments start with 0Af.
This differs from --dry-run on deploy start. A dry run also saves nothing, but validate is built for the quick-deploy workflow. It requires you to think about tests, and its job ID is what sf project deploy quick consumes.
sf project deploy validate --manifest manifest/package.xml --target-org production --test-level RunLocalTestsThe command waits for the result by default. The output shows component successes or failures, then test results, then coverage. The last lines include the job ID. Copy it, or use --use-most-recent later.
Options you will use
| Flag | Purpose |
|---|---|
| `--manifest`, `--source-dir`, `--metadata` | Select what to validate, as with `deploy start` |
| `--target-org` | The org to validate against |
| `--test-level` | `RunLocalTests`, `RunAllTestsInOrg`, or `RunSpecifiedTests` |
| `--tests` | Test classes to run, used with `RunSpecifiedTests` |
| `--wait` | Minutes to wait before the command stops waiting |
| `--async` | Return immediately and check the job later |
NoTestRun is not a useful validation level. A quick deploy needs a validation that actually ran tests, so always set the test level explicitly in scripts. That keeps the behavior from changing if a default changes.
Quick-deploying a validated job
sf project deploy quick takes the job ID and deploys the already-validated components, skipping the tests.
sf project deploy validate --manifest manifest/package.xml --target-org production --test-level RunLocalTests
sf project deploy quick --job-id 0AfXXXXXXXXXXXXXXX --target-org productionReplace the placeholder with the ID printed by the validate command. Two points matter here:
- Quick deploy does not read your local files. It promotes the exact components Salesforce stored during validation. Edits you make to your project after validating have no effect on it. If you change anything, validate again.
- Quick deploy takes no
--test-level. The tests already ran. The command only needs the job ID and the org.
The rules a validation must meet
A quick deploy is only accepted if the validated job qualifies. Check these before the release window:
- It must have run tests. A validation with no tests cannot be quick-deployed.
- The tests must have passed, and coverage must be met. For production this means the usual 75% Apex coverage requirement, plus passing tests.
- It must be recent. A validation is valid for 10 days after it completes. After that, the job ID stops working and you must validate again.
- It must target the same org. The job ID belongs to the org that ran the validation.
The org can still change between validation and quick deploy. Someone may deploy another change, or an admin may edit a component you depend on. Quick deploy does not re-verify against those changes, so a change that touches the same components can cause a failure or an unexpected result. In busy orgs, validate as close to the window as practical, and freeze other deployments in between.
Using the most recent job
You do not have to copy the ID. The CLI caches the ID of the last validation it ran on your machine, and --use-most-recent points at it.
sf project deploy quick --use-most-recent --target-org productionThis is convenient, but only safe if no other validation ran in between. If you validated two change sets, the "most recent" one may not be the one you intend. For releases, name the job explicitly with --job-id.
Capturing the job ID in a script
The --json flag prints machine-readable output. With jq you can extract the ID and pass it on.
set -euo pipefail
JOB_ID=$(sf project deploy validate \
--manifest manifest/package.xml \
--target-org production \
--test-level RunLocalTests \
--json | jq -r '.result.id')
echo "Validated job: $JOB_ID"Store $JOB_ID as a pipeline artifact or variable. Later, a manual approval step can run sf project deploy quick --job-id "$JOB_ID" --target-org production. If the validate step fails, the command exits with a non-zero status, and set -e stops the script before it can reach the quick deploy.
Validating with a narrower test set
On production, RunLocalTests is the usual choice because it runs every test in your org except tests from managed packages. If your team has a policy that allows it, you can validate with specific tests.
sf project deploy validate --manifest manifest/package.xml --target-org production --test-level RunSpecifiedTests --tests AccountServiceTest --tests InvoiceTriggerTestWith RunSpecifiedTests, Salesforce enforces coverage per deployed class and trigger rather than across the org. A class with low coverage from the listed tests fails the validation even if org-wide coverage is high. Check this before you rely on a short test list for a release.
Checking on a long validation
Validations can outlast the --wait time. When the command stops waiting, the job keeps running on the server. Use the status commands from the earlier chapter:
sf project deploy report --use-most-recent
sf project deploy resume --use-most-recentreport shows the current state. resume waits for the job again and prints the final result. Run report or resume with the job ID if you started several.
Common failures
| Symptom | Likely cause | Action |
|---|---|---|
| Quick deploy reports the job cannot be found or has expired | The validation is older than 10 days, or the ID belongs to another org | Validate again and use the new ID |
| Quick deploy rejected because tests did not run | The validation used no tests | Validate with `RunLocalTests` or another level that runs tests |
| Validation passes but quick deploy fails on a component | Org changed after validation | Revalidate close to the release |
| Coverage failure on validate | A deployed class lacks covering tests | Add tests, then validate again |
| Wrong change set goes live | `--use-most-recent` picked a different job | Use `--job-id` in release scripts |
Summary of the workflow
- Build the manifest for the release.
- Run
sf project deploy validatewith an explicit test level against the target org. - Record the job ID and confirm the result with
sf project deploy reportif needed. - During the window, run
sf project deploy quick --job-id <id>. - If anything changes or 10 days pass, go back to step 2.
Exercise
You validated a change set on Monday and noted job ID 0AfA. On Wednesday you fix a bug in one Apex class locally. Can you run sf project deploy quick --job-id 0AfA on Friday and expect the fix to ship? What should you do?
Show answer
No. Quick deploy publishes the components stored during the Monday validation, not your local files, so the fix would not ship. Run sf project deploy validate again with an explicit test level, take the new job ID, and quick-deploy that ID within 10 days of its completion.
Exercise
A release script uses sf project deploy quick --use-most-recent. Another engineer validates a different change set from the same machine an hour before the release. What can go wrong, and what is the fix?
Show answer
The cached "most recent" job is now the other engineer's validation, so the script can quick-deploy the wrong change set. Pass the explicit ID with --job-id, captured from the intended validation, such as with --json and jq -r '.result.id'.