为什么 SONAR 在 waitForQualityGate() 处失败并出现错误 401?

Why SONAR fails at waitForQualityGate() with error 401?

使用管道代码,

stage ('SonarQube') {
    withSonarQubeEnv {
        dir ('mydir/') {
            sh "'${mvnHome}/bin/mvn' sonar:sonar -Dsonar.login=something -Dsonar.projectKey=someproj -Dsonar.projectName=somename"
        }
    }
    timeout(time: 15, unit: 'MINUTES') {
        def qg = waitForQualityGate()
        if (qg.status != 'OK') {
            error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }

在第一个 mvn 部分中正确地进行并在 waitforqualitygate() 操作时中断:

org.sonarqube.ws.client.HttpException: Error 401 on http://mysonarserver/sonar/api/ce/task?id=somecode

link 是可点击的,会导致填充的 json 结构。

为什么构建失败? webhook 似乎在sonar 中设置正确,其他sonar 项目正常工作,jenkis 中的webhook 似乎也处于活动状态。

official documentation of the SonarQube Scanner for Jenkins 中所述,您必须在 withSonarQubeEnv 之外使用 waitForQualityGate()

node {
  stage('SCM') {
    git 'https://github.com/foo/bar.git'
  }
  stage('SonarQube analysis') {
    withSonarQubeEnv('My SonarQube Server') {
      sh 'mvn clean package sonar:sonar'
    } // 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}"
    }
  }
}