Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Jenkins Build unstable when Sonar limits exceeded

We're running Sonar from Jenkins and would like to mark the build as unstable when Sonar limits are exceeded. We've got the appropriate limits set as Alerts in the quality profile.

We thought we could use Build Breaker to mark Sonar as failed (which puts that fact into the Jenkins log) and then use a Jenkins Post-build Groovy script to unstable the build in that case.

Unfortunately, the Jenkins Sonar plugin marks the build failed (and stops the build process) if Sonar fails and the Jenkins folks have indicated that's as designed and have set the relevant defect to 'will not fix'.

I've also tried setting Sonar's logging to Verbose hoping that the fact that the limits that were exceeded would be in the log (so we could again use a post build groovy task), but that doesn't seem to be the case either.

Any insight? At this point, it appears to me that the best thing would be to create a variant of Build Breaker that simply reports that alerts but does not break the build, but I'd prefer not to go the custom plugin route if it can be avoided.

like image 419
crovers Avatar asked Sep 20 '12 13:09

crovers


2 Answers

Ok, we've solved this to our satisfaction, though it did require a custom Sonar plugin.

We created a version of BuildBreaker (which we called BuildWarner). The only difference (other than plugin name, package name, class name, etc) is line 44 of AlertThresholdChecker.java is changed from :

  fail("Alert thresholds have been hit (" + count + " times).");

to :

  logger.info("SONARTHRESHOLDSEXCEEDED - Alert thresholds have been hit (" + count + " times).");

Once this is running in Sonar, the Jenkins console will include the phrase SONARTHRESHOLDSEXCEEDED if any alert hits reaches the Error Threshold level.

Then, install the Jenkins Groovy Postbuild plugin. We use the following Groovy script :

if(manager.logContains(".*SONARTHRESHOLDSEXCEEDED.*")) {
     manager.addWarningBadge("Sonar Thresholds Exceeded")
     manager.createSummary("warning.gif").appendText("<h1>Sonar Thresholds Exceeded</h1>", false, false, false, "red")
     manager.buildUnstable()
}

You can also use the Jenkins Text Finder plugin if you prefer.

Important to note is that the Sonar plugin must be BEFORE the Groovy Post Build or Text Finder plugin.

Hope this helps other people.

like image 163
crovers Avatar answered Sep 30 '22 03:09

crovers


We also created a Sonar plugin based on the Build Breaker plugin that logs to the console either ERROR_THRESHOLD_EXCEEDED or WARNING_THRESHOLD_EXCEEDED if error or warning level alerts are present. Our version of the build warner plugin can be found at https://github.com/NitorCreations/sonar-build-warner-plugin

Furthermore, we used the Jenkins console log plugin to mark the build as failed or unstable. The instructions are available at the github site.

like image 30
RJo Avatar answered Sep 30 '22 02:09

RJo