如何在 Sonarqube 中使用质量门触发 webhook 到 jenkins

How to use quality gates in Sonarqube to trigger the webhook to jenkins

我正在阅读 this official post and documentation,其中解释了如何使用 jenkins 的质量门。

我的目标是在通过质量检查时在 jenkins 中触发构建。

根据post,我只需要安装一个sonarqube并使用代码片段即可。

这是手动触发构建后的 jenkins 日志:

 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8cddf9af39adbada9366efd3d707d8056c27cd8d (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 8cddf9af39adbada9366efd3d707d8056c27cd8d
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master 8cddf9af39adbada9366efd3d707d8056c27cd8d
 > git rev-list 8cddf9af39adbada9366efd3d707d8056c27cd8d # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (build & SonarQube Scan)
[Pipeline] wrap
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: hudson.AbortException: SonarQube installation defined in this job (My SonarQube Server) does not match any configured installation. Number of installations that can be configured: 1.
If you want to reassign a lot of jobs to a different SonarQube installation see http://docs.sonarqube.org/display/PLUG/Reassign+Jobs+to+Another+SonarQube+Instance
    at hudson.plugins.sonar.SonarInstallation.checkValid(SonarInstallation.java:170)
    at hudson.plugins.sonar.SonarBuildWrapper.setUp(SonarBuildWrapper.java:81)
    at org.jenkinsci.plugins.workflow.steps.CoreWrapperStep$Execution.start(CoreWrapperStep.java:80)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:184)
Caused: hudson.remoting.ProxyException: org.codehaus.groovy.runtime.InvokerInvocationException: hudson.AbortException: SonarQube installation defined in this job (My SonarQube Server) does not match any configured installation. Number of installations that can be configured: 1.
If you want to reassign a lot of jobs to a different SonarQube installation see http://docs.sonarqube.org/display/PLUG/Reassign+Jobs+to+Another+SonarQube+Instance
    at org.jenkinsci.plugins.workflow.cps.CpsStepContext.replay(CpsStepContext.java:497)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:213)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:313)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:129)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:151)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:149)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16)
Caused: hudson.remoting.ProxyException: java.lang.IllegalArgumentException: Failed to prepare withSonarQubeEnv step
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:315)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:129)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:151)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:149)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16)
    at WorkflowScript.run(WorkflowScript:6)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor499.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:74)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=10=]1(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.call(SandboxContinuable.java:33)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.call(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:330)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:82)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:242)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:230)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE

我在我的 jenkins 配置中配置了 Sonar jenkins 插件,如下所示:

我还在 jenkins 中安装了 Sonarqube Quality Gates 插件(而不是根据作者弃用的 Quality Gates 插件)

这是我的作业配置中的管道 groovy 脚本:

node {
  stage('SCM') {
    git credentialsId: 'a011099a-e3f6-4d67-9e99-8887b186699d', url: 'ssh://git@module.domain.com/docs/styled.git'
  }
  stage('build & SonarQube Scan') {
    def sonarqubeScannerHome = tool name: 'domain', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
    withSonarQubeEnv('My SonarQube Server') {
        sh "${sonarqubeScannerHome}/bin/sonar-scanner"
    } // SonarQube taskId is automatically attached to the pipeline context
  }
}

// No need to occupy a node
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}"
    }
  }
}

我真的在寻找任何方法,使用 gitlab-ci 或 jenkins 来利用质量门来 decide 如果部署了应用程序。

上面写的很清楚

SonarQube installation defined in this job (My SonarQube Server) does not match any configured installation.

instructions 说:

Configure your SonarQube server(s)

  • Log into Jenkins as an administrator and go to Manage Jenkins > Configure System:
  • Scroll down to the SonarQube configuration section, click on Add SonarQube, and add the values you're prompted for.

虽然您自己一直在参考文档,但您似乎仍然没有完成安装或使用了不同的安装名称。

如有疑问,请编辑您的 post 并在 Manage Jenkins 中添加来自 SonarQube 配置 的屏幕截图。

您必须安装 SonarScannerQuality gate 插件

然后按照下面的方式进行配置,你必须从 SonarServer Account->Security 生成它的项目密钥,并将安全密钥放在 Sonarqube 部分的 Jenkins 配置中

jpg

在 post 构建操作中包含质量门插件

在 Sonarserver 页面中,您在 Qualitygate 部分配置了您正在尝试使构建失败的属性