Skip to main content

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.

✓ Docker quickstart in under 5 minutes ✓ GitHub Actions CI integration ✓ SonarCloud free for public repos ✓ Quality gate pass/fail automation

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:

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.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

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.