Eric Workman

Parameters in a Jenkins Scripted Pipeline

Published on

I needed to add parameters to a Jenkins Scripted Pipeline recently, but I ran into issues with the documented syntax for parameters.

The docs on parameters specify that the following block is the correct syntax:

properties([
  parameters([
    string(
      defaultValue: 'Hello',
      description: 'How should I greet the world?',
      name: 'Greeting'
    )
  ])
])

node {
  echo "${params.Greeting} World!"
}

This looks very close to the Declarative Pipeline syntax, but results in an IllegalArgumentException:

java.lang.IllegalArgumentException: Expected named arguments but got 
[@string(name=Greeting,defaultValue=Hello,description=How should I greet the world?)]

This is the proper syntax for the same parameters:

properties([
  [$class: 'ParametersDefinitionProperty', parameterDefinitions: [
    [$class: 'StringParameterDefinition',
      name: 'Greeting',
      defaultValue: 'Hello',
      description: 'How should I greet the world?']
  ]]
])

node {
  echo "${params.Greeting} World!"
}

Boolean parameters should be able to use BooleanParameterDefinition instead of StringParameterDefinition.