简体   繁体   中英

SonarQube does not return status of waitForQualityGate() to jenkins?

I have used sonarQube in jenkins pipeline. I have installed all plugin related to sonarqube in jenkins. In the jenkins configure system, I configured the sonarqube server properly and jenkins global tool configuration I configured SonarQube Scanner properly.

This is jenkins pipeline code..

node{
stage('git checkout process'){
  echo 'started checkout'
  git 'https://github.com/ramkumar/sampleproject'
  echo 'completed sucessfully'
}

stage('compile package'){
  def mvnTool = tool name: 'Maven', type: 'maven'
  sh "${mvnTool}/bin/mvn clean install" 
}

  stage('SonarQube analysis') {
    withSonarQubeEnv('sonarqube') {
      mvnHome = '/opt/apache-maven/bin'
      sh "${mvnHome}/mvn sonar:sonar"

    }
  }

  stage("Quality Gate"){
          timeout(time: 1, unit: 'HOURS') {
              def qg = waitForQualityGate()
              if (qg.status != 'OK') {
                  emailext body: 'Your code was failed due to sonarqube quality gate', subject: 'Jenkins Failed Report', to: 'prakashpp666666@gmail.com'
                  error "Pipeline aborted due to quality gate failure: ${qg.status}"

              }
          }
      }

I also configured the webhooks in sonarqube. But when I build the job the 3 stage waitForQualityGate() is not returning ok status back to jenkins rather it shows Checking status of SonarQube task 'AWrQj5In7abK9JVZ9' on server 'sonarqube' SonarQube task 'AWrQj5In7abK9JVZ9' status is 'IN_PROGRESS'

and it is continously loading it is not getting completed. When I check in sonarqube server it shows Response: Server Unreachable . I am not running sonarqube on local it is running on docker . What may be problem?

Configure SonarQube webhook for quality gate

Administration > Configuration > Webhooks > Create

The URL should point to your Jenkins server http://{JENKINS_HOST}/sonarqube-webhook/

This is solved for me. as i was unaware of this hook. Once i configured this, everything went well.

Try to put sleep(60) command before the check:

sleep(60)
timeout(time: 1, unit: 'MINUTES') {
    def qg = waitForQualityGate()
    print "Finished waiting"
    if (qg.status != 'OK') {
        error "Pipeline aborted due to quality gate failure: ${qg.status}"
    }
}  

It solved same problem for me.

As suggested in the official docs here and here , I was able to get the waitForQualityGate() working correctly by configuring a webhook to the Jenkins instance on SonarQube Server.

Add a webhook of the form your <your-jenkins-instance>/sonarqube-webhook/ in your SonarQube server configuration, pointing to your Jenkins instance. Note the trailing slash is important.

I ran into a similar issue. In my case it was because of an extra slash (/) in SonarQube server URL. My Jenkins -> Configure System -> SonarQube servers -> Server URL was configured as http://sonarip:9000/
Once I removed the trailing slash and changed it to http://sonarip:9000 , the waitForQualityGate started to work as expected.

I solved this by creating a webhook in SonarQube.

  1. In Your project / Project settings, select Webhooks

在此处输入图像描述

  1. set the url to https://YOUR_JENKINS_URL/sonarqube-webhook/

ps: If you erroneously get FAIL from SQ, verify that the url of your SonarQube server (in Jenkins settings) does not end in '/'

Inspired by a nice article on the subject , I was able to narrow down my particular problem — which had the same symptom described here — to SonarQube webhook security :

  1. Go to Manage JenkinsConfigure SystemSonarQube serversAdvanced...
  2. Set Webhoot Secret to SonarQube webhook secret

So, even if you're problem isn't exactly this one, I'd strongly suggest reviewing both Jenkins and SonarQube settings together as the root cause could be something as simple...

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