Sub topic 3
The Salesforce CLI commands a pipeline relies on
This chapter assumes you know Git and GitHub basics, Salesforce metadata and Apex tests, and the command line. It covers the sf commands a pipeline calls and the flags that decide whether a run is safe, fast, and easy to debug. Later chapters put these commands inside workflows.
Why the CLI is the pipeline's interface
A GitHub Actions workflow is a list of shell commands. Salesforce does nothing for your pipeline unless a command asks it to. Every pipeline action (log in, validate, deploy, test, clean up) is one sf invocation. The pipeline passes or fails based on the exit status of those commands, so you need to know what each command does and how it signals failure.
sf is the current CLI. The older sfdx commands (for example sfdx force:source:deploy) are legacy. Write new pipelines with sf, and when you read older tutorials, translate their commands.
The command groups
| Task | Command | Notes |
|---|---|---|
| Log in with an auth URL | `sf org login sfdx-url` | Reads a file containing the URL |
| Log in with JWT | `sf org login jwt` | Needs a connected app and a private key |
| Deploy or validate | `sf project deploy start` | `--dry-run` makes it check-only |
| Validate for a later quick deploy | `sf project deploy validate` | Returns a job ID |
| Deploy a validated job | `sf project deploy quick` | Takes `--job-id` |
| Check a deployment | `sf project deploy report` | Takes `--job-id` |
| Run Apex tests | `sf apex run test` | Output formats include human, JUnit, and JSON |
| Create or delete scratch orgs | `sf org create scratch`, `sf org delete scratch` | Uses a definition file |
| List authorized orgs | `sf org list` | Useful for debugging login |
Authenticating
The login command stores a credential on the runner and gives the org an alias. Later commands refer to that alias with --target-org.
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
sf org listThe auth URL file contains a secret. Write it from a GitHub secret, use it, and delete it in the same step. The JWT alternative looks like this:
sf org login jwt \
--client-id "$CONNECTED_APP_CLIENT_ID" \
--jwt-key-file server.key \
--username "$SF_USERNAME" \
--instance-url https://login.salesforce.com \
--alias targetUse https://test.salesforce.com as the instance URL for sandboxes. Using the wrong URL is a common cause of login errors that mention an invalid grant.
Deploying with `sf project deploy start`
This is the central command. The flags you will use most:
--source-dir(or-d) names the folders or files to deploy. You can repeat it.--manifest(or-x) deploys what apackage.xmllists. Use it when you need exact control.--metadata(or-m) names individual components, such asApexClass:MyClass.--target-org(or-o) names the org alias. Pass it on every command so a pipeline never relies on an implicit default.--test-levelsets which tests run.--dry-runruns the whole deployment as a check and saves nothing.--wait(or-w) sets how many minutes the command waits for the result.--jsonprints the result as JSON, which scripts can parse.
A pull request check looks like this:
sf project deploy start \
--dry-run \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60 \
--json > deploy-result.jsonTest levels
| Level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | Nothing | Sandbox deployments only. Production rejects it for Apex or trigger changes. |
| `RunSpecifiedTests` | Classes you list with `--tests` | Fast checks on a known set |
| `RunLocalTests` | All tests except those from managed packages | The usual default for production |
| `RunAllTestsInOrg` | Every test, including packages | Rare, slow |
When you use RunSpecifiedTests, production enforces coverage per class on the classes in the deployment. Salesforce requires at least 75% overall coverage when production runs tests, so a green sandbox deployment with NoTestRun does not guarantee a green production deployment.
Exit status and `--wait`
The CLI exits with zero on success and a nonzero code on failure. GitHub Actions marks a step failed when the command exits nonzero, which is how a failed test fails the pull request check.
Be careful with --wait. If the wait period ends before Salesforce finishes, the command exits nonzero, but the deployment can still be running in the org. Set --wait longer than your slowest normal run. When a timeout happens, run sf project deploy report --job-id <id> instead of starting a second deployment on top of the first.
Validate, then quick deploy
sf project deploy validate runs a check-only deployment and prints a job ID. After a successful validation with tests, sf project deploy quick deploys that same validated package without running the tests again.
sf project deploy validate \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60
# Later, using the job ID printed by the command above
sf project deploy quick --job-id 0AfXXXXXXXXXXXXXXX --target-org target --wait 30A validation stays eligible for quick deploy for 10 days. Any change in the org's metadata or tests after validation can make it ineligible, so a quick deploy can fail even though the validation passed. Chapter plans for this tutorial return to this pattern later; here you only need to know which commands are involved.
Running Apex tests directly
sf apex run test runs tests in an org without deploying. You use it after a deployment to a scratch org or sandbox.
sf apex run test \
--test-level RunLocalTests \
--result-format junit \
--code-coverage \
--output-dir test-results \
--wait 20 \
--target-org target--output-dir writes result files that a later workflow step can upload as artifacts. Use --result-format junit when a tool needs to read the results, and human when people read the log.
Scratch org commands
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias ci-scratch \
--duration-days 1 \
--set-default
sf org delete scratch --target-org ci-scratch --no-promptDelete scratch orgs even when a test step fails, or they count against your daily and active scratch org limits. In a workflow, that means a cleanup step with if: always().
Common mistakes
- Running a command without
--target-org. It uses the default org, which may be the wrong one if a job authenticated twice. - Treating a
--dry-runsuccess as a deployment. It changes nothing in the org. - Setting
--waittoo low and starting a second deployment while the first is still running. - Using
sfdxflags withsfcommands. Flag names differ, for example--source-dirreplaces the old--sourcepath. - Printing the auth URL in a log. Run
sfcommands with secrets passed only through environment variables.
Exercise
A pull request check uses sf project deploy start --dry-run --test-level RunLocalTests --wait 5. On a large org it fails with a timeout, but the Salesforce deployment status page shows the check is still running. What should you change, and what command can you use to see the final result?
Show answer
Raise --wait above the normal run time (for example 60), because the command stopped waiting while Salesforce kept working. To see the final result of the existing run, use sf project deploy report --job-id <id> with the job ID from the original output, instead of starting a new deployment.