如何将SonarQube Quality Gate集成到我的Jenkins管道中?

时间:2019-08-19 20:55:59

标签: jenkins groovy sonarqube jenkins-groovy

我正在管道中执行传递项目声音的命令,我需要的是,就像在正常工作中一样,声纳链接在工作执行后仍会保留,在管道中也是如此,现在当我在管道中运行它时,SonarQube链接未保存,因此我具有groovy的步骤:

stage ('QA'){
  steps { 
     echo 'executing sonar'
     bat  'mvn sonar:sonar -Dsonar.host.url='+env.SONAR_URL+' - 
     Dsonar.projectName=QA:%JOB_BASE_NAME% - 
     Dsonar.projectKey=QA:%JOB_BASE_NAME%'
  }
}

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}"
       }
   }
}

1 个答案:

答案 0 :(得分:1)

尝试替换为:

stage ('QA'){
    steps {
        echo 'Running SonarQube..'
        withSonarQubeEnv('XXXXXXXX') {
            bat  'mvn sonar:sonar -Dsonar.host.url='+env.SONAR_URL+' - 
            Dsonar.projectName=QA:%JOB_BASE_NAME% - 
            Dsonar.projectKey=QA:%JOB_BASE_NAME%'
            timeout(time: 1, unit: 'HOURS') {
                script {
                    def qg = waitForQualityGate()
                    if (qg.status != 'OK') {
                        error "Pipeline aborted due to a quality gate failure:   ${qg.status}"
                    }
                }
            }
        }
    }
}

请注意,XXXXXXXX应该替换为您在Jenkins中输入的Sonar配置名称,该配置位于Manage Jenkins>配置系统> SonarQube服务器下