How to Gate on New Code vs Overall Quality in SonarQube
The Sonar way gate applies conditions to new code only, so a red gate reflects the diff rather than the whole codebase.
Sonar quality gates evaluate conditions on the New Code period by default. Set the new-code definition (previous version, days, or reference branch) so PRs are judged on what they changed.
Steps
- In the project, set New Code definition to reference branch
main(or a number of days). - Keep gate conditions scoped to "On New Code" (coverage, duplication, issues).
- Run the scan on PRs so Sonar computes the new-code diff.
sonar-project.properties
sonar-project.properties
sonar.projectKey=my-org_my-repo
sonar.newCode.referenceBranch=main
sonar.sources=src
sonar.qualitygate.wait=trueGotchas
sonar.qualitygate.wait=truemakes the scanner itself block until the gate is computed, replacing a separate wait step.- Overall-code conditions turn every PR red on legacy debt; reserve them for a periodic full-scan gate.
Related guides
How to Run a SonarCloud Scan and Wait for the Quality Gate in CIRun a SonarCloud analysis in GitHub Actions with the official scan action and block the job on the quality ga…
How to Ratchet Quality From a Baseline in CIPrevent quality regressions on a legacy codebase by baselining current issues and failing CI only when new vi…