Search code examples
jenkinssonarqubejenkins-pipeline

How to obtain the SonarQube taskId / report URL in Jenkins Pipeline?


I have intergrated Sonarqube in Jenkins Pipeline, it working as below define:

        stage('static check') {
           steps {
               echo "starting codeAnalyze with SonarQube......"
               //sonar:sonar.QualityGate should pass
               withSonarQubeEnv('sonar') {
                 sh "mvn -f pom.xml clean compile sonar:sonar -U"
               }
               script {
               timeout(10) { 
                   def qg = waitForQualityGate() 
                       if (qg.status != 'OK') {
                         echo "failure: ${qg.status}"
                         
                         # Plan to send notify to Slack, But can NOT to obtain the Sonar report URL or TaskId
                         
                       }
                   }
               }
           }
       }

Plan to send corresponding notify to Slack, But can NOT to obtain the Sonar report URL or TaskId

And found some guys suggest that use BUILD_LOG_REGEX: SonarQube result URL: ${BUILD_LOG_REGEX, regex=".*ANALYSIS SUCCESSFUL, you can browse (.*)", showTruncatedLines=false, substText="$1"}, but this only working in Email Extension Plugin.

Another suggest is compose the report URL by this: ProjectURL = host + "/dashboard?id=" + ProjectID, and we not easy to get the ProjectID, since the projectID is compose from Pom.xml as: groupId:artifactId.

Maybe there is some easy way to get the Sonar TaskId or Report URL, Especially whether there is the waitForQualityGate() or other method will return the Sonar TaskId or Report URL ?


Solution

  • You can see the report by reading the file ./target/sonar/report-task.txt

    Using def getURL = readProperties file: './target/sonar/report-task.txt'

    and extract by calling something like this ${getURL['dashboardUrl']} which is will give url of report. also that above file have taskId.

    so it should like this

    def qg = waitForQualityGate() 
     if (qg.status != 'OK') {
     echo "failure: ${qg.status}"
     def getURL = readProperties file: './target/sonar/report-task.txt
    
     Add your slackmsg here use in that ${getURL['dashboardUrl']}
    
     }