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>
Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.
Configuring parameters with Scripted Pipeline is done with the properties step, which can be found in the Snippet Generator. If you configured your pipeline to accept parameters using the Build with Parameters option, those parameters are accessible as members of the params variable.
Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.
First define your custom build parameter:
pipeline {
parameters {
string( name: 'BuildConfiguration',
defaultValue: 'Release',
description: 'Configuration to build (Debug/Release/...)')
}
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:
echo "Building configuration: ${params.BuildConfiguration}"
echo "Building configuration: " + params.BuildConfiguration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With