Prompt
How do I set up a static code analysis tool for multi-language source scanning in our CI/CD workflow?
Latest observation
To set up a static code analysis tool for multi-language source scanning in your CI/CD workflow, follow this practical approach:
1) Choose a tool that supports your languages
Pick a scanner that can analyze the languages you use across repos, for example:
- SonarQube / SonarCloud — broad multi-language support, good CI integration
- Semgrep — flexible, fast, strong for custom rules and security checks
- CodeQL — excellent security analysis, especially for supported languages
- Snyk Code — developer-friendly, security focused
If you need one tool for many languages and CI integration, SonarQube or Semgrep are common starting points.
2) Define what you want to scan
Decide whether you want to scan for:
- Code quality: bugs, code smells, duplication
- Security issues: injections, secrets, unsafe APIs
- Style/standards: lint-like checks
- Dependency issues: vulnerable packages
For multi-language repositories, make sure the tool supports each language you actually use, such as:
- JavaScript / TypeScript
- Python
- Java
- C#
- Go
- PHP
- Ruby
3) Integrate it into the CI pipeline
Add the scanner as a stage in your build pipeline, typically:
- Checkout code
- Install dependencies
- Build/test (optional but often recommended)
- Run static analysis
- Publish results
- Fail or warn based on policy
Example CI stage layout
buildteststatic_analysisdeploy
4) Install and configure the scanner
Most tools require:
- a configuration file
- authentication token or server URL
- project/repo identifiers
- language-specific settings if needed
Example: SonarQube config
Create a sonar-project.properties file:
sonar.projectKey=my-app
sonar.projectName=My App
sonar.sources=src
sonar.exclusions=**/node_modules/**,**/dist/**,**/build/**
sonar.tests=test
sonar.sourceEncoding=UTF-8
Then run in CI:
sonar-scanner \
-Dsonar.host.url=https://sonar.example.com \
-Dsonar.login=$SONAR_TOKEN
5) Add language-specific handling
For multi-language scanning, ensure the tool can detect the correct language directories and file types.
Examples:
- Exclude generated files and vendor directories
- Set separate source roots if your repo is polyglot
- Include language-specific build steps if needed
Example exclusions
sonar.exclusions=**/node_modules/**,**/vendor/**,**/target/**,**/dist/**,**/*.min.js
6) Handle secrets and credentials securely
Store tokens in your CI/CD secret manager, not in source code.
Examples:
- GitHub Actions Secrets
- GitLab CI variables
- Jenkins credentials store
- Azure DevOps variable groups
7) Make results actionable
Configure the tool to:
- annotate pull requests
- upload reports to a dashboard
- fail builds only on critical issues initially
- gradually tighten thresholds
Recommended rollout:
- Start in warning-only mode
- Fix existing high-severity issues
- Enforce quality gates on new code
- Expand rules over time
8) Example CI/CD integrations
GitHub Actions example
name: Static Analysis
on:
push:
branches: [main]
pull_request:
jobs:
scan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- name: Run SonarScanner
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
sonar-scanner \
-Dsonar.host.url=https://sonar.example.com \
-Dsonar.login=$SONAR_TOKEN
GitLab CI example
static_analysis:
image: sonarsource/sonar-scanner-cli
script:
- sonar-scanner -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN
only:
- merge_requests
- main
9) Tune performance for large repos
For large multi-language repos:
- scan only changed paths when possible
- cache dependencies and scanner data
- exclude generated artifacts
- run incremental analysis on PRs, full scans nightly
10) Establish governance
Define policies for:
- severity thresholds
- who can override failures
- how issues are triaged
- SLAs for fixing critical findings
- branch protection requiring successful analysis
Recommended implementation pattern
A practical rollout plan:
- Pick one scanner
- Run it locally first
- Add a minimal CI job
- Exclude generated/vendor directories
- Publish reports to a dashboard
- Enable pull request checks
- Enforce quality gates gradually
If you want, I can also provide:
- a ready-to-use GitHub Actions workflow
- a GitLab CI template
- a Jenkins pipeline
- or a tool comparison for SonarQube vs Semgrep vs CodeQL