简体   繁体   中英

How can I integrate SonarQube Quality Gate into my Jenkins pipeline?

I am executing in a pipeline a command to pass the sound of a project, what I need is that, just like in a normal job the sonar link remains once the job is executed, the same happens in the pipeline, now that when I run it in the pipeline, the SonarQube link is not saved, so I have the steps in 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}"
       }
   }
}

Try replacing with:

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

Note that the XXXXXXXX should be replaced with the name of a Sonar config you've entered in Jenkins under Manage Jenkins>Configure System>SonarQube servers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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