【问题标题】:How to configure a Jenkins Pipeline for SonarQube scan如何为 SonarQube 扫描配置 Jenkins 流水线
【发布时间】:2017-05-21 16:16:36
【问题描述】:

我正在尝试为我的项目配置 jenkins 管道,但如果有人能指出我做错了什么,这里缺少一些东西:

下面是流水线脚本:

node {
    stage('SonarQube analysis') {
    // requires SonarQube Scanner 2.8+
    def scannerHome = tool 'sonarScanner';
    withSonarQubeEnv('SonarQube 6.2') {
      bat "${scannerHome}/bin/sonar-runner.bat"
    }
  } 
}

下面是Jenkins Sonar Plugin相关配置: 在Manage Jenkins > Configure System 下:

Manage Jenkins > Global Tool Configuration

以下是我得到的错误:

D:\jenkins\workspace\Test_Pipeline>D:\sonar-runner-2.4/bin/sonar-runner.bat
D:\sonar-runner-2.4
SonarQube Runner 2.4
Java 1.8.0_92 Oracle Corporation (64-bit)
Windows Server 2008 R2 6.1 amd64
INFO: Runner configuration file: D:\sonar-runner-2.4\conf\sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Work directory: D:\jenkins\workspace\Test_Pipeline\.\.sonar
INFO: SonarQube Server 6.2
19:48:53.627 INFO  - Load global repositories
19:48:53.920 INFO  - Load global repositories (done) | time=298ms
19:48:53.951 WARN  - Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN  - Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN  - Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 INFO  - User cache: C:\Users\Administrator\.sonar\cache
19:48:54.378 INFO  - Load plugins index
19:48:54.378 INFO  - Load plugins index (done) | time=0ms
19:48:55.235 INFO  - Process project properties
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 3.404s
Final Memory: 5M/120M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
ERROR: 
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

问题似乎是 Jenkins 不知道我的 sonar-project.properties 放在哪里。它位于 D:\myprj 下,内容如下:

# required metadata
sonar.projectKey=my_prj:my_prj
sonar.projectName=my_prj
sonar.projectVersion=1.00
sonar.sources=my_prj/src
sonar.language=java

请建议我如何让 Jenkins 管道了解 sonar-project.properties 文件的放置位置

【问题讨论】:

    标签: sonarqube jenkins-pipeline


    【解决方案1】:

    我需要将工作区添加到我的脚本中。这是下面的脚本:

    stage('SonarQube analysis') {
        ws('D:\\my_prj') {
        // requires SonarQube Scanner 2.8+
        def scannerHome = tool 'sonarScanner';
        withSonarQubeEnv('SonarQube 6.2') {
          bat "${scannerHome}/bin/sonar-scanner.bat"
        }
      }
    }
    

    【讨论】:

    • 我知道这是一篇旧帖子,但你能帮我知道如何从我的项目根目录设置我的 sonar-project.properties 文件(我正在使用 gitlab)吗?
    • 我想使用自动安装的内置声纳扫描仪。在这种情况下,不需要通过 sh 或 bat 命令指定 sonar-runner 的路径对吗?
    【解决方案2】:

    我看到您使用的是 Sonar-runner 2.4。我认为通过运行 2.8 版本,您将获得更好的结果。这是我的 JenkinsFile 配置,使用声明式管道语法。我遇到了同样的问题,但我通过重命名我的sonar-project.properties 文件解决了这个问题,以前的名称是“sonar.properties”。我希望这可以帮助其他人。

    stage('Sonarqube analysis') {
        steps {
            script {
                scannerHome = tool 'SonarScanner';
            }
            withSonarQubeEnv('SonarQube') {
                bat "${scannerHome}/bin/sonar-scanner.bat" 
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 2016-12-10
      • 2018-03-17
      • 2017-10-27
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2017-02-06
      相关资源
      最近更新 更多