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 some experience retrieving metadata with sf project retrieve start. Here you will use sf project deploy start to send local source to an org and read the result.
What a deploy does
A deploy takes metadata from your local project, converts it from source format to the metadata format the org's Metadata API expects, and submits it as one deployment job. The org validates each component, runs any required Apex tests, and saves the components if everything passes.
By default a deployment is atomic. If any component fails, the whole job is rolled back and nothing is saved. This is why a deploy that reports one error can look like it did nothing.
Choosing what to deploy
The command needs to know which components to send. You pick them in one of three ways, and each way has its own flag.
| Selector | Flag | Use it when |
|---|---|---|
| Source directory | `--source-dir` (`-d`) | You want everything under a folder or a single file. |
| Metadata name | `--metadata` (`-m`) | You know the component types and names. |
| Manifest | `--manifest` (`-x`) | You want a reviewed, repeatable list of components. |
You can repeat --source-dir and --metadata. You can combine selectors in one command, and the CLI deploys the union of what they select.
Deploying a directory
The directory form is the most common one. This command deploys everything under force-app to the org aliased my-sandbox:
sf project deploy start --source-dir force-app --target-org my-sandboxPoint --source-dir at a narrower path to deploy less. A path can be a package directory, a subfolder such as force-app/main/default/classes, or a single file. When you give a file that belongs to a component with several files, the CLI includes the related files. For example, if you name AccountService.cls, it also sends AccountService.cls-meta.xml.
Deploying by metadata name
Use --metadata when you know the type and name:
sf project deploy start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__c --target-org my-sandboxThe value is Type:Name. A bare type, such as --metadata ApexClass, selects every component of that type in your project. Wildcards such as ApexClass:Account* also work. Quote them in your shell so the shell does not expand them first.
Deploying with a manifest
A manifest lists components in a package.xml file. This form is the best fit when a release needs a fixed, reviewable scope:
sf project deploy start --manifest manifest/package.xml --target-org my-sandboxA manifest can only select components that exist in your local project. If it names a component you have not retrieved, the deploy reports that the component was not found in the project.
Using the default org and source tracking
If you ran sf config set target-org my-sandbox, you can leave out --target-org. The CLI then uses your default org.
If the target is a scratch org or a sandbox with source tracking enabled, you can run the command with no selector at all:
sf project deploy startThe CLI compares your local files with its record of the org. It then deploys only the components you changed locally. This is the usual daily loop for development orgs. Production orgs and sandboxes without tracking have no such record, so you must always pass a selector there.
If someone changed the same component in the org, the CLI reports a conflict and stops. The source tracking chapter covers resolving these. For now, know that --ignore-conflicts overwrites the org's version with yours. Use it only when you have decided your local copy is the correct one.
Waiting for the result
A deploy is asynchronous on the org side. The CLI submits the job and then polls it until it finishes or its wait time runs out. The default wait is 33 minutes. You can change it with --wait (in minutes):
sf project deploy start --source-dir force-app --target-org my-sandbox --wait 10If the wait runs out, the job keeps running in the org. Only the CLI stops watching. Later chapters cover sf project deploy report and sf project deploy resume for picking it up again.
Reading the result
On success, the CLI prints a table of the components it deployed, with their type, name, and file path. It also shows the job ID and its state. On failure, it prints a table of problems. Each row gives the file, the line and column when they apply, and the message.
Look at the problems table first. Then check the following:
- Component failures. These are compile errors, missing dependencies, or invalid field references. Fix the file named in the row.
- Test failures. These appear in a separate section with the test class and method names, the message, and the stack trace. A deploy can fail on tests even when every component compiles.
- Code coverage warnings. In production, each Apex class and trigger you deploy needs enough test coverage, and the org as a whole must reach 75%.
For scripts, add --json. The output then contains the same information as structured data, including the job ID and a status field. The command also returns a non-zero exit code when the deployment does not succeed.
Test levels at deploy time
You choose which Apex tests run with --test-level. The spine's later chapters cover the choice in depth, but you need the defaults now:
- In a sandbox, scratch org, or Developer Edition org, the default is
NoTestRun. - In production, when the deploy includes Apex classes or triggers, the default is
RunLocalTests.
You can set the level yourself:
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTestsRunLocalTests runs every test in your org except tests that come from managed packages.
Useful flags and what they cost
| Flag | Effect | Risk |
|---|---|---|
| `--dry-run` | Runs the full deploy checks and saves nothing. | None. |
| `--ignore-warnings` | Lets the deploy succeed when it has warnings. | You may hide a real problem. |
| `--ignore-conflicts` | Overwrites org changes that tracking found. | Loses work made in the org. |
| `--ignore-errors` | Saves the components that passed even if others failed. | Leaves the org partly deployed. Production orgs do not accept it. |
| `--concise` | Prints only failures. | Less detail on success. |
Prefer --dry-run before any deploy you are unsure about:
sf project deploy start --source-dir force-app --target-org my-sandbox --dry-runCommon first-deploy mistakes
- Wrong org. The deploy goes to the default org when you omit
--target-org. Check withsf config get target-orgbefore you deploy to something important. - Missing dependencies. A custom field that refers to an object missing from the org fails. Include the dependency in the same deploy.
- Ignored files. If
.forceignoreexcludes a file, the deploy skips it without any error. A missing component after a "successful" deploy often means this. - Stale source. Deploying old local copies can overwrite newer org changes. Retrieve first when others also edit the org.
Exercise
You run sf project deploy start --source-dir force-app --target-org my-sandbox and it reports one failed Apex class among forty components. What state is the sandbox in, and what would change that behavior?
Show answer
By default a deployment is atomic, so the failure rolls back the whole job and none of the forty components are saved. Adding --ignore-errors makes the org save the components that passed, but it leaves the org partly deployed and is not accepted for production deploys. Fixing the failing class and deploying again is the safer path.
Exercise
You deploy a metadata selection to a production org and have not set --test-level. The deployment includes an Apex class. Which tests run?
Show answer
Production defaults to RunLocalTests when a deploy includes Apex classes or triggers, so all local tests run, which excludes tests from managed packages.