SonarQube: Code Quality Gates, Setup & CI Integration Guide
SonarQube is the industry-standard static analysis platform for tracking bugs, vulnerabilities, code smells, and test coverage across your codebase. This guide covers Docker setup, quality gate configuration, GitHub Actions integration, and how to read the reports that actually matter.
What Is SonarQube (vs ESLint and Semgrep)?
ESLint catches style and syntax issues within JavaScript files. Semgrep is a pattern-matching security scanner. SonarQube operates at a higher level: it tracks metrics over time, computes technical debt, measures test coverage, and enforces quality gates that block deployments. Unlike one-off linters, SonarQube maintains a persistent database of your project history so you can see whether quality is trending up or down.
| Tool | Primary Use | Tracks History | Quality Gates |
|---|---|---|---|
| ESLint | JS/TS style & syntax | No | Exit code only |
| Semgrep | Security patterns | Limited | Rule-based only |
| SonarQube | Full quality platform | Yes (DB) | Rich, metric-based |
Docker Quickstart
The fastest way to run SonarQube locally is with Docker Compose. This starts the server and a PostgreSQL database:
# docker-compose.yml
version: "3"
services:
sonarqube:
image: sonarqube:community
ports:
- "9000:9000"
environment:
SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
SONAR_JDBC_USERNAME: sonar
SONAR_JDBC_PASSWORD: sonar
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_USER: sonar
POSTGRES_PASSWORD: sonar
POSTGRES_DB: sonar
# Start with:
docker compose up -d
# Open http://localhost:9000 — default login admin/admin Connecting Your Project
After logging in, create a project and generate a token. Then run sonar-scanner from your project root:
npx sonar-scanner \
-Dsonar.projectKey=my-project \
-Dsonar.sources=src \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token=YOUR_TOKEN \
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info Quality Gates: Pass/Fail Criteria
A quality gate is a set of conditions your project must meet to pass. The built-in "Sonar Way" gate checks new code only — this is intentional: it lets you improve without being blocked by legacy debt. Common conditions to add:
- Coverage on new code >= 80%
- Duplicated lines on new code <= 3%
- Reliability Rating on new code = A (no new bugs)
- Security Rating on new code = A (no new vulnerabilities)
- Maintainability Rating on new code = A
GitHub Actions Integration
# .github/workflows/sonar.yml
name: SonarCloud Analysis
on: [push, pull_request]
jobs:
sonar:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # full history for blame data
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm ci && npm test -- --coverage
- uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.qualitygate.wait=true Reading the Reports
The SonarQube dashboard splits issues into four categories:
| Category | What it Means | Rating Scale |
|---|---|---|
| Bugs | Runtime defects | A (0) → E (many) |
| Vulnerabilities | Security weaknesses | A (0) → E (critical) |
| Code Smells | Maintainability debt | Time estimate |
| Coverage | % lines tested | Percentage |
SonarQube vs SonarCloud vs SonarLint
SonarLint is an IDE plugin (VS Code, IntelliJ) that gives real-time feedback as you type — it is the fastest feedback loop. SonarQube is self-hosted and used in CI pipelines. SonarCloud is the hosted SaaS version, free for public repos. In practice, teams run SonarLint locally for immediate feedback and SonarCloud/SonarQube in CI as a gate.
Related Products
on GumroadDeveloper Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →AI Prompt Engineering Toolkit
58 structured prompt templates for Claude, ChatGPT, and Gemini — organized by use case. Code Review, Debugging, Documentation, Architecture, and 10 model-specific tips. Stop writing prompts from scratch.
Buy on Gumroad →Developer Cheat Sheet Bundle
10 print-ready cheat sheets for the tools developers use every day: Git, Docker, Linux CLI, SQL, Regex, HTTP Status codes, VS Code shortcuts, Bash scripting, jq/JSON, and Markdown.
Buy on Gumroad →You Might Also Need
JSON Formatter & Validator
Format, beautify, and validate JSON online. Paste your JSON to instantly pretty-print it with 2-space, 4-space, or tab indentation. Minify JSON too. Free, runs in your browser.
Open free tool →
Regex Tester
Free regex tester. Test regular expressions against multiple strings with expected match/no-match assertions — see instant pass/fail results. Built-in presets for email, URL, IPv4, and phone. Pro: unl
Open free tool →
Base64 Encoder & Decoder
Free Base64 Encoder & Decoder. Convert text, images, and any file to Base64 and back. Supports URL-safe Base64, image data URIs, and file download from Base64. Runs 100% in your browser — no data sent
Open free tool →
More Free Tools
Related Articles
curl 深度使用手冊:API 測試從入門到進階
curl 是每位開發者必備的 CLI 工具。本文涵蓋 GET/POST/PUT/DELETE 請求、認證、header 設定、檔案上傳、除錯技巧,以及 REST API 測試的完整實戰範例。
Best AI Code Review Tools 2026: CodeRabbit, Sourcery, DeepCode & Bito Compared
Compare top AI code review tools for 2026: CodeRabbit, Sourcery, DeepCode, and Bito. Features, pricing, GitHub/GitLab integration, and choosing the right tool for your team.
AI Code Review Tools 2026: GitHub Copilot, CodeRabbit, Qodo Merge Ranked
Compare AI code review tools for 2026: GitHub Copilot code review, CodeRabbit, Qodo Merge, Sourcery, and PR-Agent. Pricing, accuracy, and GitHub integration.
AI Coding Tools Comparison 2026: GitHub Copilot vs Cursor vs Windsurf vs Cline
Comprehensive comparison of the best AI coding tools in 2026: GitHub Copilot, Cursor, Windsurf, and Cline. Pricing, features, model support, and real-world benchmarks.
AI Pair Programming in 2026: GitHub Copilot vs Cursor vs Claude Code
Master AI pair programming in 2026. Compare GitHub Copilot, Cursor, and Claude Code for autocomplete, chat, and agentic workflows. Includes setup guides and productivity tips.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →Frequently Asked Questions
What is the difference between SonarQube Community and Developer edition?▼
Community Edition is free and open-source. It supports single-branch analysis for Java, JavaScript, Python, and 15+ languages, making it sufficient for most open-source projects and small teams. Developer Edition adds multi-branch and pull request analysis, deeper security rules (taint analysis), and additional language support. It is a paid tier aimed at professional teams that need per-PR quality gates.
How do I configure a quality gate to fail CI?▼
In the SonarQube UI, go to Quality Gates and create or edit a gate. Add conditions such as 'Coverage on new code is less than 80%' or 'Reliability Rating on new code is worse than A'. Then in your GitHub Actions workflow, add sonarcloud-github-action or sonar-scanner with the sonar.qualitygate.wait=true property. The step will exit with a non-zero code when the gate fails, blocking the merge.
What is technical debt in SonarQube?▼
Technical debt is an estimate of the effort (in minutes or hours) required to fix all code smells in the codebase. SonarQube calculates it using a SQALE method: each code smell type has a remediation cost, and the total is summed. The Maintainability Rating (A–E) is derived from the debt ratio — debt divided by total development cost. It gives teams a concrete, actionable number rather than a vague quality score.
What is the difference between SonarCloud and SonarQube?▼
SonarQube is self-hosted software you run on your own infrastructure (or Docker). SonarCloud is the SaaS version hosted by Sonar. SonarCloud is free for public repositories and requires no server maintenance, making it ideal for open-source projects. SonarQube gives you full control over data, custom plugins, and on-premise deployment needed for compliance-sensitive environments.
How do I integrate SonarQube with GitHub Actions?▼
Add a workflow step that runs sonar-scanner with your project key and host URL. For SonarCloud, use the SonarSource/sonarcloud-github-action action. Pass SONAR_TOKEN as a secret. Set sonar.projectKey, sonar.organization (Cloud only), sonar.sources, and sonar.javascript.lcov.reportPaths for coverage. Add sonar.qualitygate.wait=true to make the step block until the quality gate result is returned.
What are code smells vs bugs vs vulnerabilities in SonarQube?▼
Bugs are code that is likely to behave unexpectedly or produce wrong results at runtime — for example, a null dereference or an unreachable catch block. Vulnerabilities are security weaknesses that an attacker could exploit, such as SQL injection or hardcoded credentials. Code smells are maintainability issues that are not immediately harmful but increase technical debt — duplicated code, overly complex methods, or unused variables.