Skip to content
Latchkey

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=true

Gotchas

  • sonar.qualitygate.wait=true makes 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

Run this faster and cheaper on Latchkey managed runners. Start free →