Running a SonarQube Scanner in a Jenkins 2 Pipeline

I want to complete the "SonarQube Scanner" step in my Jenkins 2.x pipe.

When I try to create a groovy sample in the pipeline syntax, I get only a groovy script in the following format:

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

Does anyone know what the correct Step syntax is? For instance. Post JUnit report looks like

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

I use the following versions:

  • Jenkins 2.11
  • SonarQube Scanner 2.6.1
  • SonarQube 2.4.1 Plugin
+4
source share
3 answers

I think I get it. You must first get your SonarQube Scanner.

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

Then you can call the sonar scanner through Shell:

sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=..."
+8
source
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=."

}
+3

Instead, you can simply provide the full path to the sonar. As shown in the snippet below.

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 special sonar properties, add them as the sonar-project.properties file, as shown here Sonar Project Properties

+1
source

All Articles