12

I am using maven to build my java app, Jenkins for CI and Sonar for metrics.

Currently I have a build job that creates the sonar report. (Triggered via a post-build step in Jenkins.)

I would like to set this up to fail the build if certain thresholds are met - i.e. any major or blocker violations or Complexity more than 1.7.

Any guidance would be appreciated! - L

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
laura
  • 2,951
  • 9
  • 44
  • 61

2 Answers2

18

Install the build breaker plugin.

Fails your build if the code breaches any of the alert thresholds you specify on the project's Sonar quality profile.

Update

Sonarqube no longer recommends the use of this plugin:

Update (2019-02-11)

It appears the product has changed since I wrote this answer 7 years ago

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • 5
    Unfortunately the Build Breaker plugin is unavailable for Sonar 5.2+ – June Jan 14 '16 at 13:05
  • I was using it to fail pull requests with Github... what a shame – ianaz Oct 24 '16 at 12:55
  • Not being able to break a build, when static analysis fails in Sonar is IMO a deal-breaker on using Sonar. The only way to ensure that 'code smell's'/'bugs' are not addressed during check'ins is by breaking the build. Would we put up with not being able to break a build when unit tests fail? IMO it's the same thing. Not having this built-in to Sonar gradle plugin bewilders me. Yes, there is a problem with a synchronous build vs async Sonar processing, but this is an implementation problem. If Sonar are serious about what they do, this is a must have. – Ben Feb 11 '19 at 12:24
  • @Ben I don't disagree. When I wrote this answer 7 years ago the plugin worked perfectly. I would encourage you to take this up with SonarQube – Mark O'Connor Feb 11 '19 at 18:40
  • @Ben It appears breaking the build is supported in Jenkins pipelines. I've updated my answer – Mark O'Connor Feb 11 '19 at 18:43
  • 1
    Yes, sorry my first comment was aimed at SonarQube not yourself :-). Thanks, I saw that Jenkins support. Alas, we use TeamCity. Cheers – Ben Feb 12 '19 at 06:09
  • Sonar have updated their advice with new suggestions: https://blog.sonarsource.com/breaking-the-sonarqube-analysis-with-jenkins-pipelines/ – Ben Ketteridge Mar 12 '20 at 16:22
  • last link reports 404 for me – Ente Mar 27 '20 at 17:13
2

Use Jenkins Quality Gates Plugin, which fails the build if the predefined sonar quality gates are not green.

  1. Plugin ID quality-gates
  2. Latest Release 2.5
  3. Latest Release Date May 17, 2016
  4. Required Core 1.625.3

This plugin will stop a job when a quality gate measures is detected.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
johnnymnmonic
  • 774
  • 8
  • 11