This is a complete, hands-on SonarQube cheat sheet: install, configure, operate, automate and secure it - with copy-paste commands grouped by task. Bookmark it for day-to-day work, incidents and interviews alike.
SonarQube statically analyzes code for bugs, vulnerabilities, security hotspots and 'code smells', and enforces a Quality Gate in CI so problematic changes fail the build.
Why you reach for it
- Catches bugs and vulnerabilities before review
- Quality Gates enforce a consistent bar across teams
- Tracks tech-debt and coverage trends over time
Installation
docker run -d --name sonarqube -p 9000:9000 sonarqube:community
# open http://localhost:9000 (admin/admin)Configuration reference
Project configuration file.
# sonar-project.properties
sonar.projectKey=acme-web
sonar.sources=src
sonar.tests=test
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.qualitygate.wait=trueCore commands you use daily
sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.token=$SONAR_TOKENScan & gate
sonar-scanner \
-Dsonar.projectKey=acme-web \
-Dsonar.sources=src \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.qualitygate.wait=trueQuality Gate via API
curl -s -u $SONAR_TOKEN: \
"http://localhost:9000/api/qualitygates/project_status?projectKey=acme-web" | jq .projectStatus.statusUse it in CI/CD
Scan and fail on the Quality Gate.
- uses: sonarsource/sonarqube-scan-action@v3
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- uses: sonarsource/sonarqube-quality-gate-action@v1Best-practice checklist
- Enforce a 'new code' Quality Gate on every PR
- Wire coverage reports into the scan
- Triage security hotspots, don't blanket-ignore them
Common pitfalls to avoid
- Analyzing but not enforcing the Quality Gate, so nothing improves
- Chasing a green gate by disabling rules instead of fixing issues
- No coverage report wired in, so coverage looks like zero
Security quick wins
- Treat vulnerabilities and hotspots as gating, not advisory
- Keep tokens scoped and rotate them
Works well with
- Trivy - A fast, all-in-one scanner for container images, filesystems, IaC and Kubernetes.
- GitHub Actions - CI/CD built into GitHub - event-driven workflows defined in YAML, with a huge marketplace of actions.
- GitLab CI/CD - Pipelines-as-code baked into GitLab, with built-in registry, environments and security scanning.
A cheat sheet is muscle memory on paper - keep it close until the commands live in your fingers.
Want senior help putting SonarQube to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.