Sub topic 6
Installing the Salesforce CLI on the runner
Prerequisites and scope
This chapter assumes you can read a GitHub Actions workflow file and know what a job and a step are. It also assumes you have used the sf command locally, for example sf project deploy start. Here you will learn how to get sf onto a GitHub-hosted runner, keep its version predictable, make installs faster, and check that it works before any step needs an org.
A runner is a fresh virtual machine that GitHub starts for each job. It has Git, Node.js, and many common tools, but it does not have the Salesforce CLI. Every job that calls sf must install it first, because nothing carries over from one job to the next.
Choosing an install method
The Salesforce CLI is published as the npm package @salesforce/cli. The sf executable comes from that package. There are three common ways to get it onto a runner.
| Method | How it works | Strengths | Weaknesses |
|---|---|---|---|
| `npm install --global @salesforce/cli` | Installs from the npm registry | Simple, works on any runner with Node.js, easy to pin a version | Downloads on every run unless you cache |
| Project-local `npm install` | Lists the CLI in `package.json` and installs with `npm ci` | The version is committed with the code, so local and CI match | Needs a `package.json` and lockfile, and you call it with `npx sf` or the `node_modules/.bin` path |
| Container image | Runs the job inside an image that already contains the CLI | No install step | You maintain or trust a third-party image, and image tags can drift |
For most teams the first method is the right starting point, and it is what the example workflows in this tutorial use. Move to the project-local method when you want one version for every developer and the pipeline.
The basic install
The npm package requires Node.js. The ubuntu-latest runner includes Node.js, but its version can change when GitHub updates the image. Use actions/setup-node so that the version is stated in your workflow.
name: Check CLI install
on:
workflow_dispatch:
jobs:
cli-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Show CLI version
run: sf --versionThe workflow_dispatch trigger lets you start the workflow by hand from the Actions tab. That makes it a safe way to test an install change without opening a pull request.
The --version step is cheap and worth keeping. If the install fails or sf is not on the PATH, the failure shows up here with a clear message instead of in the middle of a deployment.
Pinning the version
An unpinned install gets the latest release at the time of the run. That is convenient, but a new release can change flags, output formats, or default behavior. A pipeline that passed yesterday can then fail today with no change in your code.
You can pin a version in the install command.
- name: Install Salesforce CLI (pinned)
run: npm install --global @salesforce/[email protected]Replace 2.60.0 with a release you have tested. You can also use a distribution tag. The latest tag is the default, and the latest-rc tag gives you release candidates, which is useful in a separate workflow that warns you about upcoming breaking changes.
| Choice | Behavior | Use when |
|---|---|---|
| No version | Newest stable release on each run | Prototyping, or when you want to follow changes closely |
| Exact version such as `@2.60.0` | Same release every run | Production pipelines where stability matters |
| `@latest-rc` | Release candidate | A scheduled canary workflow that detects problems early |
Using a project-local install
If your repository already has a package.json, you can add the CLI as a development dependency. This keeps the version in source control, and a pull request that raises it is reviewed like any other change.
{
"name": "salesforce-project",
"private": true,
"devDependencies": {
"@salesforce/cli": "2.60.0"
}
}Run npm install once locally to create package-lock.json, and commit both files. In the workflow, use npm ci, which installs exactly what the lockfile lists and fails if the files disagree.
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Show CLI version
run: npx sf --versionLater steps must then call npx sf ... instead of plain sf ..., or add ./node_modules/.bin to the PATH. Mixing the two styles is a common source of confusion, because a global sf and a local sf can be different versions.
Caching to speed up installs
The CLI is a large package, and a global install can take a noticeable part of a short job. The cache option of actions/setup-node stores npm's download cache between runs, keyed by your lockfile.
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npmThis option requires a lockfile in the repository, so it fits the project-local method. With a global install and no lockfile, the action cannot compute a cache key and the step fails. In that case, either leave cache out or switch to the project-local method.
Caching saves download time but does not remove the install itself, since npm still unpacks the files. Measure before you optimize. For a pipeline that runs a few times a day, a plain install is usually fine.
Plugins
Some pipeline steps need CLI plugins, such as sfdx-git-delta for delta deployments, which a later chapter covers. Plugins are installed with sf plugins install after the CLI itself.
sf plugins install sfdx-git-delta
sf pluginsTwo points matter on a runner. First, plugins live outside your repository, so you must install them in every job that uses them. Second, an unpinned plugin install has the same drift risk as an unpinned CLI. Pin it with sf plugins install [email protected], using a version you have tested, or list it as a dependency in package.json.
Where the CLI runs: the job lifecycle
The following diagram shows why each job needs its own install. Jobs run on separate machines, so the second job cannot reuse the CLI from the first.
If two jobs share the same steps, you can avoid copying them by writing a reusable workflow or a composite action. Both are beyond this chapter. For now, repeating three short steps in each job is acceptable.
Common errors
sf: command not found. The install step did not run, failed, or ran in a different job. Check that the install step is in the same job as the step that callssf. With a project-local install, usenpx sf.npm ERR! EACCESor permission errors. This usually appears on self-hosted runners where global installs need elevated rights. GitHub-hosted runners do not have this problem. On a self-hosted runner, set an npm prefix in the user's home directory or use the project-local method.- Unsupported Node.js version. The CLI requires a current long-term-support Node.js release. If the install warns about the engine version, raise
node-versioninactions/setup-node. - Dependency cache errors. The message "Dependencies lock file is not found" means
cache: npmis set but nopackage-lock.jsonis committed. Commit the lockfile or remove the option. - Behavior changed with no code change. An unpinned CLI or plugin updated between runs. Compare the version printed by
sf --versionin a passing run and a failing run.
Security note on installing tools
An install step runs code from a package registry on a machine that will soon hold org credentials. Keep the install steps before the authentication step, pin versions, and install only the plugins you need. Review any plugin before adding it, because it runs with the same access as the CLI. The next chapters cover how credentials are stored and used.
Exercises
Exercise
Your workflow has two jobs, validate and deploy. The validate job installs the Salesforce CLI. The deploy job runs sf project deploy start and fails with "sf: command not found". Why does this happen, and what is the fix?
Show answer
Each job runs on its own fresh runner, so the CLI installed in validate does not exist in deploy. Add the Node.js setup step and the npm install --global @salesforce/cli step to the deploy job (or move the shared steps into a reusable workflow or composite action).
Exercise
A pipeline that passed on Monday fails on Thursday with an error about an unknown flag, and nobody changed the repository. Name the likely cause and two changes that would make the pipeline stable.
Show answer
The CLI was installed without a version, so Thursday's run got a newer release whose flags differ. Pin the version in the install command (for example @salesforce/[email protected]) or list it in package.json with a committed lockfile and install it with npm ci. Pinning plugins and printing sf --version in the log also help.
Exercise
You add cache: npm to actions/setup-node in a workflow that installs the CLI globally and has no package-lock.json. The step fails. Why, and how can you fix it?
Show answer
The npm cache key is computed from the lockfile, and none exists, so the action reports that the dependencies lock file was not found. Either remove the cache option, or add a package.json with the CLI as a dev dependency, commit package-lock.json, and install with npm ci.