How can you make SonarQube version 5.2 fail a Jenkins Build?
There is no direct functionality in 5.2, either built-in or via plugins, to allow this, but it can be accomplished via web serivces, but
- You'll have to implement your own Jenkins plugin (or a complicated scripting step) to do it.
- In 5.2 the security implications are unattractive. They're better in 5.3
To roll your own
Take a look at the end of your analysis log. You'll see it includes a line like
[INFO] More about the report processing at http://your.sonarqube.server/api/ce/task?id=[guid]
Check the "sonar" directory created during analysis for a report-task.txt file to pick up that guid; it's the ceTaskId value.
In 5.2 If you have global admin perms you can click-through on that link to get the current processing status of the analysis report. In 5.3 you only need execute analysis perms. A "done" report looks like this:
{"task":{"id":"AVExRaJddM_jFJ3_Fp09","type":"REPORT","componentId":"c81cfb77-7bb8-4ea6-ac84-dfee43b43b99","componentKey":"org.apache.asyncweb:asyncweb-parent","componentName":"Apache Asyncweb Parent","componentQualifier":"TRK","status":"SUCCESS","submittedAt":"2015-11-22T23:17:05+0100","submitterLogin":"XXXX","startedAt":"2015-11-22T23:17:07+0100","executedAt":"2015-11-22T23:17:15+0100","executionTimeMs":7677,"logs":true}}
Once you get to status SUCCESS, you could then use web services to query the project's quality gate status.
So it's doable, but in 5.2 only if you want to configure a global-admin-level user's credentials to do it with. In 5.3 it gets better.
Edit for 6.2
6.2 adds webhooks. You can configure up to 10 global and up to 10 project-level URLs to be POSTed to after analysis report processing is complete. The post body is a JSON payload that includes project identifiers, and quality gate status.
You can use Jenkins' Quality Gates plugin for it.
It will provide you 'Quality Gates' as a post-build option. You should then just fill in your project key from SonarQube. Remember, it will fail your Jenkins job both in cases of warning or failure on your quality gate.
An even quicker workaround, with only the native shell tools (jsawk must be installed extra)
#!/bin/bash
CURL='/usr/bin/curl'
RVMHTTP="http://sonar:9000/sonar/api/qualitygates/project_status?projectKey=PROJECTKEY"
CURLARGS="-u mysonaruser:mysonarpass"
# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"
if [[ $raw = *"\"status\":\"ERROR\""* ]]; then
exit 1
else
exit 0
fi
The PROJECTKEY your can find out via
curl -u mysonaruser:mysonarpass http://sonar:9000/sonar/api/projects/index"
A quick workaround, add a post step Execute shell script :
if [ "\`curl -sL -w %{http_code} http://sonar_host/api/qualitygates/project_status?projectKey=project_key -o /dev/null -S --quiet 2>&1 | jsawk -a 'return this.status'\`" == "ERROR" ];
then
exit 1;
fi;