简体   繁体   English

在 jenkins 管道节点内时,Sonarqube 质量门卡在待处理状态

[英]Sonarqube quality gate stuck on pending when inside jenkins pipeline node

We're trying to add a Sonarqube scan as part of our Jenkins pipeline script.我们正在尝试添加 Sonarqube 扫描作为 Jenkins 管道脚本的一部分。 We have a multi-module maven project, and we're using the Maven Sonarqube plugin to run the aggregated scan.我们有一个多模块 maven 项目,我们使用 Maven Sonarqube 插件来运行聚合扫描。 To initiate the scan we followed the instructions in Sonarqube's documentation as shown here (scroll down to the end of the page).要启动扫描,我们遵循Sonarqube的文档中的说明,如图这里(向下滚动到页面的结束)。

So the pipeline script looks something like this :所以管道脚本看起来像这样:

node {
    stage('SonarQube analysis') {
        withSonarQubeEnv('My SonarQube Server') {
            sh 'mvn clean package sonar:sonar'
        }
    }
}
stage("Quality Gate") {
    timeout(time: 1, unit: 'HOURS') { 
        def qg = waitForQualityGate() 
        if (qg.status != 'OK') {
            error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
}

and it works as expected.它按预期工作。 But since we're using it in more than one file, we'd like to have as less duplicate code as possible, so we want to have it inside the node like this :但是由于我们在多个文件中使用它,我们希望尽可能减少重复代码,因此我们希望将它放在节点中,如下所示:

node {
    stage('SonarQube analysis') {
        withSonarQubeEnv('My SonarQube Server') {
            sh 'mvn clean package sonar:sonar'
        }
    }
    stage("Quality Gate") {
        timeout(time: 1, unit: 'HOURS') { 
            def qg = waitForQualityGate() 
            if (qg.status != 'OK') {
                error "Pipeline aborted due to quality gate failure: ${qg.status}"
            }
        }
    }
}

But for some reason, this gets the quality gate stuck on PENDING status until the timeout is reached.但出于某种原因,这会使质量门卡在 PENDING 状态,直到达到超时。 I'm trying to understand why this is happening, and what could be done to avoid moving the quality gate check outside the node.我试图了解为什么会发生这种情况,以及可以做些什么来避免将质量门检查移到节点之外。

Any help would be greatly appreciated!任何帮助将不胜感激!

waitForQualityGate performs an HTTP call to the SonarQube server. waitForQualityGate对 SonarQube 服务器执行HTTP 调用

Make sure your build node has HTTP access to your SonarQube instance (the Jenkins master having access to it does not imply build nodes also have it).确保您的构建节点可以通过 HTTP 访问您的 SonarQube 实例(Jenkins master 可以访问它并不意味着构建节点也拥有它)。

Regardless, as I said in a comment, using a waiting step inside a node is not a good idea in general.无论如何,正如我在评论中所说,在node内使用等待步骤通常不是一个好主意。

I was also facing the same issue and I fixed it by adding sleep before waitForQualityGate stage我也面临着同样的问题,我通过在waitForQualityGate阶段之前添加睡眠来修复它

sleep 10
stage("Quality Gate") {
        timeout(time: 1, unit: 'HOURS') { 
            def qg = waitForQualityGate() 
            if (qg.status != 'OK') {
                error "Pipeline aborted due to quality gate failure: ${qg.status}"
            }
        }
    }

This could be a better way to retry.这可能是一种更好的重试方式。

def retryForTimeoutExceeded(count = 3, Closure closure) {
    for (int i = 1; i <= count; i++) {
        try {
            closure()
            break
        } catch (FlowInterruptedException error) {
            int retriesLeft = count - i
            def hasTimeoutExceeded = error.causes[0].getClass().toString() == 'class org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution$ExceededTimeout'
            println "Timeout Exceeded for clousre.\nRetries left: $retriesLeft"
            if (retriesLeft == 0 || !hasTimeoutExceeded) {
                throw error
            }
        }
    }
}

stage("Quality Gate") {
        retryForTimeoutExceeded {
            timeout(time: 5, unit: 'MINUTES') {
                // Just in case something goes wrong, pipeline will be killed after a timeout
                def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to sonar quality gate failure: ${qg.status}"
                }
            }
        }
    }

Timeout can be configured accordingly可以相应地配置超时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM