Sub topic 15
Working with the Salesforce CLI
This chapter assumes you know Git basics (branches, commits, pull requests, merges), have a project set up in DevOps Center, and can run commands in a terminal. It covers where the Salesforce CLI fits next to DevOps Center, which tasks it handles better, and which mistakes to avoid when you mix the two.
Why use the CLI next to DevOps Center
DevOps Center gives you a point-and-click flow: work items, a pipeline, pull requests, and promotion. It does not cover every task. The Salesforce CLI (sf) is a command-line tool that talks to orgs and to your local project files. You use it for jobs that DevOps Center does not expose, or where you want more control.
Typical CLI jobs in a DevOps Center team:
- Checking a change against a stage's org before you promote it.
- Retrieving or deploying metadata that the work item workflow handles poorly.
- Running Apex tests and reading the full results.
- Working locally in an editor such as VS Code, with a local clone of the same repository.
- Scripting repeated checks.
The key idea is that Git is the shared source of truth. DevOps Center and the CLI both read and write the same repository. If the CLI work ends up in the correct branch through a normal commit and pull request, DevOps Center can see it. If it goes straight to an org and skips Git, the repository and the org drift apart.
The project layout the CLI expects
A DevOps Center repository uses the Salesforce DX source format. The file sfdx-project.json at the repository root tells the CLI where your source lives.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Points to check:
pathis the folder the CLI reads and writes. DevOps Center commits changes into this same folder, so the two must agree.sourceApiVersionsets the API version used for deploys and retrieves. Keep it consistent across the team. A mismatch can produce diffs that look like real changes.sfdcLoginUrlmatters for logins. Sandboxes usehttps://test.salesforce.com. You can override it with the--instance-urlflag when you log in.
Keeping tooling files out of the pipeline
A .forceignore file lists paths that the CLI skips when it deploys or retrieves. It works like .gitignore, but for org operations.
# .forceignore
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
.sfdx
.sfLocal editor settings, lint configs, and Jest tests under __tests__ are not metadata. If they go into a deployment, the deployment fails or carries files that do not belong in an org. Keep .forceignore in the repository, so everyone, including DevOps Center, uses the same rules.
Note that .forceignore does not stop Git from tracking a file. Use .gitignore for that. A file can be in one list, both, or neither, and each combination means something different.
Validating a change before you promote it
DevOps Center runs its own validation and tests as part of promotion. You can still check a change yourself, outside DevOps Center, against the org of the stage you plan to promote into. A validation-only deployment (a dry run) does everything a deployment does except save the changes.
sf org login web --alias uat
sf project deploy start --source-dir force-app --target-org uat --dry-run --test-level RunLocalTestsWhat each part does:
sf org login web --alias uatopens a browser, signs you in, and stores the login under the aliasuat.--source-dir force-appselects the folder to deploy.--target-org uatpicks the stored org.--dry-runvalidates without saving.--test-level RunLocalTestsruns all tests in your org except those from managed packages.
Run this from a checkout of the branch you want to test. If your local branch differs from the pipeline branch, the result describes your local files, not what DevOps Center will promote.
Seeing what a promotion would carry
Pipeline stages map to Git branches. A typical pipeline has a branch per stage, such as main for production and UAT for the user acceptance stage. You can compare them with plain Git.
git fetch origin
git log --oneline origin/main..origin/UATThe two-dot range origin/main..origin/UAT lists commits that are on UAT but not on main. Those are the changes a promotion to production would carry. Use it to spot surprises, such as a commit nobody remembers or a hotfix that skipped a stage.
You can also list the files that differ:
git diff --stat origin/main...origin/UATThe three-dot form compares UAT with the common ancestor of the two branches. It shows what UAT added, without showing changes that exist only on main.
| Syntax | Meaning in `git log` | Meaning in `git diff` |
|---|---|---|
| `A..B` | Commits in B that are not in A | Direct difference between A and B |
| `A...B` | Commits in either one, but not in both | Changes in B since the common ancestor |
Common errors when mixing the two
| Problem | Likely cause | Fix |
|---|---|---|
| Org and repository disagree | A change was made in the org and deployed or retrieved by the CLI, but never committed through a pull request | Retrieve it into a work item branch and commit it, or discard the org change |
| CLI deploy overwrites a teammate's work | Deploying a local branch to a shared stage org | Treat stage orgs as written only by the pipeline |
| Validation passes locally, fails in DevOps Center | Different branch, different API version, or different test level | Match the branch, `sourceApiVersion`, and test level |
| Noisy diffs on every retrieve | Different CLI or API versions between developers | Pin `sourceApiVersion` and update the CLI together |
| Tooling files appear in a deployment | Missing `.forceignore` entry | Add the pattern and remove the file from the deploy |
Rules of thumb
- Do not deploy to a stage org (UAT, staging, production) with the CLI as a routine step. The pipeline should be the only writer there, or the stage's branch no longer describes the org.
- Use the CLI freely against your own development sandbox, then bring the changes into a work item.
- Use CLI validation as an early warning, not as the official gate.
- Commit through a work item branch and pull request, so reviewers and DevOps Center can see the change.
Exercise
Your team's UAT branch is ahead of main. Write the Git command that lists the commits a promotion to production would carry, and explain why a teammate should not use sf project deploy start against the UAT org to "speed things up."
Show answer
Run git fetch origin and then git log --oneline origin/main..origin/UAT. This lists commits on UAT that are not on main. Deploying directly to the UAT org with the CLI skips the pipeline. The org would then contain changes that are not in the UAT branch, so the branch no longer describes the org, reviews and promotion history are bypassed, and later promotions can overwrite or conflict with the direct deployment.
Exercise
A dry-run deployment with RunLocalTests passes on your laptop, but the same change fails validation in DevOps Center. Name two causes you would check first.
Show answer
Check that your local branch matches the branch DevOps Center is promoting, and check that sourceApiVersion and the test level match. Other valid causes include unignored tooling files or metadata that exists only in your local org.