Sub topic 14
Adding Code Analyzer to a CI Pipeline
This chapter shows how to run Code Analyzer on every pull request. It covers how to fail a job on serious findings, keep the report, and avoid the common mistakes that make CI scans noisy or useless.
Prerequisites
You should already know how to:
- Run
sf code-analyzer runlocally with a workspace and a rule selector. - Read severity levels (1 is Critical, 5 is Info) and report formats.
- Edit a
code-analyzer.ymlfile. - Read a basic GitHub Actions workflow. The examples use GitHub Actions, but the same commands work in any CI system that can run Node.js.
Why scan in CI
A local scan depends on each developer remembering to run it. A CI scan runs the same command, with the same configuration, on every change. It gives you three things:
- A consistent gate. A pull request with a serious violation cannot merge silently.
- A record. The report is stored with the build, so reviewers can read it without rerunning anything.
- A shared baseline. The rule selector, severity threshold, and
code-analyzer.ymllive in the repository, so everyone is judged by the same rules.
The three pieces of a CI scan
Every CI integration combines three features you have already seen:
| Piece | Option or file | Role in CI |
|---|---|---|
| Rule selection | `--rule-selector` | Decides which rules run |
| Failure gate | `--severity-threshold` | Makes the command exit nonzero |
| Report | `--output-file` | Produces a file the job can upload |
The configuration file ties them together by keeping project-wide decisions (ignored paths, severity overrides) out of the command line.
A complete GitHub Actions workflow
This workflow scans on pull requests, fails on severity 2 or worse, and uploads a SARIF report.
name: code-analysis
on: [pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install --global @salesforce/cli
- run: sf plugins install code-analyzer
- run: sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2 --output-file results.sarif
- uses: actions/upload-artifact@v4
if: always()
with:
name: code-analyzer-results
path: results.sarifWalk through it step by step:
on: [pull_request]triggers the job whenever a pull request is opened or updated.actions/checkout@v4copies the repository onto the runner. Without it there is noforce-appfolder to scan.actions/setup-node@v4provides Node.js 20. The Salesforce CLI and its plugins need Node.js.- The two install steps put
sfand the Code Analyzer plugin on the runner. A fresh runner starts empty each time, so these steps run on every build. - The
runstep does the scan. If any violation has severity 2 or more severe, the command exits with a nonzero status and the step fails. - The upload step has
if: always(). This detail matters, and the next section explains why.
Why `if: always()` matters
GitHub Actions skips later steps once a step fails, unless the later step says otherwise. The scan step fails exactly when it finds serious violations. That is also the moment when you most need the report.
Without if: always(), the upload step would be skipped on failure, and the reviewer would see a red check with no file to explain it. With it, the report is uploaded whether the scan passes or fails.
The output file extension selects the format. results.sarif produces SARIF (Static Analysis Results Interchange Format), a JSON-based standard that many code-hosting tools can read. Other extensions such as .html, .csv, .json, and .xml produce those formats.
Choosing a threshold
The severity threshold decides how strict the gate is. Lower numbers are more severe.
| Threshold | Fails the job on | Typical use |
|---|---|---|
| `1` | Critical only | Very noisy codebases adopting the tool for the first time |
| `2` | Critical and High | A sensible default for most teams |
| `3` | Critical, High, and Moderate | Mature projects with a clean baseline |
Without --severity-threshold, the command reports violations but still exits with status 0 for most findings, so the job passes. A scan that never fails is a report, not a gate. Decide on purpose which one you want.
Start with a threshold your current code already meets. If the codebase has 40 severity 2 violations today and you set the threshold to 2, every pull request fails until someone fixes all 40. Teams then tend to disable the check. Instead, begin at 1, fix the High findings over time, and lower the number step by step.
Keeping the command short with a config file
As the command line grows, move shared decisions into code-analyzer.yml at the repository root and point to it explicitly:
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
rules:
pmd:
AvoidDebugStatements:
severity: "Info"sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector Recommended --severity-threshold 2Because the file is committed, a change to the rules goes through code review like any other change. Developers get the same results locally and in CI, which prevents the "it passes on my machine" argument.
Common errors and how to fix them
The job passes but you expected a failure
Check that --severity-threshold is present. Then check that the violation is not in an ignored path. A violation in staticresources stops counting once that folder is in ignores.files. Run the same command locally with --view table to see what the scan found.
The job fails with "command not found" for `sf`
The runner does not have the CLI. Make sure the install step comes before the scan step and that Node.js is set up first. In other CI systems, confirm the global npm bin directory is on PATH.
Results differ between local and CI
Usual causes:
- A different plugin version. Locally you may have an older or newer release than the runner, which installs the latest. If you need repeatable results, pin the version in the install step (for example, with
sf plugins install code-analyzer@<version>, using a real version number from the plugin's release page). - A different working directory, which changes how
--workspacepaths resolve. - A configuration file that exists locally but is not committed.
The scan is slow
Scanning the whole workspace on every pull request grows costly in a large repository. Two fixes help:
- Use
--targetto limit the scan to changed files. Your CI system can list them, for example withgit diff --name-only, and you can pass them to the command. - Keep heavy analysis, such as the graph engine's data-flow checks, for a nightly or main-branch job instead of every pull request.
The upload step reports "No files were found"
The scan crashed before writing a report, or the path does not match the --output-file value. Both must name the same file.
Where this fits in a team workflow
A practical setup uses two jobs:
- A pull request job that uses the Recommended selector, a threshold of 2, and a SARIF upload. It stays fast and blocks only serious problems.
- A scheduled or main-branch job that runs a broader selector, perhaps including the graph engine, and reports without blocking. It surfaces slower, deeper findings for later cleanup.
You can also mark the scan job as a required status check in your repository's branch protection settings. Without that setting, a red check is advice, and anyone with merge rights can ignore it.
Exercise
A team's workflow runs sf code-analyzer run --workspace force-app --rule-selector Recommended --output-file results.sarif and then an upload step with if: always(). A pull request adds a severity 1 violation, yet the job is green. What is missing, and what else should be checked so the failure is visible and branch merging is blocked?
Show answer
The command has no --severity-threshold, so violations do not cause a nonzero exit status. Add --severity-threshold 2 (or 1 to start stricter only on Critical). Also confirm the violating file is not matched by an ignores.files pattern in code-analyzer.yml, and mark the scan job as a required status check in branch protection so a failing job blocks the merge.
Exercise
Why does the upload step use if: always(), and what happens to your debugging if you remove it?
Show answer
GitHub Actions skips later steps after a failure by default. The scan step fails exactly when it finds violations at or above the threshold. Without if: always(), the report would not be uploaded in that case, so reviewers would see a failed check with no report explaining it.