Execute SonarQube Scanner within Jenkins 2 Pipeline

南笙酒味 提交于 2019-12-18 04:21:47

问题


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

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



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

}



回答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



来源:https://stackoverflow.com/questions/38124171/execute-sonarqube-scanner-within-jenkins-2-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!