一尘不染

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

jenkins

使用管道代码,

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

链接是可单击的,并导致填充的json结构。

为什么构建失败?Webhook似乎在声纳中设置正确,并且其他声纳项目正常运行,jenkis中的webhook似乎也处于活动状态。


阅读 1400

收藏
2020-07-25

共1个答案

一尘不染

就像SonarQube Scanner for
Jenkins
官方文档中所述,您必须waitForQualityGate()在以下范围之外使用withSonarQubeEnv

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}"
    }
  }
}
2020-07-25