如何应对Jenkins Pipeline中的SonarQube质量门

velaa5lx  于 2023-04-29  发布在  Jenkins
关注(0)|答案(5)|浏览(200)

在我的Jenkins Pipeline中,我需要对SonarQube质量门做出React。有没有更简单的方法来实现这一点,但在声纳扫描仪日志中寻找结果页面(e.例如https://mysonarserver/sonar/api/ce/task?id=xxxx)并从那里解析JSON结果?
我使用Jenkins 2。30和SonarQube 53
先谢谢你了

t2a7ltrp

t2a7ltrp1#

基于Vincent的回答,并使用Pipeline utility steps,下面是我的更新版本(使用sonarscanner报告文件):

withSonarQubeEnv('SONAR 6.4') {
                    sh "${scannerHome}/bin/sonar-scanner"
                    sh "cat .scannerwork/report-task.txt"
                    def props = readProperties  file: '.scannerwork/report-task.txt'
                    echo "properties=${props}"
                    def sonarServerUrl=props['serverUrl']
                    def ceTaskUrl= props['ceTaskUrl']
                    def ceTask
                    timeout(time: 1, unit: 'MINUTES') {
                        waitUntil {
                            def response = httpRequest ceTaskUrl
                            ceTask = readJSON text: response.content
                            echo ceTask.toString()
                            return "SUCCESS".equals(ceTask["task"]["status"])
                        }
                    }
                    def response2 = httpRequest url : sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"], authentication: 'jenkins_scanner'
                    def qualitygate =  readJSON text: response2.content
                    echo qualitygate.toString()
                    if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
                        error  "Quality Gate failure"
                    }
                }

请注意使用Jenkins凭据(身份验证:'jenkins_scanner')来检索正在验证的Sonar中的质量门。

o2gm4chl

o2gm4chl2#

使用SonarQube Scanner for Jenkins 2.8.1,解决方案开箱即用:

stage('SonarQube analysis') {
    withSonarQubeEnv('My SonarQube Server') {
        sh 'mvn clean package sonar:sonar'
    } // SonarQube taskId is automatically attached to the pipeline context
  }
}
stage("Quality Gate"){
    timeout(time: 1, unit: 'HOURS') { // 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}"
    }
  }
}
ddarikpa

ddarikpa3#

先扫描:

node("sonar") {
      deleteDir()
      unstash 'sources'
      def scannerHome = tool 'sonar-scanner'; 
      withSonarQubeEnv('sonarqube-rec') {
          withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {        
           // requires SonarQube Scanner for Maven 3.2+
           sh '''
             mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
             echo "SONAR_AUTH_TOKEN=$SONAR_AUTH_TOKEN" >> target/sonar/report-task.txt
           '''
           stash includes: "target/sonar/report-task.txt", name: 'sonar-report-task'
          }
      }
    }

然后检查质量门:

stage("Quality Gate"){
    node("sonar") {
        deleteDir()
        unstash 'sonar-report-task'
        def props = utils.getProperties("target/sonar/report-task.txt")
        echo "properties=${props}"
        def sonarServerUrl=props.getProperty('serverUrl')
        def ceTaskUrl= props.getProperty('ceTaskUrl')
        def ceTask
        def URL url = new URL(ceTaskUrl)
          timeout(time: 1, unit: 'MINUTES') {
            waitUntil {
              ceTask = utils.jsonParse(url)
              echo ceTask.toString()
              return "SUCCESS".equals(ceTask["task"]["status"])
            }
          }
          url = new URL(sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"] )
          def qualitygate =  utils.jsonParse(url)
          echo qualitygate.toString()
          if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
            error  "Quality Gate failure"
          }
   }
}
b91juud3

b91juud34#

我使用了“.sonar/report-task.txt”来检索ceTaskUrl -然后我使用Pipeline Shared Libraries并编写了自己的管道函数来检索质量门。
http://mySonarQube.com:9001/api/ce/task?id=“ceTaskUrl”
解析”任务。analysisId”
解析http://mySonarQube.com:9001/api/qualitygates/project_status?analysisId=中的质量门”任务。analysisId”

gdx19jrr

gdx19jrr5#

错误
响应代码:HTTP/1.1 403

相关问题