【问题标题】:How to link each Jenkins build to its own SonarQube analysis version?如何将每个 Jenkins 构建链接到它自己的 SonarQube 分析版本?
【发布时间】:2019-11-28 15:52:45
【问题描述】:

我正在运行 SonarQube - Jenkins 集成。

我需要做到以下几点:

  • 将每个管道构建与相应的 SonarQube 分析相关联。

构建 A - 通过质量门
构建 B - 质量门失败

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

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

我尝试了以下方法:

sonar.projectVersion = ${env.BUILD_NUMBER}

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

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

【问题讨论】:

    标签: jenkins sonarqube sonarqube-scan sonar-runner sonarlint


    【解决方案1】:

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

    希望这会有所帮助。

    问候

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 2018-01-17
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2016-12-14
      • 2020-06-13
      相关资源
      最近更新 更多