waitForQualityGate return 401

waitForQualityGate return 401

我的服务器上安装了 SonarQube 服务器和声纳扫描仪 + Jenkins(没有 docker)。 我遇到的问题是质量门步骤总是 return 401,即使分析工作正常也很艰难。我怀疑可能存在授权问题,但我不知道如何发送到 waitForQualityGate 方法。

INFO: Analysis total time: 12.972 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 14.641s
INFO: Final Memory: 13M/50M
INFO: ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Quality gate)
[Pipeline] waitForQualityGate
Checking status of SonarQube task 'AXoxAunUF1YE_9gTnBHP' on server 'SonarQube'
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
org.sonarqube.ws.client.HttpException: Error 401 on https://<sonar-url>/api/ce/task?id=AXoxAunUF1YE_9gTnBHP : 
    at org.sonarqube.ws.client.BaseResponse.failIfNotSuccessful(BaseResponse.java:36)
    at hudson.plugins.sonar.client.HttpClient.getHttp(HttpClient.java:38)
    at hudson.plugins.sonar.client.WsClient.getCETask(WsClient.java:51)
    at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.checkTaskCompleted(WaitForQualityGateStep.java:234)
    at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.start(WaitForQualityGateStep.java:171)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:319)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
    at jdk.internal.reflect.GeneratedMethodAccessor544.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
    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:163)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
    at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:161)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.run(WorkflowScript:25)

我的 Jenkinsfile 看起来像这样

pipeline {
    agent any

    stages {
        stage('Clone sources') {
            steps {
                git branch: 'main', 
                credentialsId: '<github-credentials-id>',
                url: '<github-url>'
            }
        }
        stage('SonarQube analysis') {
            steps {
                withSonarQubeEnv('SonarQube') { 
                    sh "sonar-scanner \
                        -Dsonar.projectKey=<project-key> \
                        -Dsonar.sources=. \
                        -Dsonar.host.url=https://<sonar-url> \
                        -Dsonar.login=<scanner-user-token>"
                }
            }
        }
        stage("Quality gate") {
            steps {
                timeout(time: 1, unit: 'HOURS') {
                    waitForQualityGate abortPipeline: true
                }
            }
        }
    }
}

为了生成 <scanner-user-token> 我在 SonarQube 中创建了一个新用户(扫描仪用户)并为其生成了一个令牌。

到目前为止我尝试了什么

...
        stage("Quality gate") {
            steps {
                timeout(time: 1, unit: 'HOURS') {
                    waitForQualityGate abortPipeline: true,
                    credentialsId: '<global-scanner-user-token-id>',
                }
            }
        }
...

其他配置

版本

谢谢!

我设法解决了这个问题:

  1. 从 SonarQube 分析步骤中删除 -Dsonar.login
       stage('SonarQube analysis') {
            steps {
                withSonarQubeEnv('SonarQube') { 
                    sh "sonar-scanner \
                        -Dsonar.projectKey=<project-key> \
                        -Dsonar.sources=. \
                        -Dsonar.host.url=https://<sonar-url> "
                }
            }
        }
  1. 使用
  2. 在 Jenkins 中创建全局机密文本
  3. 在 Jenkins 内部转到 **配置系统 -> SonarQube 服务器 ** 并设置服务器身份验证令牌,即在第 2 点设置的全局密码。还要检查环境变量

我通过将 localhost 更改为 IPv4 地址解决了这个问题。