Unknown stage section "withSonarQubeEnv"
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' } } } }}