Why SONAR fails at waitForQualityGate() with error

2019-07-25 06:29发布

Using pipeline code,

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}"
        }
    }

Which progresses crrectly over the first mvn section and breaks on waitforqualitygate() operation:

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

the link is clickable and leads to a filled json structure.

Why the build fails? Webhook seems to be set properly in sonar, and other sonar projects are working correctly, webhook in jenkis also seems to be active.

1条回答
放荡不羁爱自由
2楼-- · 2019-07-25 07:03

Like described in the official documentation of the SonarQube Scanner for Jenkins, you must use waitForQualityGate() outside of 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}"
    }
  }
}
查看更多
登录 后发表回答