I have created a Jenkins build pipeline and configured SOnar as I described in one of my earlier questions.
The Console Output for the build provides in it a URL that I am using for checking the results of Sonar Analysis. However, my requirement is that based on the number of defects that Sonar finds, it should fail the Jenkins build if a specific 'x' no. of defects are found. Pls suggest how this can be configured in the pipeline
You can try to do it directly from you pipeline script:
def scannerHome = tool 'SonarQube Scanner';
withSonarQubeEnv('SonarQube') {
sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=advant-web -Dsonar.sources=. -Dsonar.exclusions=node_modules/**,build/** -Dsonar.projectVersion=1.0.${BUILD_NUMBER}"
}
sleep 10
sh "curl -u user:password -X GET -H 'Accept: application/json' http://localhost:9000/api/qualitygates/project_status\\?projectKey\\=my-project > status.json"
def json = readJSON file:'status.json'
echo "${json.projectStatus.status}"
if ("${json.projectStatus.status}" == "ERROR") {
currentBuild.result = 'FAILURE'
error('SonarQube quality gate status of a project is invalid.')
}
or in case of upgrade SonarQube Scanner for Jenkins up to 2.61 you can write something like following:
...
timeout(time: 5, unit: 'MINUTES') {
def qualitygate = waitForQualityGate()
if (qualitygate.status != "OK") {
error "Pipeline aborted due to quality gate coverage failure."
}
}
You can read more here: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins
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