【问题标题】:Why SONAR fails at waitForQualityGate() with error 401?为什么 SONAR 在 waitForQualityGate() 失败并出现错误 401?
【发布时间】:2017-12-08 03:19:00
【问题描述】:

使用管道代码,

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 似乎也处于活动状态。

【问题讨论】:

    标签: jenkins sonarqube jenkins-pipeline sonarqube-scan


    【解决方案1】:

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

    【讨论】:

    • 对不起,我粘贴了错误的代码。它在 withSonarQubeEnv 之外
    • 在将 Quality Gate 步骤添加到 JenkinsFile 时遇到完全相同的错误。
    • SonarQube 版本是 6.3,我使用 DSL 进行管道。这个阶段看起来像这样.stage("SonarQube Quality Gate") { steps { script { timeout(time: 1, unit: 'HOURS') { // 以防万一出现问题,管道将在超时后被杀死 def qg = waitForQualityGate() // 重用之前由 withSonarQubeEnv 收集的 taskId if (qg.status != 'OK') { error "Pipeline aborted due to quality gate failure: ${qg.status}" } } } } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多