Sub topic 9
Previewing changes and running dry-run deploys
This chapter assumes you have a Salesforce DX project, an authorized org set as the target, and you know how to run a basic sf project deploy start. Before you push changes to an org, you can check what will move and whether the org will accept it.
Two kinds of preview
The CLI offers two separate checks, and they answer different questions.
- A preview lists what the CLI would deploy or retrieve, based on source tracking. It does not contact the deploy service to validate anything.
- A dry-run deploy sends the real deployment to the org and runs the org's checks, but saves nothing.
Use the preview to see the size of a change. Use the dry run to learn whether it will succeed.
Previewing with source tracking
Source tracking compares your local files with the org's last known state. This works for orgs that support tracking, such as scratch orgs and sandboxes with tracking enabled.
sf project deploy preview --target-org my-sandboxThe output groups components into those that would be deployed, those that would be deleted, and those with conflicts. A conflict means the component changed both locally and in the org. Conflicts are covered in a later chapter.
You can narrow the preview with the same selectors you use when deploying.
sf project deploy preview --source-dir force-app/main/default/classes --target-org my-sandboxA matching sf project retrieve preview command shows what a retrieve would bring down.
Running a dry-run deploy
Add --dry-run to a deploy to run it without saving changes.
sf project deploy start --source-dir force-app --target-org my-sandbox --dry-runThe org processes the deployment as normal. It checks that components compile, that dependencies exist, and that the metadata is valid. At the end it rolls everything back. If the command reports success, the same command without --dry-run should also succeed, assuming the org and your files have not changed in between.
What a dry run does not do
A dry run is a check, not a reservation.
- It does not produce a job ID you can quick-deploy later. For that, use
sf project deploy validate, covered in a later chapter. - Test behavior still depends on the test level. A dry run to a sandbox with the default level may run no tests, while a production dry run runs local tests. Set
--test-levelexplicitly if you need the same behavior as the real deploy. - The org can change between the dry run and the real deploy. Another user's deployment can invalidate your result.
Reading the result
A failed dry run prints a table of problems with the component name, file, line, and message. Fix the files and run it again. Each run is safe to repeat because nothing is saved.
Exercise
You ran sf project deploy start --source-dir force-app --target-org my-sandbox --dry-run and it succeeded. Does the sandbox now contain your changes, and can you quick-deploy that run?
Show answer
No to both. A dry run rolls back everything, so the org is unchanged, and it produces no validated job ID. To get a job ID for quick deploy, run sf project deploy validate instead.
Exercise
What is the difference between sf project deploy preview and sf project deploy start --dry-run?
Show answer
The preview lists the components that source tracking says would be deployed, deleted, or in conflict, without org validation. The dry run sends the deployment to the org, which validates it and then rolls it back.