How to Fail Jenkins Job from SonarQube Scanner if Quality Gate Fails?

Using:

  • Sonarqube Developer Edition 7.9.1
  • Jenkins 2.204.2
  • SonarQube Scanner for Jenkins 2.11
  • Sonarqube Scanner 4.2.0.1873

Goal:
We have a jenkins job which runs “Execute SonarQube Scanner”. We pass the git branch name via Analysis Property ‘sonar.branch.name’.
The Scanner and Sonarqube server both display the correct result of the scan as well as the Quality Gate status specific to the branch.
The Jenkins job also displays the Quality Gate status for the specified branch.
Is there a way for the Scanner to fail the build based on the Quality Gate status?

What I’ve tried:
I’ve tried using the Sonar Quality Gates 1.3.1 plugin but it’s over two years stale and didn’t account for the specified branch.

The Scanner plugin seems to know the correct quality gate status for the branch.
Is there a command line option or analysis property which could direct it to fail the build if the quality gate fails?

Welcome :slight_smile:

yes, there’s a solution for both ways

If still using classic Jenkins jobs (it’s 2020, so i hope not :wink: ) go with the
sonar-build-breaker plugin or implement your own polling for the quality gate status
via Sonarqube web api.

If using Jenkins pipelines use the waitForQualityGate() step / method.
Documentation https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/
There’s one gotcha, officially it’s not recommended / supported to use it within parallel step.
We’ve tried nevertheless to speed up our npm builds and it works most of the time with
intermittent errors, i.e. one of 12 analyses runs on timeout.
Every analysis creates it’s listener and waits for the json payload from Sonarqube webhook
and i guess there are threading problems.

The problem is known by Sonarsource and they are already on it, see
https://jira.sonarsource.com/browse/SONARJNKNS-316 with other tickets linked.

Gilbert

2 Likes

Gilbert,
Thanks for your help.
We are still using classic jenkins jobs but we are investigating pipelines.
The Sonarqube plugin Build Breaker does what we need.
We just need to cleanup our master branch of issues but the feature branches are passing quality gate and our development process is almost complete.
Thanks again for your help.
-John