Wins
Wins

Reputation: 3460

Dynamic parameter on Jenkins Pipeline depending on branch

I have something like this on my jenkins pipeline

properties([
    parameters([
        booleanParam(description: 'Merge master to this branch', name: 'merge_master', defaultValue: false),
        someOtherParameters
    ])
])

Obviously the first parameter that doesn't make sense if the pipeline is running on master branch. So, how can I have this parameter only if the pipeline is not running on master branch?

Upvotes: 4

Views: 4516

Answers (2)

Jon Daley
Jon Daley

Reputation: 31

I was able to use hakamari's example as long as I only had items that had classes that could be found like string and boolean. Since I'm also using (CascadeChoiceParameter), and others, I got the same array error, and I had to convert all to the $class: 'org.biouno.unochoice.CascadeChoiceParameter' syntax to get it to work properly. I'm not sure why, but it sure was frustrating to figure that out.

newParameters.add([
  $class: 'hudson.model.ChoiceParameterDefinition',
  name: 'AWSenvironment',
  choices: ['Development', 'Provision'],
  description: 'where to deploy, most of the time will be Development'
])
newParameters.add([
  $class: 'hudson.plugins.validating_string_parameter.ValidatingStringParameterDefinition',
  name: 'HostName',
  defaultValue: 'AutoBuild',
  description: 'What hostname would you like?<br/><i>Your last name will be prefixed to this name</i>',
  regex: /^[a-zA-Z0-9.:-]+$/,
  failedValidationMessage: "Regular alphanumerics, periods, colons, and hyphens only!",
])

Upvotes: 0

hakamairi
hakamairi

Reputation: 4678

If you haven't found a way yet, you could just add the elements to the parameters list conditionally like this

def list = []
if (env.BRANCH_NAME != 'master') {
    list.add(booleanParam(description: 'Merge master to this branch', name: 'merge_master', defaultValue: false))
}
//example list.add(otherParams)
//finally
properties([parameters(list)])

More on adding to lists in groovy can be found here.

Upvotes: 6

Related Questions