Sub topic 8
Deploying source to an org
This chapter assumes you have a Salesforce DX project, an authorized org set as your default target, and know how to retrieve metadata by directory or name. Deploying is the reverse of retrieving. The CLI reads source files in your project and sends them to an org.
The basic command
The command is sf project deploy start. You choose what to send with one of three flags, the same selectors you used for retrieve:
# Everything under a source directory
sf project deploy start --source-dir force-app --target-org my-sandbox
# Named components only
sf project deploy start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__c --target-org my-sandbox
# Whatever a manifest lists
sf project deploy start --manifest manifest/package.xml --target-org my-sandboxIf you leave out --target-org, the CLI uses the default org you set with sf config set target-org.
What happens during a deploy
The CLI converts your source files to metadata format and uploads them as one deployment. The org checks every component, saves them if all succeed, and runs any tests the test level requires. The deployment is all or nothing by default. If one component fails, nothing is saved.
Reading the result
By default the command waits and prints a table of deployed components with their state, such as Created, Changed, or Unchanged. On failure it prints a table of problems with the file, line, and message. Read the file and line first, because most failures are a missing dependency or a failing Apex test.
The wait time is set with --wait, in minutes. When the wait ends before the org finishes, the deployment keeps running in the org. You check on it later with the status commands covered in a later chapter.
What gets sent
Only files inside a package directory listed in sfdx-project.json are eligible. Files matched by .forceignore are skipped even if you name them. If a component you expect is missing from the result table, check these two places first.
Common selector mistakes
- The flag
--source-dirtakes a path, while--metadatatakesType:Name. Mixing the two forms gives a "not found" style error. - A manifest that lists a component with no matching local file will not create it. The file must exist in the project.
- Deploying to the wrong org is easy when the default changes. Pass
--target-orgexplicitly in scripts and before any production deploy.
Exercise
You run sf project deploy start --source-dir force-app --target-org my-sandbox and 30 components succeed but one Apex class has a compile error. How many components are saved in the org, and why?
Show answer
None. A deployment is all or nothing by default, so one failing component causes the whole deployment to be rolled back and the errors are reported.