简体   繁体   English

在Jenkins 2 Pipeline中执行SonarQube扫描仪

[英]Execute SonarQube Scanner within Jenkins 2 Pipeline

I want to execute a "SonarQube Scanner" Step within my Jenkins 2.x Pipeline. 我想在Jenkins 2.x管道中执行“SonarQube扫描仪”步骤。

When I try to create a sample groovy within the pipeline-syntax I only get a groovy script of the following format: 当我尝试在管道语法中创建一个示例groovy时,我只获得了以下格式的groovy脚本:

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

Does anyone know what is the correct Step Syntax? 有谁知道什么是正确的步骤语法? Eg Publish JUnit Report looks like 例如发布JUnit报告

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

I use the following Versions: 我使用以下版本:

  • Jenkins 2.11 詹金斯2.11
  • SonarQube Scanner 2.6.1 SonarQube扫描仪2.6.1
  • SonarQube Plugin 2.4.1 SonarQube插件2.4.1

I think I got it. 我想我明白了。 First you have to retrieve your SonarQube Scanner Tool 首先,您必须检索SonarQube扫描仪工具

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

Then you can call sonar-scanner via Shell: 然后你可以通过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 如果您有特定的声纳属性,请将它们添加为sonar-project.properties文件,如此处所示的声纳项目属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM