Resources /
Blog

Salesforce CLI: What It Is and How Teams Use It

Submit your details to get a book

6
Min Read
Resources /
Blog

Salesforce CLI: What It Is and How Teams Use It

Download

Submit your details to get a book

6
Min Read
Glowing gear on a neon platform beside terminal panels, linked to code, branch, check, clock and list tiles
Glowing gear on a neon platform beside terminal panels, linked to code, branch, check, clock and list tiles

The Salesforce CLI is Salesforce's command-line tool for working with orgs and metadata, and its command is sf. Developers and release managers use it to log in to orgs, pull and push metadata, run Apex tests and script deployments, from a laptop terminal or a CI server.

The sections below walk through installing it, connecting an org, the commands release teams use most and the CLI's place in a CI/CD pipeline.

What is the Salesforce CLI?

The CLI runs commands against Salesforce orgs from a terminal. Every action is a command, so a deploy or a test run can go into a script that runs the same way every time.

It's one of several Salesforce deployment methods. Unlike change sets, which only travel between orgs tied to the same production org, the CLI can deploy to any org you can authorize.

The current version is sf (v2). Salesforce recommends that anyone still on sfdx (v7) move to sf (v2), and says the commands you already run in a terminal or CI script keep working after the move.

How do you install the Salesforce CLI?

Salesforce offers four install routes: a .pkg file on macOS, an .exe installer on Windows, TAR files for any supported system, and npm if Node.js is already on the machine. Pick one route per machine, because Salesforce warns that mixing them can cause path problems that are hard to diagnose.

The npm route suits CI servers: install with npm install @salesforce/cli --global, then run sf version to confirm it worked. Salesforce recommends always running the latest version or a release candidate, so make updating part of the routine.

How do you connect the CLI to an org?

Before it can do anything, the CLI needs an authorized org. On your own machine, sf org login web --alias dev-sandbox opens a browser for the login, and the alias saves typing later; for a sandbox, add --instance-url https://test.salesforce.com.

A CI server has no browser, so pipelines log in with the JWT bearer flow through a connected app. Its private key belongs in the pipeline's secret store, never in the repository.

To see every org the CLI can use, run sf org list. On a shared machine, run sf org logout when you finish.

How is a Salesforce DX project organized?

Running sf project generate --name my-project creates a Salesforce DX project: a folder with a config file, sfdx-project.json, and a force-app directory for your metadata. The CLI keeps metadata there in source format, which Salesforce built to work better with version control than the format the Metadata API uses.

Source format splits large files into smaller ones. Custom objects, for example, are broken into subfolders by default, so two developers editing different fields are less likely to overwrite each other's work.

What are the most useful Salesforce CLI commands?

Day-to-day release work comes down to a handful of commands. These are the ones release teams reach for most, and Salesforce's command reference lists every flag.

CommandWhat it does
sf project generate --name my-projectCreates a Salesforce DX project folder with the standard structure
sf org login web --alias devLogs in to an org in the browser and saves an alias for it
sf project retrieve start --metadata ApexClassPulls metadata from the org into your local project
sf project deploy start --source-dir force-appDeploys local source to the target org
sf project deploy validate --source-dir force-appChecks a deployment, including Apex tests, without saving any changes
sf project deploy quick --job-id <id>Deploys a validated job without running the tests again
sf apex run test --test-level RunLocalTestsRuns Apex tests in the org and reports the results

Retrieve brings metadata into a local folder, which you then commit to version control. Deploy sends it the other way, and the CLI converts the files back into the format the Metadata API expects.

For a sandbox or scratch org with source tracking, sf project deploy start and sf project retrieve start with no flags move only what has changed. Without tracking, you name what you want with flags or a package.xml manifest, which sf project generate manifest can build for you.

Before a big deploy, sf project deploy preview lists what will go to the org and flags potential conflicts. Deploys made from the CLI appear on the Deployment Status page in Setup, like any other Metadata API deployment.

How does the CLI fit a CI/CD pipeline?

In a pipeline, the same commands run on a server instead of a laptop. A typical job installs the CLI with npm, logs in with JWT, validates the change on a pull request and deploys it once the branch merges.

Terminal: validate, then quick deploy
# Validate the change and run local tests, without saving anything
sf project deploy validate \
  --source-dir force-app \
  --target-org prod \
  --test-level RunLocalTests

# Deploy the validated job without running the tests again
sf project deploy quick \
  --job-id 0AfXXXXXXXXXXXXXXX \
  --target-org prod

Our guide to building a CI/CD pipeline has a full GitHub Actions example. Which branch triggers which step is up to the team, and the common models are compared in our post on CI/CD branching strategy.

Production deploys have one rule no script can skip: Salesforce requires at least 75% Apex code coverage. Running RunLocalTests during validation catches a shortfall before release night.

Commands also accept a --json flag, which returns structured output instead of text meant for a person. That's how a pipeline reads a deploy's status or test results and decides whether to carry on.

Where does the CLI fall short?

Everything happens in a terminal, which suits developers more than admins who build in Setup. A pipeline built only on CLI scripts can leave part of the team outside the release process.

Rollback, approvals and an audit trail aren't CLI commands, so teams that need them script and maintain that logic themselves. The pipeline is also only one piece of release management, and Salesforce's own Lifecycle Center covers part of that ground.

Where Flosum fits

Flosum's DevOps Cloud App runs on Flosum-managed AWS or on a virtual machine you own in AWS, Azure or GCP. Admins and developers work in the same pipeline and Git is optional, so nobody has to learn the CLI to ship a change.

In the DevOps Cloud App, merge logic works component by component for each metadata type. It also adds one-click rollback, drift detection across orgs and dependency checks before release, the pieces a CLI-only pipeline leaves you to build.

FAQs

Is the Salesforce CLI free?

Yes. Salesforce distributes it at no charge, and its source code is public on GitHub.

What is the difference between sf and sfdx?

sfdx (v7) is the older, now deprecated release of the Salesforce CLI, and sf (v2) is the current one. You have to uninstall sfdx (v7) before installing sf (v2), and older sfdx-style commands still run afterwards.

Can admins use the Salesforce CLI?

Yes, as long as their user has API access to the org. The commands assume some comfort with a terminal and a local project folder, so many admins prefer a point-and-click release tool.

How do you update the Salesforce CLI?

If you used an installer, run sf update, per Salesforce's update guide. If you installed with npm, run the npm install command again to get the latest version.

Can the Salesforce CLI create scratch orgs?

Yes. Once Dev Hub is enabled, sf org create scratch builds a temporary org from a definition file, which teams use for short-lived development and testing.

Book a demo to see how Flosum DevOps runs Salesforce releases without CLI scripts.

Table Of Contents
Author
Stay Up-to-Date
Get flosum.com news in your inbox.

Thank you for subscribing

Ship Salesforce changes faster
Teams on Flosum report 40% higher development velocity and 75%
fewer merge conflicts.
Free to start  |  No credit card required