Using waitForQualityGate in a Jenkins declarative pipeline

Idan Adar picture Idan Adar · Mar 20, 2017 · Viewed 25k times · Source

The following SonarQube (6.3) analysis stage in a declarative pipeline in Jenkins 2.50 is failing with this error in the console log: http://pastebin.com/t2ja23vC. More specifically:

SonarQube installation defined in this job (SonarGate) does not match any configured installation. Number of installations that can be configured: 1.

Update: after changing "SonarQube" to "SonarGate" in the Jenkins settings (under SonarQube servers, so it'll match the Jenkinsfile), I get a different error: http://pastebin.com/HZZ6fY6V

java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.


The stage is a modification of the example from the SonarQube docs: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

stage ("SonarQube analysis") {
     steps {
        script {
           STAGE_NAME = "SonarQube analysis"

           if (BRANCH_NAME == "develop") {
              echo "In 'develop' branch, don't analyze."
           }
           else { // this is a PR build, run sonar analysis
              withSonarQubeEnv("SonarGate") {
                 sh "../../../sonar-scanner-2.9.0.670/bin/sonar-scanner"   
              }
           }
        }
     }
  }

  stage ("SonarQube Gatekeeper") {
     steps {
        script {
           STAGE_NAME = "SonarQube Gatekeeper"

           if (BRANCH_NAME == "develop") {
              echo "In 'develop' branch, skip."
           }
           else { // this is a PR build, fail on threshold spill
              def qualitygate = waitForQualityGate()
              if (qualitygate.status != "OK") {
                 error "Pipeline aborted due to quality gate coverage failure: ${qualitygate.status}"
              } 
           }
        }
     }
  }     

I also created a webhook, sonarqube-webhook, with the URL http://****/sonarqube-webhook/. Should it be like that, or http://****/sonarqube/sonarqube-webhook? To access the server dashboard I use http://****/sonarqube.

In SonarQube's Quality Gates section I created a new quality gate:

enter image description here

I am not sure if the setting in SonarGate is correct. I do use jenkins-mocha to generate an lcov.info file that is used in Sonar to generate the coverage data.

Perhaps the quality gate setting is the wrong setting to do? The end result is to fail the job in Jenkins if coverage % is not met.

enter image description here

Finally, I am not sure if the following configurations in the Jenkins system configuration are at all required:

enter image description here

And

(It's 9000 not 900... cut text in the screen shot) enter image description here

Answer

Jan Fabry picture Jan Fabry · Mar 21, 2017

The SonarQube Jenkins plugin scans the build output for two specific lines, which it uses to get the SonarQube report task properties and project URL. If your invocation of sonar-scanner does not output these lines, the waitForQualityGate() call won't have the task ID to look them up. So you will have to figure out the correct settings to make it more verbose.

See the extractSonarProjectURLFromLogs and extractReportTask methods in the SonarUtils class of the plugin to understand how they work:

  • ANALYSIS SUCCESSFUL, you can browse <project URL> is used to add a link to the badge (in the build history)
  • Working dir: <dir with report-task.txt> is used to pass the task ID to the waitForQualityGate step