首页 > 解决方案 > 如何将每个 Jenkins 构建链接到它自己的 SonarQube 分析版本?

问题描述

我正在运行 SonarQube - Jenkins 集成。

我需要实现以下目标:

IE

构建 A - 通过质量门
构建 B - 未通过质量门

如果我单击与构建 A关联的 SonarQube 链接- 它会指向显示失败的 SonarQube 仪表板。

如果我单击与构建 B关联的 SonarQube 链接- 它会指向显示成功的 SonarQube 仪表板。

我尝试了以下方法:

sonar.projectVersion = ${env.BUILD_NUMBER}

这只是告诉与哪个分析版本比较最新分析。

如何在 Jenkins 脚本管道中实现与 SonarQube 仪表板和特定内部版本号的直接链接?

标签: jenkinssonarqubesonarqube-scansonar-runnersonarlint

解决方案


我知道您想在 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")
                }

希望这可以帮助。

问候


推荐阅读