【问题标题】:Work around global webhook for sonarQube Quality Gate解决 sonarQube Quality Gate 的全局 webhook
【发布时间】:2021-01-31 02:59:05
【问题描述】:

我有兴趣为我的项目添加质量门。在 Jenkins 构建后,我添加了一个脚本,该脚本在 SonarQube 服务器上创建一个项目并确定测试质量是否足够(可以找到代码来源here)。脚本如下

stage('SonarCloud') {
      steps {
        withSonarQubeEnv('SonarQube') {

            sh 'mvn clean package sonar:sonar '

        }
      }
    }
stage("Quality Gate") {
      steps {
        timeout(time: 15, unit: 'MINUTES') { // If analysis takes longer than indicated time, then build will be aborted
            waitForQualityGate abortPipeline: true
            script{
                def qg = waitForQualityGate() // Waiting for analysis to be completed
                if(qg.status != 'OK'){ // If quality gate was not met, then present error
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                }
            }
        }
      }
    }

问题是我的构建永远不会自行完成(它在超时后中止。删除超时使它看起来永远运行。话虽如此,分析被创建并放置在 SonarQube 服务器上)。这是我在超时之前看到的消息:

SonarQube 任务“AXUx0uqFm6myUuXQbknO”状态为“IN_PROGRESS”

由于超时而取消嵌套步骤

我的问题是我不是 SonarQube 服务器的管理员,所以我无法添加可以解决此问题的 webhook。我想知道是否有解决方法?

我尝试替换

sh 'mvn clean package sonar:sonar '

sh 'mvn clean package sonar:sonar -Dsonar.webhooks.project=https://my-jenkins-server.com/sonarqube-webhook/'

但没有到达任何地方。

我也尝试改编this code,在类似问题中讨论过,但也没有走远。

【问题讨论】:

    标签: sonarqube jenkins-pipeline multibranch-pipeline


    【解决方案1】:

    实际上有一种方法可以解决这个问题。解决此问题的正确方法是将 webhook 安装回 Jenkins,但您显然无法解决此问题。

    下一个“正确”修复是使用 SonarQube REST api,但首先我需要解释“waitForQualityGate()”中发生的情况。

    它做的第一件事是使用 REST api 端点“/api/ce/task”来查看后台任务是否完成。它从“report-task.txt”文件中获取要检查的任务 ID,该文件的格式类似于属性文件。如果第一次检查不是 SUCCESS 或 ERROR,则进入等待循环,等待调用 webhook。

    因此,您可以通过简单地添加比后台任务花费更多秒的“睡眠”时间,然后调用“waitForQualityGate()”来实现非常便宜的修复。

    一个更合适的修复会做这样的事情:

    def reportFilePath = "target/sonar/report-task.txt"
    def reportTaskFileExists = fileExists "${reportFilePath}"
    if (reportTaskFileExists) {
        echo "Found report task file"
        def taskProps = readProperties file: "${reportFilePath}"
        echo "taskId[${taskProps['ceTaskId']}]"
        while (true) {
            sleep 20
            def taskStatusResult    =
                sh(returnStdout: true,
                   script: "curl -s -X GET -u ${authString} \'${sonarProps['sonar.host.url']}/api/ce/task?id=${taskProps['ceTaskId']}\'")
                echo "taskStatusResult[${taskStatusResult}]"
            def taskStatus  = new JsonSlurper().parseText(taskStatusResult).task.status
            echo "taskStatus[${taskStatus}]"
            // Status can be SUCCESS, ERROR, PENDING, or IN_PROGRESS. The last two indicate it's
            // not done yet.
            if (taskStatus != "IN_PROGRESS" && taskStatus != "PENDING") {
                break;
            }
        }
    }
    

    【讨论】:

    • 什么是authString?它似乎是未定义的变量(如果是,我如何定义它?)
    • 另外,使用 '${taskProps['ceTaskUrl']}\' 代替 '${sonarProps['sonar.host.url']}/api/ce/task?id=${taskProps['ceTaskId']}\' 可以吗?看起来更简单
    • authString 是您的 sonarqube 实例所需的任何凭据。
    • ceTaskId 属性在您读取report-task.txt 文件时设置。将 ceTaskUrl 属性设置为完整的字符串并没有多大意义,这样您就可以引用较短的表达式一次。
    猜你喜欢
    • 2017-03-15
    • 2017-06-18
    • 2020-08-05
    • 2020-07-25
    • 2018-12-18
    • 1970-01-01
    • 2018-06-08
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多