简体   繁体   中英

Execute SonarQube Scanner within Jenkins 2 Pipeline

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? Eg 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

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=..."
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=."

}

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

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