Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access parameters in a Jenkins pipeline script?

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>
like image 930
RaGe Avatar asked Dec 22 '16 05:12

RaGe


People also ask

How do you pass a parameter in Jenkins pipeline script?

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.

How do you pass parameters in a scripted pipeline?

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.

How do I get Jenkins build parameters?

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.


1 Answers

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
like image 127
Bjorn Reppen Avatar answered Oct 10 '22 12:10

Bjorn Reppen