I'm trying to use a job parameter in a pipeline script, following the Parametrized pipeline using template documentation.
My script:
node {
// Display the parameter value of the parameter name "myparam"
println myparam
sh "echo '${myparam}'"
}
but Jenkins cannot find my parameter:
groovy.lang.MissingPropertyException: No such property: myparam for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
What am I missing?
Jenkins Version: 2.8
My full job xml looks like this:
<flow-definition plugin="[email protected]">
<actions />
<description />
<keepDependencies>false</keepDependencies>
<properties>
<com.synopsys.arc.jenkinsci.plugins.jobrestrictions.jobs.JobRestrictionProperty plugin="[email protected]" />
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.StringParameterDefinition>
<name>myparam</name>
<description>bar</description>
<defaultValue>foo</defaultValue>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]">
<script>node { //Dislay the parameter value of the parameter name "myparam" println myparam sh "echo '${myparam}'" }</script>
<sandbox>false</sandbox>
</definition>
<triggers />
</flow-definition>
First define your custom build parameter:
It will automatically show up in page shown after you click "Build with parameters" from the Jenkins job page.
Then access the variable inside the script: