问题
Inside my jenkinsfile I want to set multiple properties based on some dependencies.
So in the top of my jenkinsfile I am setting my first parameter:
properties([
parameters([
booleanParam(
defaultValue: false,
description: '...',
name: 'parameters1'
),
])
])
Some lines below I want to set another parameter if a condition is met
if(awesomeCondition) {
properties([
parameters([
booleanParam(
defaultValue: false,
description: '...',
name: 'parameters2'
),
])
])
}
The problem I am now running into is that the second parameter is overriding the first parameter. How to handle this correctly?
回答1:
The properties step overrides the existing job properties so, as you noted, the second call overrides the previous one. This is expected behaviour.
What you need to do is to keep a list of new parameters and then use a single call to properties step:
def newParameters = []
newParameters.add([
$class: 'hudson.model.BooleanParameterDefinition',
name: "p1",
default: false,
description:"Some help text"
])
...
if(awesomeCondition) {
newParameters.add([
$class: 'hudson.model.BooleanParameterDefinition',
name: "p2",
default: false,
description:"Some help text"
])
}
...
properties([parameters(newParameters)])
The $class: 'hudson.model.BooleanParameterDefinition' is needed since we are creating the objects outside of the properties step. For other types of parameters see sub-classes to this class.
来源:https://stackoverflow.com/questions/42277315/jenkins-groovy-extend-properties-array