关于sonarqube:在Jenkins声明式管道中使用waitForQualityGate

Using waitForQualityGate in a Jenkins declarative pipeline

Jenkins 2.50中声明性管道中的以下SonarQube(6.3)分析阶段因控制台日志中的以下错误而失败:http://pastebin.com/t2ja23vC。进一步来说:

SonarQube installation defined in this job (SonarGate) does not match any configured installation. Number of installations that can be configured: 1.

更新:在Jenkins设置中将" SonarQube"更改为" SonarGate"(在SonarQube服务器下,因此它将与Jenkinsfile匹配),我得到了另一个错误:http://pastebin.com/HZZ6fY6V

java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.

该阶段是SonarQube文档中示例的修改:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
stage ("SonarQube analysis") {
     steps {
        script {
           STAGE_NAME ="SonarQube analysis"

           if (BRANCH_NAME =="develop") {
              echo"In 'develop' branch, don't analyze."
           }
           else { // this is a PR build, run sonar analysis
              withSonarQubeEnv("SonarGate") {
                 sh"../../../sonar-scanner-2.9.0.670/bin/sonar-scanner"  
              }
           }
        }
     }
  }

  stage ("SonarQube Gatekeeper") {
     steps {
        script {
           STAGE_NAME ="SonarQube Gatekeeper"

           if (BRANCH_NAME =="develop") {
              echo"In 'develop' branch, skip."
           }
           else { // this is a PR build, fail on threshold spill
              def qualitygate = waitForQualityGate()
              if (qualitygate.status !="OK") {
                 error"Pipeline aborted due to quality gate coverage failure: ${qualitygate.status}"
              }
           }
        }
     }
  }

我还用URL http://****/sonarqube-webhook/创建了一个Webhook sonarqube-webhook。是那样还是http://****/sonarqube/sonarqube-webhook?要访问服务器仪表板,请使用http://****/sonarqube

在SonarQube的Quality Gates部分中,我创建了一个新的Quality Gate:

enter image description here

我不确定SonarGate中的设置是否正确。我确实使用jenkins-mocha生成了lcov.info文件,该文件在Sonar中用于生成coverage数据。

也许质量门设置是错误的设置?最终结果是,如果未达到覆盖率%,将无法在Jenkins工作。

enter image description here

最后,我不确定是否完全需要Jenkins系统配置中的以下配置:

enter image description here

(这是9000而不是900 ...在屏幕截图中剪切了文本)
enter image description here


SonarQube Jenkins插件会扫描构建输出中的两行,以获取SonarQube报告任务属性和项目URL。如果您对sonar-scanner的调用未输出这些行,则waitForQualityGate()调用将没有任务ID来查找它们。因此,您将必须找出正确的设置以使其更加冗长。

请参阅插件的SonarUtils类中的extractSonarProjectURLFromLogsextractReportTask方法以了解它们如何工作:

  • ANALYSIS SUCCESSFUL, you can browse 用于添加到徽章的链接(在构建历史记录中)
  • Working dir: 用于将任务ID传递到waitForQualityGate步骤


当使用Jenkins从站执行作业时,这被发现是Jenkins的SonarQube扫描仪中的错误(如果作业在主服务器上运行,则可以工作)。您可以在这里阅读更多信息:https://jira.sonarsource.com/browse/SONARJNKNS-282

我已经使用v2.61的扫描仪插件的测试版本进行了测试,发现它可以正常工作。
解决方案是在发行时升级到v2.61。

然后,此阶段将工作:

1
2
3
4
5
6
7
8
9
10
11
12
stage ("SonarQube analysis") {
   steps {
      withSonarQubeEnv('SonarQube') {
         sh"../../../sonar-scanner-2.9.0.670/bin/sonar-scanner"  
      }

      def qualitygate = waitForQualityGate()
      if (qualitygate.status !="OK") {
         error"Pipeline aborted due to quality gate coverage failure: ${qualitygate.status}"
      }
   }
}

如果您在Docker容器中运行SonarCube,请检查内存是否耗尽。我们正在竭尽全力。这似乎是问题所在。