Jan Hudec
Jan Hudec

Reputation: 76236

Dynamically evaluate default in Jenkins pipeline build parameter

In Jenkins declarative pipeline we can define build parameters like

pipeline {
    …
    parameters {
        string(name: 'PARAMETER', defaultValue: 'INITIAL_DEFAULT')
        choice(name: 'CHOICE', choices: ['THIS', 'THAT'])
    }
    …
}

However the parameter definitions of the job are only updated when the job runs after the build parameters dialog was already shown. That is, when I change the INITIAL_DEFAULT to something else, the next build will still default to INITIAL_DEFAULT and only the one after that will use the new value.

The same problem is with the choices, and there it is even more serious, because string default can be overwritten easily when starting the build, but if the new option isn't there, it cannot be selected at all.

So is there a way to define functions or expressions that will be executed before the parameter dialog to calculate current values (from files, variable in global settings or any other suitable external configuration)?

I remember using some plugins for this in the past with free-style jobs, but searching the plugin repository I can't find any that would mention how to use it with pipelines.

I don't care too much that the same problem applies to adding and removing parameters, because that occurs rarely. But we have some parameters where the default changes often and we need the next nightly to pick up the updated value.

Upvotes: 0

Views: 1164

Answers (1)

Jan Hudec
Jan Hudec

Reputation: 76236

It turns out the extended-choice-parameter does work with pipeline, and the configurations can be generated by the directive generator. It looks something like

extendedChoice(
    name: 'PARAMETER',
    type: 'PT_TEXTBOX',
    defaultPropertyFile: '/var/lib/jenkins/something.properties',
    defaultPropertyKey: 'parameter'
)

(there are many more options available in the generator)

Groovy script to get global environment variables can be had from this other answer.

Upvotes: 1

Related Questions