2016-11-09 93 views

回答

5

基於文森特的答案,以及使用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_scanner」)來檢索Sonar中的質量門是否被認可。

+1

Hi @tibo,你能告訴我你是如何使用「jenkins_scanner」對sonarqube進行身份驗證的? –

+1

我明白了,它來自http請求插件。 https://stackoverflow.com/questions/41571090/basic-auth-with-jenkins-http-request-plugin 非常感謝,這個答案幫助我實現質量門故障的通知。 –

+0

工程奇蹟。感謝你的分享。 –

1

我用「.sonar /報告-task.txt」檢索ceTaskUrl - 然後我用Pipeline Shared Libraries和寫我自己的流水線功能檢索質量門。

http://mySonarQube.com:9001/api/ce/task?id= 「ceTaskUrl」

解析 「task.analysisId」

解析質量門從http://mySonarQube.com:9001/api/qualitygates/project_status?analysisId= 「task.analysisId」

+0

你有沒有在Github的某個地方提供你的庫的源代碼?我是Groovy的新手,並希望從示例中學習。謝謝。 –

+0

我剛剛爲這個函數創建了一個Repo:https://github.com/chforster/JenkinsPipelineLibrary –

3

掃描第一:

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" 
      } 
    } 
} 
相關問題