Your team just adopted SonarQube and the initial scan shows 5,000 issues. Management wants to gate deployments on code quality, but developers are overwhelmed. How do you configure Quality Gates to balance quality enforcement with developer productivity?
Quick Answer
Implement the 'Clean as You Code' approach: configure the Quality Gate to only enforce standards on NEW code (new issues = 0, new coverage > 80%), while treating existing issues as technical debt to address incrementally. This prevents blocking deployments for legacy issues while ensuring all new code meets standards.
Detailed Answer
Clean as You Code Philosophy
SonarQube's recommended approach is 'Clean as You Code' - enforce strict quality on new code added in each analysis period while accepting that legacy code has existing debt. The Quality Gate evaluates conditions on the 'New Code' period (since last version, previous version, or a specific date). This means developers are only responsible for the quality of code they write, not for fixing thousands of pre-existing issues.
Quality Gate Configuration
The default 'Sonar way' Quality Gate enforces: zero new bugs, zero new vulnerabilities, zero new security hotspots reviewed (all must be reviewed), new code coverage > 80%, and new code duplication < 3%. These conditions apply only to the New Code period. You can customize thresholds per project or globally. For legacy codebases, start with relaxed thresholds and tighten over time.
Graduated Enforcement
Phase 1 (weeks 1-4): Quality Gate in 'advisory' mode - fails in SonarQube but doesn't block the pipeline. Developers learn the standards. Phase 2 (weeks 5-8): Quality Gate blocks PR merges via PR decoration but doesn't block deployments. Phase 3 (week 9+): Quality Gate blocks the deployment pipeline via waitForQualityGate abortPipeline: true. This graduated approach prevents developer revolt.
Handling False Positives
SonarQube will flag some false positives. Train developers to mark these as 'Won't Fix' with a justification, or 'False Positive' for incorrect detections. Create project-level exclusions for generated code, test fixtures, or vendor libraries. Review bulk-marked issues periodically to ensure the team isn't gaming the system.
Code Example
# Configure Quality Gate via API
# Create a custom Quality Gate
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create?name=Team-Standard"
# Add conditions for NEW code only
# New bugs = 0
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create_condition" \
-d "gateName=Team-Standard&metric=new_bugs&op=GT&error=0"
# New vulnerabilities = 0
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create_condition" \
-d "gateName=Team-Standard&metric=new_vulnerabilities&op=GT&error=0"
# New coverage >= 80%
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create_condition" \
-d "gateName=Team-Standard&metric=new_coverage&op=LT&error=80"
# New duplicated lines <= 3%
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create_condition" \
-d "gateName=Team-Standard&metric=new_duplicated_lines_density&op=GT&error=3"
# New code smells maintainability rating = A
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/create_condition" \
-d "gateName=Team-Standard&metric=new_maintainability_rating&op=GT&error=1"
# Set as default for all projects
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/qualitygates/set_as_default?name=Team-Standard"
# Configure New Code period (project setting)
curl -u admin:$TOKEN -X POST \
"https://sonar.company.com/api/new_code_periods/set" \
-d "type=PREVIOUS_VERSION&project=my-service"
# Alternative: reference branch for PR analysis
# sonar.newCode.referenceBranch=main
# GitHub Actions integration with Quality Gate check
# .github/workflows/sonar.yml
name: SonarQube
on: [push, pull_request]
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history needed for blame
- uses: sonarsource/sonarqube-scan-action@v2
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- uses: sonarsource/sonarqube-quality-gate-action@v1
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# sonar-project.properties
sonar.projectKey=order-service
sonar.sources=src/main
sonar.tests=src/test
sonar.exclusions=**/generated/**,**/vendor/**
sonar.coverage.exclusions=**/config/**,**/dto/**Interview Tip
Lead with 'Clean as You Code' - it's SonarQube's official recommendation and shows you understand pragmatic quality management. The graduated enforcement approach (advisory -> PR blocking -> pipeline blocking) demonstrates empathy for developer experience while achieving quality goals.