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'
}
}
}
}
}
mindparse
Updated on March 15, 2020Comments
-
mindparse about 4 years
I am experimenting using Pipeline jobs with the Blue Ocean beta plugin enabled on our Jenkins server.
We have a sonarqube scanner build step configured in one of our freestyle jobs, which I now want to add a stage for in my Jenkins file.
I see from the sonar docs there is a way to achieve this here
We are using Jenkins 2.32.3 and have SonarQube scanner 2.8 installed.
I tried the suggested stage config block, where our scanner is named 'SonarQube Scanner'
stage('SonarQube analysis') { // requires SonarQube Scanner 2.8+ def scannerHome = tool 'SonarQube Scanner 2.8'; withSonarQubeEnv('SonarQube Scanner') { sh "${scannerHome}/bin/sonar-scanner" } }
But I get an error thrown from Jenkins:
WorkflowScript: 29: Not a valid stage section definition: "def scannerHome = tool 'SonarQube Scanner 2.8'". Some extra configuration is required. @ line 29, column 5.
What am I missing?
Thanks
P.S - I definitely have the sonar scanner installed:
Update
I get the following output in the console log:
hudson.remoting.ProxyException: java.lang.IllegalArgumentException: Failed to prepare withSonarQubeEnv step at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:315) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:129) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108) at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:151) at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16) at WorkflowScript.run(WorkflowScript:46) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:163) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:385) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:179) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:177) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:384) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:97) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:284) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:283) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:95) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:237) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withCredentialsBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:236) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:94) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:316) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:315) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:93) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:215) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:214) at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/lib/jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:91) at ___cps.transform___(Native Method) at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57) at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109) at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82) at sun.reflect.GeneratedMethodAccessor355.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46) at com.cloudbees.groovy.cps.Next.step(Next.java:74) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: hudson.remoting.ProxyException: org.codehaus.groovy.runtime.InvokerInvocationException: hudson.AbortException: SonarQube installation defined in this job (SonarQube Scanner) does not match any configured installation. Number of installations that can be configured: 1. If you want to reassign a lot of jobs to a different SonarQube installation see http://docs.sonarqube.org/display/PLUG/Reassign+Jobs+to+Another+SonarQube+Instance at org.jenkinsci.plugins.workflow.cps.CpsStepContext.replay(CpsStepContext.java:492) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:213) at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:313) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:129) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108) at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:151) at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16) ... 29 more Caused by: hudson.remoting.ProxyException: hudson.AbortException: SonarQube installation defined in this job (SonarQube Scanner) does not match any configured installation. Number of installations that can be configured: 1. If you want to reassign a lot of jobs to a different SonarQube installation see http://docs.sonarqube.org/display/PLUG/Reassign+Jobs+to+Another+SonarQube+Instance at hudson.plugins.sonar.SonarInstallation.checkValid(SonarInstallation.java:170) at hudson.plugins.sonar.SonarBuildWrapper.setUp(SonarBuildWrapper.java:81) at org.jenkinsci.plugins.workflow.steps.CoreWrapperStep$Execution.start(CoreWrapperStep.java:80) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:184) ... 42 more Finished: FAILURE
Update 2:
Manage Jenkins > Configure System
-
mindparse about 7 yearsThanks for this, I get past that error but now Jenkins tells me
Use a tool from a predefined Tool Installation
. I have checked the sonarqube scanners installations in my global tool settings in Jenkins (see my edited post) I definitely have it installed as I used it for existing freestyle jobs. Is there a way I can verify the installed path of where the scanner is on my system? -
Christopher Orr about 7 yearsCan you paste the build log somewhere (or add it to your question)? That text is the description of the
tool
step, rather than an error message. I installed the SonarQube plugin, created a new version in "Global Tool Configuration" called "SonarQube Scanner 2.8" (and chose v2.8), and after configuring a SonarQube server, the Pipeline above worked as expected. -
mindparse about 7 yearsSee my updated question with an error that comes out in the console log - it seems it is struggling to find where the scanner is installed. Think I had better see if I can find where it is on our Jenkins box!
-
Christopher Orr about 7 years@mindparse The log shows that a configuration with the name "SonarQube Scanner" can't be found — that's something you need to configure in Manage Jenkins > Configure Systems > SonarQube installations > SonarQube Installations, where you enter the server URL etc. Probably you've already done that, but maybe didn't use the name "SonarQube Scanner" as used by the
withSonarQubeEnv
step? -
mindparse about 7 yearsAh, I was looking under Global Tool Settings, not Configure Systems as you have described. Looking under SonarQube Servers I can see a 'SonarQube' installation but I noticed the 'Enable injection of SonarQube server configuration as build environment variables' was not checked so i have ticked this box (see updated screengrab) and tried my build again, this time making sure I use the name 'SonarQube' in my step. But I still get an error - ERROR: No tool named SonarQube found. I can't think what else to check\try!
-
Christopher Orr about 7 yearsThe name used in the
tool
step needs to match the name from Global Tool Configuration, which in your screenshot is "SonarQube Scanner" (i.e. no "2.8" suffix). The name in thewithSonarQubeEnv
step needs to match the SonarQube Installations > Name, which is just "SonarQube" in your screenshot (which is what I was trying to say in my previous comment, and sounds like you fixed). -
mindparse about 7 yearsGroan...I am an idiot, that was exactly it! Thanks for all your help. I just never looked at the Configure systems area, I was always referring to the Global Tool Settings. Thank you so much :)