我正在运行SonarQube-Jenkins集成。
我需要实现以下目标:
即
Build A-通过质量关卡
版本B-质量门未通过
如果我单击与 build A 相关联的SonarQube链接-它会指向显示失败的SonarQube仪表板。
如果我单击与 build B 相关联的SonarQube链接-它会定向到显示成功的SonarQube仪表板。
我尝试了以下操作:
sonar.projectVersion = ${env.BUILD_NUMBER}
这只是告诉您要与哪个分析版本比较最新的分析。
如何在Jenkins脚本管道中实现与SonarQube仪表板以及特定内部版本号的直接链接?
答案 0 :(得分:0)
我知道您想在詹金斯(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")
}
希望这会有所帮助。
致谢