Unknown stage section “withSonarQubeEnv”

百般思念 提交于 2019-12-03 03:00:34

You're mixing Scripted Pipeline with Declarative Pipeline syntax.

While the snippet you posted from SonarQube documentation would work, you will need to adapt it since you're using Declarative (as indicated by the "Not a valid stage section definition" error).

Normally, you'd define a tools section in your Pipeline, but it looks like the SonarQube plugin doesn't support Declarative, nor does it add itself to the PATH.

Since you can't normally define variables in Declarative Pipeline, the script step has to be used to call the tool step and store the path to the installed tool. For example:

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      steps {
        script {
          // requires SonarQube Scanner 2.8+
          scannerHome = tool 'SonarQube Scanner 2.8'
        }
        withSonarQubeEnv('SonarQube Scanner') {
          sh "${scannerHome}/bin/sonar-scanner"
        }
      }
    }
  }
}

The tool name "SonarQube Scanner 2.8" needs to match the "Name" field of a SonarQube Installation on the Global Tools Configuration page. The name used in the withSonarQubeEnv step needs to match the "Name" field of a SonarQube server defined on the Configure System page.


If the SonarQube plugin did support Declarative, and added itself to PATH, the Pipeline could be a wee bit simpler:

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      tools {
        sonarQube 'SonarQube Scanner 2.8'
      }
      steps {
        withSonarQubeEnv('SonarQube Scanner') {
          sh 'sonar-scanner'
        }
      }
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!