Jenkins pipline 中的 Sonar Qube Quality Gates 插件

Vla*_*ski 5 jenkins sonarqube

我已经安装并配置了 SonanarQube Quality Gates 插件,并在非管道作业中成功配置了它。但是当你尝试在 pipline 中配置它时,当我尝试像这样制作 pipline 时,我会遇到不同的错误:

stage ("SonarQube analysis") {
 dir('source') {
            def scannerHome = tool 'sonscanner';
             withSonarQubeEnv('sonar'){
               timeout(time: 1, unit: 'HOURS');
                 withSonarQubeEnv('sonar'){
                      def qg = waitForQualityGate()
             if (qg.status != 'OK') {
              error "Pipeline aborted due to quality gate failure: ${qg.status}"
      }
          } 
             } 
                } 
                  }         
Run Code Online (Sandbox Code Playgroud)

我看到这个:

java.lang.IllegalStateException: There is no body to invoke
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:95)
at org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.start(TimeoutStepExecution.java:46)
Run Code Online (Sandbox Code Playgroud)

当作为 SonarQube 分析之后的单独步骤时:

stage("Quality Gate"){
        timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
            def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
        if (qg.status != 'OK') {error "Pipeline aborted due to quality gate failure: ${qg.status}"

          } else {

                echo 'Quality Gate PASSED'

            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.
at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.processStepParameters(WaitForQualityGateStep.java:125)
at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.start(WaitForQualityGateStep.java:107)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:224)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:150)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
Run Code Online (Sandbox Code Playgroud)

当管道:

     stage ("SonarQube analysis") {

 dir('source') {

            def scannerHome = tool 'sonscanner';

             withSonarQubeEnv('sonar'){

            sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=android -Dsonar.sources='${WORKSPACE}'/source -Dsonar.language=java -Dsonar.projectBaseDir='${WORKSPACE}'/source -Dsonar.java.binaries='${WORKSPACE}'/source/app/build/intermediates/classes/debug -Dsonar.junit.reportsPath='${WORKSPACE}'/sourceapp/build/test-results/testDebugUnitTest"

  }    

     }              

       }



  stage("Quality Gate"){

        timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout

            def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv

            if (qg.status != 'OK') {error "Pipeline aborted due to quality gate failure: ${qg.status}"

            } else {

                echo 'Quality Gate PASSED'

            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

这:

Caused: java.lang.IllegalStateException: Unable to parse response from https://sonar//api/ce/task?id=AWCYPE3v1VuUrqmUzLyR:
Run Code Online (Sandbox Code Playgroud)

Ent*_*eco 0

无法解析来自 https://sonar//api/ce/task?id=AWCYPE3v1VuUrqmUzLyR 的响应:

请注意 url 中的双 //。您可以通过删除 Jenkins 上插件配置中的尾部斜杠来轻松解决此问题。

在詹金斯中:

  1. 管理詹金斯
  2. 配置系统
  3. 滚动到名为的部分SonarQube servers
  4. 删除;)\中的尾随Server URL

它应该看起来像这样:

https://sonarcloud.io

不是

https://sonarcloud.io/