Sub topic 14
Adding Code Analyzer to a CI pipeline
This chapter assumes you can run sf code-analyzer run locally, know the severity levels, and have used a basic GitHub Actions workflow. Here you move the same scan into a pull request check.
Why run the scan in CI
A local scan depends on each developer remembering to run it. A CI job runs the same scan on every pull request, so serious findings are caught before merge. Three earlier features make this work:
- The rule selector picks a stable set of rules.
- The severity threshold turns findings into a pass or fail result.
- A file output keeps the report for later review.
The workflow
This GitHub Actions workflow scans on pull requests. It fails the job on severity 2 (High) or worse, and it uploads the SARIF report even when the scan fails.
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.sarifHow each step works
actions/checkoutcopies your repository onto the runner. Without it,force-appdoes not exist and there is nothing to scan.actions/setup-nodeprovides Node.js, which the Salesforce CLI needs. The scan engines also run on top of it.- The two
runlines install the CLI and the plugin. A fresh runner has neither. - The scan step uses
--severity-threshold 2. If any violation has severity 2 or a more severe level, the command exits with a nonzero status. GitHub Actions treats that as a failed step and fails the job. --output-file results.sarifwrites the report. The file extension selects the format, and SARIF is a standard format that other tools can read.- The upload step has
if: always(). By default, a step is skipped once an earlier step has failed. Without this condition, the report would be lost in exactly the case where you need it.
Using the configuration file
Your team's settings should live in code-analyzer.yml in the repository, so local runs and CI behave the same way. Add --config-file code-analyzer.yml to the scan step, or keep the file in the project root so it is found automatically. Ignored paths and severity overrides then apply to both.
Common errors
- The report is missing from the run. The upload step lacks
if: always(), or the scan failed before it wrote the file. - The job fails on old findings. The first run on an existing codebase often reports many violations. Start with a higher threshold such as
1, fix those findings, then tighten it. Alternatively, ignore generated or third-party folders in the configuration file. - The job passes but violations exist. The threshold is set too low in severity. A threshold of
2ignores severity 3 to 5 findings. These still appear in the report. - Results differ between your laptop and CI. The plugin versions differ. Pin a version when you install, for example
sf plugins install code-analyzer@latestat a known release, to keep results stable.
Exercise
Your scan step fails because of a High finding, and the pipeline has no uploaded report to inspect. What is the likely cause, and how do you fix it?
Show answer
The upload step is skipped because a failed step stops later steps by default. Add if: always() to the upload step so it runs whether the scan passes or fails.
Exercise
You want the job to fail only on severity 1 (Critical) findings while still reporting all findings. Which option do you change?
Show answer
Change --severity-threshold 2 to --severity-threshold 1. The report still lists every violation, and only severity 1 findings produce a nonzero exit status.