Execute SonarQube Scanner within Jenkins 2 Pipeline

18,156

Solution 1

I think I got it. First you have to retrieve your SonarQube Scanner Tool

def sonarqubeScannerHome = tool name: 'SonarQubeScanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'

Then you can call sonar-scanner via Shell:

sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=..."

Solution 2

env.sonarHome= tool name: 'scanner-2.4', type: 'hudson.plugins.sonar.SonarRunnerInstallation'

withSonarQubeEnv('sonar.installation') { // from SonarQube servers > name
  sh "${sonarHome}/bin/sonar-runner -Dsonar.host.url=${SONAR_HOST_URL}  -Dsonar.login=${SONAR_AUTH_TOKEN}    -Dsonar.projectName=xxx -Dsonar.projectVersion=xxx -Dsonar.projectKey=xxx -Dsonar.sources=."

}

Solution 3

You can, instead, just provide the full path to ur sonar-runner. As shown in the below snippet.

def runSonarScan(sonar_url){
    withEnv(['SONAR_HOST=' + sonar_url]) {
        sh '''
        $/opt/sonarqube/sonar-runner-2.4/bin/sonar-runner -e -Dsonar.host.url=${SONAR_HOST}
        '''
    }
}

If you have specific sonar properties add them as a sonar-project.properties file as shown here Sonar Project Properties

Share:
18,156
Christoph Forster
Author by

Christoph Forster

Developer, Clean Code Enthusiast, Build-Manager Fun: Mountainbiking, Running, Reading

Updated on June 04, 2022

Comments

  • Christoph Forster
    Christoph Forster almost 2 years

    I want to execute a "SonarQube Scanner" Step within my Jenkins 2.x Pipeline.

    When I try to create a sample groovy within the pipeline-syntax I only get a groovy script of the following format:

    step <object of type hudson.plugins.sonar.SonarRunnerBuilder>
    

    Does anyone know what is the correct Step Syntax? E.g. Publish JUnit Report looks like

    step([$class: 'JUnitResultArchiver', testResults: ''])
    

    I use the following Versions:

    • Jenkins 2.11
    • SonarQube Scanner 2.6.1
    • SonarQube Plugin 2.4.1