Sub topic 6
Installing the Salesforce CLI on the runner
This chapter assumes you know basic Git and GitHub usage, can read YAML, and have seen the sf commands for deploying and testing. Here you will make a GitHub Actions runner ready to run those commands.
Why the runner starts empty
A runner is a fresh virtual machine created for each job. It has Git, Node.js and many common tools, but you should not rely on it having the Salesforce CLI, or on it having the version you want. Every job must install the CLI itself, and the machine is thrown away when the job ends.
The CLI is distributed as the npm package @salesforce/cli. Installing it needs Node.js, so the workflow sets up Node first.
The install steps
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 versions
run: |
sf --version
sf plugins --coreEach step has a job:
actions/checkout@v4copies your repository onto the runner. Without it,force-appandsfdx-project.jsondo not exist.actions/setup-node@v4selects the Node.js version. Pinning it keeps runs repeatable.npm install --globalputs thesfcommand on the path for later steps.sf --versionprints the installed version into the log. This is a cheap way to confirm what a failing run actually used.
Pinning the CLI version
Without a version, npm installs the latest release. A new release can change output formats or flags and break a pipeline that did not change. Pin the version to avoid that:
- name: Install Salesforce CLI
run: npm install --global @salesforce/[email protected]The number above is only an example. Use the version you tested locally, and update it on purpose through a pull request. Running sf --version on your own machine shows what to match.
Speeding up repeated installs
Installing the CLI takes about a minute. You can cache npm downloads with the cache option of setup-node, but it needs a lock file in the repository. If you have no package-lock.json, skip caching at first. A global install is not covered by the cache, so the saving is small unless you install the CLI as a project dependency.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `sf: command not found` | The install step failed or ran in a different job | Install the CLI in every job that calls `sf` |
| `EACCES` permission error | Global install without write access, usually on a self-hosted runner | Use a user-level npm prefix or install locally |
| Plugin command not found | A plugin such as `sfdx-git-delta` was not installed | Add `sf plugins install` after the CLI install |
| Different output from your laptop | Version mismatch | Pin the version and compare `sf --version` |
Jobs do not share a file system. If you split work into two jobs, both need the checkout, Node setup and CLI install steps.
Flow of a prepared runner
Exercise
A workflow has two jobs, validate and test. Only validate has the step that installs the Salesforce CLI. The test job fails with sf: command not found. Why, and how do you fix it?
Show answer
Each job runs on its own fresh runner, so the CLI installed in validate does not exist in test. Add the checkout, Node.js setup and CLI install steps to the test job as well.
Exercise
Why is it better to pin @salesforce/cli to a specific version than to install the latest one in a shared pipeline?
Show answer
A new CLI release can change flags or output and break the pipeline without any change in your repository. A pinned version changes only when you update it in a pull request.