问题
Im new to jenkins pipeline scripting and sonarqube. it would be great if I can get some help with the question below. I want to fail the Jenkins declarative pipeline job when quality gate check fails. As per sonar documentation (https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/#header-6), I tried with below two scenrions but both are seems not working and failing with errors. sonarqube analysis is working fine but it failing at QualityGate check. I created webhook in sonarserver which is returning json output. Not sure what Im missing here. Version I using Sonrscanner version - 3.0.0.702
scenario 1:
Getting error “Invalid parameter “abortPipeline”, did you mean “null”?” when run below code. I saprated with
stage('Sonarqube Analysis') {
environment {
scannerHome = tool 'ALM Sonar'
}
steps {
withSonarQubeEnv('ALM Prod Sonar') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'HOURS') {
waitForQualityGate abortPipeline: true }
}
}
scenario 2:
Getting error “Invalid JSON String”. Below analysis, its going till the “test2” further its failing to read status waitForQualityGate(). Please advise. I put the script quality gate in saparate stage still its failing with same error.
stage('Sonarqube Analysis') {
environment {
scannerHome = tool 'ALM Sonar'
}
steps {
withSonarQubeEnv('ALM Prod Sonar') {
sh "${scannerHome}/bin/sonar-scanner"
}
sleep time: 30000, unit: 'MILLISECONDS'
echo "test1"
script {
echo "test2"
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
echo "test3" }
}
}
}
回答1:
You can try below code as a work around.
stage('Sonarqube Analysis') {
environment {
scannerHome = tool 'Sonar scanner'
}
steps {
withSonarQubeEnv('Sonarserver') {
sh "${scannerHome}/bin/sonar-scanner"
}
sleep time: 30000, unit: 'MILLISECONDS'
script {
sh "curl -u username:password -X GET -H 'Accept: application/json' https://alm.accenture.com/sonar/api/qualitygates/project_status?projectKey=adop:SDSPDVCR:baggagetracking > status.json"
def json = readJSON file:'status.json'
echo "${json.projectStatus.status}"
if ("${json.projectStatus.status}" == "ERROR") {
currentBuild.result = 'FAILURE'
error('Pipeline aborted due to quality gate failure.')
}
来源:https://stackoverflow.com/questions/61105737/sonarqube-quality-gate-status-check-fail-in-jenkins-pipeline