jenkins - 如何将每个 Jenkins 构建链接到它自己的 SonarQube 分析版本?
问题描述
我正在运行 SonarQube - Jenkins 集成。
我需要实现以下目标:
- 将每个管道构建与相应的 SonarQube 分析相关联。
IE
构建 A - 通过质量门
构建 B - 未通过质量门
如果我单击与构建 A关联的 SonarQube 链接- 它会指向显示失败的 SonarQube 仪表板。
如果我单击与构建 B关联的 SonarQube 链接- 它会指向显示成功的 SonarQube 仪表板。
我尝试了以下方法:
sonar.projectVersion = ${env.BUILD_NUMBER}
这只是告诉与哪个分析版本比较最新分析。
如何在 Jenkins 脚本管道中实现与 SonarQube 仪表板和特定内部版本号的直接链接?
解决方案
我知道您想在 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")
}
希望这可以帮助。
问候
推荐阅读
- powershell - 存储有错误的命令输出
- javascript - 未捕获的 ReferenceError:未定义 removeLine
- r - R. 从向量中引导最大值
- grafana - 带有prometheus的动态下拉值grafana
- performance - COBOL - 通过 para1-exit 执行 para1 VS 通过 para1 执行 para1
- regex - 正则表达式从标头中检索验证令牌
- git - 同步到远程仓库时 git push 的问题
- memory-leaks - ebay交易api内存使用情况
- docker - 如何将主机称为主机名而不是容器名称?
- c++ - 实体组件架构:想要拆分大实体 -> 难以重构