Sub topic 17
Adding static analysis and quality gates
This chapter assumes you can read a GitHub Actions workflow, run sf commands, and know what Apex tests and code coverage are. Tests tell you whether code behaves correctly. Static analysis checks the source without running it, and it catches problems tests often miss, such as SOQL inside loops, unused variables, or insecure patterns.
What a quality gate is
A quality gate is a check that must pass before a change can merge. In GitHub, you make a check a gate by marking its job as a required status check in the branch protection rules for main. If the job fails, the pull request cannot merge.
Running Salesforce Code Analyzer
Salesforce Code Analyzer is a CLI plugin that runs several rule engines, including PMD for Apex and ESLint for Lightning Web Components. Install it with sf plugins install code-analyzer, then run it against your source folder.
The workflow below runs on pull requests. It fails the job when any violation has severity 2 or higher, where 1 is the most severe.
name: Static analysis
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI and Code Analyzer
run: |
npm install --global @salesforce/cli
sf plugins install code-analyzer
- name: Run Code Analyzer
run: >
sf code-analyzer run
--workspace force-app
--severity-threshold 2
--output-file results.csv
- name: Upload report
if: always()
uses: actions/upload-artifact@v4
with:
name: code-analyzer-results
path: results.csvThe --severity-threshold 2 option makes the command exit with a non-zero code when it finds violations at that severity or worse. A non-zero exit code is what fails the step. The if: always() condition on the upload step keeps the report available even when the analysis step fails.
Choosing gates
A pipeline usually combines several gates. Each one answers a different question.
| Gate | Question it answers | Where it runs |
|---|---|---|
| Code Analyzer | Does the code follow quality and security rules? | Pull request |
| Check-only deploy | Does the change deploy cleanly? | Pull request |
| Apex tests with coverage | Does the code behave correctly? | Pull request and merge |
| Environment approval | Has a person agreed to release? | Production deploy |
Run the cheap, fast gates first. Static analysis needs no org connection, so it can run as a separate job in parallel with the validation job and give feedback within a minute or two.
Keeping gates useful
- Start with a threshold your existing code passes, then tighten it. A gate that fails on day one gets disabled.
- Fix or suppress each false positive deliberately, with a comment explaining why.
- Keep gates required. An optional check is only advice.
Exercise
Your static analysis job fails on a pull request, but the PR can still be merged. What setting is probably missing, and why does the report still upload when the analysis step fails?
Show answer
The job is not listed as a required status check in the branch protection rule for main. The upload step uses if: always(), so it runs even after an earlier step fails.