SonarQube
Continuous code quality and security analysis. Detect bugs, vulnerabilities, and code smells across 30+ programming languages with automated static analysis.
π What is SonarQube?
SonarQube is an open-source platform for continuous inspection of code quality. It performs automatic reviews with static analysis to detect bugs, code smells, and security vulnerabilities on 30+ programming languages. It integrates seamlessly with CI/CD pipelines to enforce quality gates before code reaches production.
Bug Detection
Find issues early
Security
Vulnerability scanning
Code Smells
Maintainability
Coverage
Test tracking
π‘ Key Insight: SonarQube follows a "Clean as You Code" methodology β focus on keeping new code clean rather than fixing legacy issues all at once.
π Core Concepts
π Bugs
π Vulnerabilities
π§Ή Code Smells
π‘οΈ Security Hotspots
π¦ Quality Gate
A Quality Gate is a set of conditions that determine whether your code is ready for production. If any condition fails, the gate fails and the pipeline should be blocked.
| Metric | Condition | Default Threshold |
|---|---|---|
| New Bugs | Must be | 0 |
| New Vulnerabilities | Must be | 0 |
| New Code Smells | Severity | A rating |
| Code Coverage | New code β₯ | 80% |
| Duplications | New code β€ | 3% |
| Security Hotspots | Reviewed | 100% |
β Gate Passed
All conditions met. Pipeline continues to deployment. New code meets quality standards.
β Gate Failed
One or more conditions not met. Pipeline blocked. Developer must fix issues before merging.
π Supported Languages
βοΈ CI/CD Integration
GitHub Actions
# .github/workflows/sonar.yml
name: SonarQube Analysis
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for accurate blame
- name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@v3
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- name: Quality Gate Check
uses: SonarSource/sonarqube-quality-gate-action@v1
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
π sonar-project.properties
# Project Configuration
sonar.projectKey=my-genai-project
sonar.projectName=GenAI Application
sonar.projectVersion=1.0
# Source directories
sonar.sources=src
sonar.tests=tests
# Language-specific settings
sonar.python.version=3.11
sonar.python.coverage.reportPaths=coverage.xml
# Exclusions
sonar.exclusions=**/node_modules/**,**/venv/**,**/__pycache__/**
sonar.test.exclusions=**/tests/**
# Encoding
sonar.sourceEncoding=UTF-8
π³ Run SonarQube Locally with Docker
# Start SonarQube server
docker run -d --name sonarqube \
-p 9000:9000 \
-v sonar_data:/opt/sonarqube/data \
-v sonar_logs:/opt/sonarqube/logs \
sonarqube:community
# Access at http://localhost:9000
# Default credentials: admin / admin
# Run scanner on your project
docker run --rm \
-e SONAR_HOST_URL="http://host.docker.internal:9000" \
-e SONAR_TOKEN="your-token-here" \
-v "$(pwd):/usr/src" \
sonarsource/sonar-scanner-cli
π€ SonarQube for GenAI/ML Projects
AI/ML projects benefit greatly from static analysis. Common issues SonarQube catches in GenAI codebases:
β Common Issues Found
- β’ Hardcoded API keys (OpenAI, Azure, AWS)
- β’ Unhandled exceptions in API calls
- β’ Resource leaks (file handles, DB connections)
- β’ Insecure HTTP calls to model endpoints
- β’ Missing input validation on user prompts
β Best Practices Enforced
- β’ Environment variables for secrets
- β’ Proper error handling and retries
- β’ Context managers for resources
- β’ HTTPS for all external API calls
- β’ Input sanitization and validation
β οΈ Warning: SonarQube detects hardcoded secrets with high accuracy. Always use environment variables or a secrets manager (Vault, AWS Secrets Manager, GCP Secret Manager) for API keys in GenAI applications.
π Editions Comparison
| Feature | Community (Free) | Developer | Enterprise |
|---|---|---|---|
| Languages | 15+ | 25+ | 30+ |
| Branch Analysis | β | β | β |
| PR Decoration | β | β | β |
| SAST / Taint Analysis | Basic | β | β |
| Portfolio Management | β | β | β |
| Pricing | Free | From $150/yr | From $20K/yr |
π Key Metrics & Ratings
Reliability Rating
- A β 0 bugs
- B β 1 minor bug
- C β 1 major bug
- D β 1 critical bug
- E β 1 blocker bug
Security Rating
- A β 0 vulnerabilities
- B β 1 minor vuln
- C β 1 major vuln
- D β 1 critical vuln
- E β 1 blocker vuln
Maintainability Rating
- A β Technical debt ratio β€ 5%
- B β 6-10%
- C β 11-20%
- D β 21-50%
- E β > 50%
β Best Practices
Do's
- Enforce Quality Gates in CI/CD pipeline
- Focus on "Clean as You Code" (new code first)
- Review Security Hotspots regularly
- Set up PR decoration for developer feedback
- Track code coverage trends over time
Don'ts
- Ignore Quality Gate failures
- Suppress warnings without understanding them
- Skip security reviews for "internal" projects
- Set coverage thresholds too low (< 60%)
- Exclude entire directories to pass the gate
βοΈ SonarQube vs Alternatives
| Tool | Type | Self-Hosted | Best For |
|---|---|---|---|
| SonarQube | SAST + Quality | β | Full code quality + security |
| SonarCloud | SaaS | β | Cloud-native teams |
| CodeClimate | Quality | β | Maintainability focus |
| Snyk | Security | β | Dependency vulnerability scanning |
| ESLint / Pylint | Linting | β | Language-specific linting |
π Learn More
Related Topics
Test Your Knowledge
Score 8/10 or higher to pass
You need to be logged in to take this quiz.
Login to Continue