Nino Cangialosi
Nino Cangialosi

Reputation: 65

choice equivalent in Jenkins scripted pipeline

I recently refactored my declartive pipeline into a scripted form. Even if everything seems to work fine, I have a problem coming from the initialization of a multiple valued paramenter.

In my declarative pipeline, I was using the following definition of multiple valued parameter (which was working as it should):

    parameters {

        choice(choices: ['fix', 'major', 'minor', 'none'], description: "Increase version's number: MAJOR.MINOR.FIX", name: "VERSIONING")

    }

I refactored it into this form for the scripted pipeline:

    properties([
        parameters([
            choice(choices: ['fix\nmajor\nminor\nnone'], description: "Increase version's number: MAJOR.MINOR.FIX", name: "VERSIONING"),
        ]),
    ])

The problem is that when I realised that something wasn't working as it should, and printed the variable value with a sh """echo "Versioning parameter check:" ${params.VERSIONING}""" step, I got this from the Jenkins' console:

Versioning parameter check: false

Which is both a value not in the list, and of a different type (boolean instead of string).

Is there a way to implement multiple value parameter initialization in Jenkins scripted pipelines? Why this directive doesn't work out of the box in the scripted pipeline, whereas it does in the declarative type? Is this a bug or am I doing something wrong?

Upvotes: 1

Views: 3370

Answers (3)

Unforgettable631
Unforgettable631

Reputation: 1020

We use the following for a choice parameter, so it looks like your own definition but without the brackets:

properties([
        parameters([
            choice(choices: 'fix\nmajor\nminor\nnone', description: "Increase version's number: MAJOR.MINOR.FIX", name: "VERSIONING"),
        ]),
    ])

If you want the default value to be empty, just add an empty first choice like this:

choice(choices: '\nfix\nmajor\nminor\nnone'

Upvotes: 0

smelm
smelm

Reputation: 1334

Your definition is absolutely fine. You just need to pass the choices as list items and not as \n seperated values.

properties([
    parameters([
        choice(choices: ['fix', 'major', 'minor', 'none'], description: "Increase version's number: MAJOR.MINOR.FIX", name: "VERSIONING"),
    ])
])

Upvotes: 3

MaratC
MaratC

Reputation: 6889

Try the other option for defining choice parameter:

    properties([
        parameters([
        [$class: 'ChoiceParameterDefinition',
           choices: 'fix\nmajor\nminor\nnone\n',
           name: 'VERSIONING',
           description: "Increase version's number: MAJOR.MINOR.FIX"
        ],
        ]),
    ])

Upvotes: 1

Related Questions