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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With