Sub topic 3
The Salesforce CLI commands a pipeline relies on
This chapter assumes you know basic command-line use and have a Salesforce DX project. The sf CLI is the only tool your workflow uses to talk to Salesforce. Later chapters wrap these commands in GitHub Actions, so learn them here first.
How a pipeline uses the CLI
Every sf command exits with code 0 on success and a nonzero code on failure. A GitHub Actions step fails when its command exits nonzero, so the CLI's exit code decides whether a run passes.
Core commands
| Command | Purpose |
|---|---|
| `sf org login sfdx-url` | Authenticates with a stored auth URL file |
| `sf project deploy start` | Deploys source to an org |
| `sf project deploy report` | Shows the status of a deployment by job ID |
| `sf apex run test` | Runs Apex tests and reports coverage |
| `sf org list` | Lists authenticated orgs, useful for debugging |
Authenticating
The auth URL is a secret string that identifies an org and holds a refresh token. You write it to a temporary file and pass the file to the CLI.
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
sf org listThe --alias flag gives the org a name. Later commands can then use --target-org target instead of a username.
Deploying and validating
sf project deploy start sends your source to the org. Adding --dry-run makes it a check-only deployment. Salesforce runs the full validation, including tests, but saves nothing.
sf project deploy start \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60 \
--dry-runThe --wait 60 flag makes the command block for up to 60 minutes. Without it, the CLI may return before the deployment finishes, and the step could pass while the deployment is still running.
Running tests on their own
You can run tests without deploying, which suits scratch orgs and quick feedback.
sf apex run test \
--test-level RunLocalTests \
--result-format human \
--code-coverage \
--wait 20 \
--target-org targetMaking output readable by scripts
Add --json to any command to get machine-readable output. The output includes a status field and, for deployments, an id you can pass to sf project deploy report --job-id. Use this when a later step must read a result rather than only check whether the command passed.
Common errors
- Missing
--target-org. The command uses the default org, which may not be the one you meant. - No
--wait. The step ends early and hides later failures. - Wrong source path.
--source-dirmust match thepathinsfdx-project.json, usuallyforce-app.
Exercise
A step runs sf project deploy start --source-dir force-app --target-org target --dry-run. It passes, but nothing changes in the org. Is the pipeline broken? What does the step prove?
Show answer
The pipeline is not broken. --dry-run makes this a check-only deployment, so Salesforce validates the metadata (and runs tests if a test level is set) but saves nothing. A passing step proves the deployment would succeed against that org at that moment.