Enforce SonarQube Quality Gates in CI to Block Merges on New-Code Issues
Configure SonarScanner to wait for the Quality Gate and fail your pipeline when new-code issues violate the gate, then verify with a deliberate violation.
01 Aug 2025, 19:57 UTC

The problem: green builds with red code
SonarQube can flag new bugs, vulnerabilities, and uncovered code, but a scan alone does not stop a merge. If your CI pipeline runs the scanner and then continues regardless of the result, developers can merge code that fails the project's Quality Gate. The useful takeaway is to make the scanner wait for the gate decision and fail the build when the gate returns ERROR.
Prerequisites
- A SonarQube server (assumed version 9.9 LTS or 10.x; behavior differs across editions and versions)
- A project created in SonarQube and bound to a Quality Gate
- SonarScanner installed on your CI runner
- A CI pipeline with permission to execute analysis using a project token
Quality Gate is a set of conditions on metrics such as bugs, vulnerabilities, code smells, coverage, and duplication. New code means code added or changed since the project's reference branch, as defined by the server. Short-lived branch is a temporary branch, such as a pull request branch, analyzed before merging.
Procedure: fail the pipeline on Quality Gate errors
1. Bind the project to a gate
In SonarQube, open the project, go to Administration > Quality Gates, and assign the gate you want to enforce. Use the default gate only after reviewing its conditions; default thresholds vary by server version.
2. Configure the scanner to wait for the gate
Run the scanner on your CI runner with the wait property enabled so the command does not return until the server has evaluated the gate:
# Run on the CI runner. Replace placeholders with your values.
sonar-scanner \
-Dsonar.projectKey=my-app \
-Dsonar.projectName="My Application" \
-Dsonar.sources=. \
-Dsonar.host.url=https://sonarqube.example.com \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.qualitygate.wait=true
Required permissions: the CI user or token must have the Execute Analysis permission on the project. Risk: if the server is slow or unreachable, the scanner can block the pipeline until it times out.
3. Fail the build when the gate is ERROR
With sonar.qualitygate.wait=true, SonarScanner exits with a non-zero status when the gate status is ERROR. Configure your pipeline to treat that exit code as a failure. For example, in a shell-based pipeline:
# Run on the CI runner
if ! sonar-scanner ... -Dsonar.qualitygate.wait=true; then
echo "SonarQube Quality Gate failed. Blocking merge."
exit 1
fi
Alternatively, poll the server after scanning:
# Run on the CI runner. Replace SONAR_TOKEN and projectKey.
curl -s -u "$SONAR_TOKEN:" \
"https://sonarqube.example.com/api/qualitygates/project_status?projectKey=my-app"
Check the JSON status field. A value of ERROR means the gate failed and the pipeline should stop.
| Approach | Behavior | Best for |
|---|---|---|
sonar.qualitygate.wait=true | Scanner blocks until the server evaluates the gate; exits non-zero on ERROR | Simple pipelines that can wait for analysis latency |
Poll api/qualitygates/project_status | Pipeline reads gate status after scanning and fails independently | Pipelines that separate scanning from gating or need custom retry logic |
Expected checks
- Open the project dashboard in SonarQube after analysis and confirm the Quality Gate shows OK or ERROR.
- Confirm the CI job fails when the gate is ERROR and passes when it is OK.
- Compare the scanner and server versions in SonarQube under Administration > System > Update Center (or System Info) to catch mismatches that can suppress gate data.
Verification with a deliberate violation
Before relying on the gate, introduce a small new-code issue in a feature branch, such as an unused import or a hardcoded string that triggers a rule, then run the pipeline. The job should fail and the SonarQube dashboard should show the gate as ERROR for that branch. Remove the violation and confirm the gate returns to OK. Do not leave the violation in the main branch.
Limitations
- Community Edition has limited support for pull request decoration and short-lived branches; blocking merges based on PR analysis may require Developer Edition or above.
- Quality Gate conditions and metric definitions change between SonarQube versions; review the gate after upgrading the server.
- Scanner and server version mismatches can suppress Quality Gate data or cause analysis failures.
sonar.qualitygate.waitextends build time. Set a pipeline timeout appropriate for your server's analysis latency.
Recovery options
This procedure changes CI pipeline configuration, not production data. If the gate blocks legitimate merges, revert the pipeline change or remove sonar.qualitygate.wait=true temporarily, then adjust the Quality Gate conditions in SonarQube rather than disabling enforcement entirely.
Note: This draft requires current verification against your SonarQube version and edition before production use. Confirm scanner exit codes and API behavior in a non-production project first.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.