How do I access parameters in a Jenkins pipeline script?

36.2k Views Asked by At

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>
3

There are 3 best solutions below

0
On BEST ANSWER

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
0
On

In addition to Bjorn Reppens example for declarative pipeline here is also one for scripted pipeline syntax:

properties([
  parameters([
    string( name: 'BuildConfiguration', 
            defaultValue: 'Release', 
            description: 'Configuration to build (Debug/Release/...)')
  ])
])

node{
 ...
}

Note that properties block can be specified inside node element or outside of it. Then you access your parameters just like in declarative pipeline via params.BuildConfiguration

0
On

If your parameter name has special characters in it such as dot or hyphen, you can access it this way:

pipeline {
    stages {
        stage('Test') {
            steps {
                echo "${params['app.jms.jndi-provider-url']}"
            }
        }
    }
}