我知道您想在 Jenkins 工作中阅读 Sonar Quality Gate 吗?
分析完成后,Sonar Quality Gate 将生成一个“report-task.txt”文件。
根据您的项目规模,可能需要一段时间。
然后,使用Sonar rest api,你可以得到json状态并使用它。
这是我正在使用的复制/粘贴示例:
myEcho('DEBUG', "I want to wait for SONAR completion")
if (fileExists('target/sonar/report-task.txt')) {
//echo sh(returnStdout: true, script: 'cat target/sonar/report-task.txt')
def reportTak = readProperties(file: "target/sonar/report-task.txt")
def countLoop = 0
def countMax = 10
def sonarGateIsDone = false
// wait at least 1 second before asking for Sonar feedback
sleep(time: 3000, unit: 'MILLISECONDS')
while (!sonarGateIsDone && (countLoop <= countMax)) {
countLoop++
// loop while status is over OR timeout...
echo sh(returnStdout: true, script: "curl ${SONAR_URL}api/ce/task?id=${reportTak.ceTaskId} -o target/sonar/output.json")
if (fileExists('target/sonar/output.json')) {
def outputJson = readJSON(file: 'target/sonar/output.json')
if ("${outputJson.task.status}" == 'SUCCESS' || "${outputJson.task.status}" == 'CANCELED' || "${outputJson.task.status}" == 'FAILED') {
myEcho('INFO', "Sonar Gate internal analysis finished [${outputJson.task.status}]")
if ("${outputJson.task.status}" == 'SUCCESS') {
// internal process done, lets check Gate status
echo sh(returnStdout: true, script: "curl ${SONAR_URL}api/qualitygates/project_status?analysisId=${outputJson.task.analysisId} -o target/sonar/sonarGate.json")
if (fileExists('target/sonar/sonarGate.json')) {
def sonarGateJson = readJSON(file: 'target/sonar/sonarGate.json')
if ("${sonarGateJson.projectStatus.status}" == 'OK') {
// Gate is OK
myEcho('INFO', "Sonar gate is OK : status=[${sonarGateJson.projectStatus.status}]")
sonarGateIsDone = true
} else {
// Gate is NOK
myEcho('WARN', "Sonar gate is NOK : status=[${sonarGateJson.projectStatus.status}]")
sonarGateIsDone = true
}
} else {
// cannot find SonarGate.json ?!
myEcho('FAIL', 'Sonar gate check failed : cannot find [target/sonar/sonarGate.json]')
}
} else {
myEcho('WARN', "Sonar gate check is [${outputJson.task.status}]...")
}
} else {
// Sonar internal analysis isnt over, keep on going
myEcho('INFO', "Sonar Gate internal analysis still ongoing, wait a little... [${outputJson.task.status}]")
}
} else {
myEcho('FAIL', 'Sonar gate check failed : cannot find [target/sonar/output.json]')
}
// reaching here might probably be for Sonar to get time to make it...
if (!sonarGateIsDone) {
sleep(time: 1000, unit: 'MILLISECONDS')
}
} // while loop
if (!sonarGateIsDone) {
myEcho('WARN', "Waiting too long for completion... gave up !")
}
} else {
myEcho('INFO', "target/sonar/report-task.txt DOES NOT EXISTS")
}
希望这会有所帮助。
问候