Jenkins Groovy extend properties array

佐手、 提交于 2019-12-12 05:47:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!