在 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.

Update: 在 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

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/ 创建了一个网络钩子 sonarqube-webhook。应该是那样,还是http://****/sonarqube/sonarqube-webhook?要访问服务器仪表板,我使用 http://****/sonarqube.

在 SonarQube 的质量门部分,我创建了一个新的质量门:

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

也许质量门设置是错误的设置?如果未达到覆盖率百分比,最终结果是 Jenkins 中的作业失败。

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

(是 9000 不是 900...在屏幕截图中剪切文本)

SonarQube Jenkins 插件扫描两个特定行的构建输出,它用于获取 SonarQube 报告任务属性和项目 URL。如果您调用 sonar-scanner 没有输出这些行,则 waitForQualityGate() 调用将没有任务 ID 来查找它们。所以你必须想出正确的设置来让它更冗长。

查看插件 SonarUtils class 中的 extractSonarProjectURLFromLogsextractReportTask 方法以了解它们的工作原理:

  • ANALYSIS SUCCESSFUL, you can browse <project URL>用于给徽章添加一个link(在构建历史中)
  • Working dir: <dir with report-task.txt>用于将任务ID传递给waitForQualityGate步骤

这被发现是 Jenkins 的 SonarQube 扫描器中的一个错误,当使用 Jenkins slave 进行作业时(如果作业在 master 上 运行,它会工作)。您可以在这里阅读更多内容:https://jira.sonarsource.com/browse/SONARJNKNS-282

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

此阶段将起作用:

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,请检查内存是否耗尽。我们已经竭尽全力了。这似乎是问题所在。