Sonarqube质量门未向Jenkins发送webhook

chy5wohz  于 2022-12-29  发布在  Jenkins
关注(0)|答案(8)|浏览(272)

我将Jenkins配置为与sonarqube扫描仪一起工作。扫描工作正常。Jenkins管道工作正常,Jenkins日志中没有任何问题。
SonarQube扫描仪3.0.3.778Jenkins:2.70 SonarQube扫描仪Jenkins插件:2.6.1
我使用这个代码:

stage('SonarQube analysis') {
        sh 'sed -ie "s|_PROJECT_|${PROJECT_CODE}|g" $WORKSPACE/_pipeline/sonar-project.properties'
        // requires SonarQube Scanner 3.0+
        def scannerHome = '/opt/sonar/bin/sonar-scanner';
        withSonarQubeEnv('mscodeanalysis') {
            sh "${scannerHome}/bin/sonar-scanner -Dproject.settings=$WORKSPACE/_pipeline/sonar-project.properties"
        }
    }
    }
    }
}
    }
    // No need to occupy a node
    stage("Quality Gate"){
        timeout(time: 15, 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 quality gate failure: ${qg.status}"
            }
        }
    }

我的问题来自质量门。它从来没有POST的json有效负载到jenkins。我没有看到一个json条目内的jenkins日志。但我知道jenkins和sonarqube服务器之间的连接是工作,因为我能够发送一个POST使用curl从sonarqube虚拟机。
下面是jenkins作业的输出:

Timeout set to expire in 15 min
[Pipeline] {
[Pipeline] waitForQualityGate
Checking status of SonarQube task 'AV3irVJXpvBxXXNJYZkd' on server 'mscodeanalysis'
SonarQube task 'AV3irVJXpvBxXXNJYZkd' status is 'PENDING'
Cancelling nested steps due to timeout

这是我没能到达Jenkins输油管的有效载荷网址:http://sonar-server:9000/api/ce/task?id=AV3irVJXpvBxXXNJYZkd

{"task":{"organization":"default-organization","id":"AV3irVJXpvBxXXNJYZkd","type":"REPORT","componentId":"AV3hrJeCfL_nrF2072FH","componentKey":"POOL-003","componentName":"POOL-003","componentQualifier":"TRK","analysisId":"AV3irVkZszLEB6PsCK9X","status":"SUCCESS","submittedAt":"2017-08-14T21:36:35+0000","submitterLogin":"jenkins","startedAt":"2017-08-14T21:36:37+0000","executedAt":"2017-08-14T21:36:38+0000","executionTimeMs":650,"logs":false,"hasScannerContext":true}}

我无法插入图像,但质量门为通过,分析任务成功。
如果我需要包括更多信息,请告诉我。谢谢

hiz5n14c

hiz5n14c1#

问题可能是Jenkins正在使用带有自签名证书的https。
1.为SonarQube生成信任存储库:

keytool -import -trustcacerts -alias jenkins-host-name -file cert.crt -keystore sonarqube.jks

密钥库密码:密码
其中cert.crt-是用于jenkins ssl的证书,jenkins-host-name-是docker网络中jenkins的主机名(用于webhook)
1.将信任存储添加到SonarQube Dockerfile:

FROM sonarqube
COPY sonarqube.jks /var/sonar_cert/
COPY sonar.properties /opt/sonarqube/conf/sonar.properties
  1. Update sonar.properties
sonar.ce.javaAdditionalOpts=-Djavax.net.ssl.trustStore=/var/sonar_cert/sonarqube.jks -Djavax.net.ssl.trustStorePassword=password

然后,如果你有一个正确的用户名和密码为Jenkins在webhook网址提供一切都应该工作。
已尝试:Jenkins2.107.2,声纳Qube 7.1

dgsult0t

dgsult0t2#

下面是我们解决此问题的一个简单示例:
SonarQube随机挂起在“挂起”状态。告诉它重试会刷新它。在本例中,我们将其设置为10秒

maxRetry = 200
forloop (i=0; i<maxRetry; i++){
    try {
        timeout(time: 10, unit: 'SECONDS') {
            waitForQualityGate()
        }
    } catch(Exception e) {
        if (i == maxRetry-1) {
            throw e
        }
    }
}
m0rkklqb

m0rkklqb3#

我很惊讶地发现@Katone Vi的回答非常有效。基于他们的回答,我们添加了一个成功时快速退出的功能,并使用DSL来处理原始请求:

stage('SonarQube') {
      steps {
        withSonarQubeEnv('SonarQube') {
          sh """
            ${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=XXX_${env.STAGE}_lambda
          """
        }

        script {
          Integer waitSeconds = 10
          Integer timeOutMinutes = 10
          Integer maxRetry = (timeOutMinutes * 60) / waitSeconds as Integer
          for (Integer i = 0; i < maxRetry; i++) {
            try {
              timeout(time: waitSeconds, unit: 'SECONDS') {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                  error "Sonar quality gate status: ${qg.status}"
                } else {
                  i = maxRetry
                }
              }
            } catch (Throwable e) {
              if (i == maxRetry - 1) {
                throw e
              }
            }
          }
        }
      }
    }
68de4m5k

68de4m5k4#

如果您已经配置SonarQube使用HTTP(S)代理,请确保您的jenkins可以通过代理访问或配置为“非代理主机”。这可以通过http.nonProxyHosts属性或HTTP_NONPROXYHOSTS环境变量来完成。有关详细信息和语法,请参阅文档。

nukf8bse

nukf8bse5#

如果您使用的是Jenkinsfile,解决方法如下:
定义凭据:

environment { 
  CRED = credentials('jenkins_user_pass') 
 }

则用途:

stage("Quality Gate") {
    steps {
         script {
                while(true){
                    sh "sleep 2"
                    def url="http://jenkinsURL/job/${env.JOB_NAME.replaceAll('/','/job/')}/lastBuild/consoleText";
                    def sonarId = sh script: "wget -qO- --content-on-error --no-proxy --auth-no-challenge --http-user=${CRED_USR} --http-password=${CRED_PSW} '${url}'  | grep 'More about the report processing' | head -n1 ",returnStdout:true
                    sonarId = sonarId.substring(sonarId.indexOf("=")+1)
                    echo "sonarId ${sonarId}"
                    def sonarUrl = "http://jenkinsURL/sonar/api/ce/task?id=${sonarId}"
                    def sonarStatus = sh script: "wget -qO- '${sonarUrl}' --no-proxy --content-on-error | jq -r '.task' | jq -r '.status' ",returnStdout:true
                    echo "Sonar status ... ${sonarStatus}"
                    if(sonarStatus.trim() == "SUCCESS"){
                        echo "BREAK";
                        break;
                    }
                    if(sonarStatus.trim() == "FAILED "){
                        echo "FAILED"
                        currentBuild.result = 'FAILED'
                        break;
                    }
                }
            }
        }
    }
gmxoilav

gmxoilav6#

我也遇到过类似的问题,虽然Sonar服务器中的质量门后端活动需要不到20秒的时间来完成其分析。但jenkins作业中的sonar-webhook的质量门失败/成功响应需要大量时间并卡住。

stage('Sonar:QG') {
            steps {
                **sleep(10)  /* Added 10 sec sleep that was suggested in few places*/**
                script{
                    timeout(time: 10, unit: 'MINUTES') {
                        def qg = waitForQualityGate abortPipeline: true
                        if (qg.status != 'OK') {
                            echo "Status: ${qg.status}"
                            error "Pipeline aborted due to quality gate failure: ${qg.status}"
                        }
                    }
                }
            }
        }

主要检查以下事项:-声纳中是否配置了Webhook:- SonarQube -〉管理-〉Webhook http://:/sonarqube-webhook/
或者使用localhost代替IP在我的情况下解决了这个问题。

efzxgjgh

efzxgjgh7#

我做了更简单的决定,但效果是一样的

stage("Quality gate") {
      steps {
        retry(3){
            waitForQualityGate abortPipeline: true
          }

      }
    }
g52tjvyc

g52tjvyc8#

在阶段(“SonarQube analysis”)和阶段(“Quality Gate”)之间添加sh“sleep 10”修复了该问题。

Checking status of SonarQube task 'AV3rHxhp3io6giaQF_OA' on server 'sonarserver'
SonarQube task 'AV3rHxhp3io6giaQF_OA' status is 'SUCCESS'
SonarQube task 'AV3rHxhp3io6giaQF_OA' completed. Quality gate is 'OK'

相关问题