4

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?

Wins
  • 3,420
  • 4
  • 36
  • 70
  • You probably can't do that. You could try to throw an exception if the parameter is provided for master, but it still would be displayed on manual runs. I guess the way to go would be to have 2 separate jobs, on for master and the other for not master branches. – hakamairi Jun 20 '18 at 11:53
  • Thanks @hakamairi for your comment. I'm still not convince that we can't do that given that I've been able to have dynamic trigger job for different branches – Wins Jun 20 '18 at 12:10
  • Have you tried putting an if around that, just to have those parameters enabled on branches other than master? Then you might give it a kick with just a manual run (or two, on master and on something not master). This would provide an easy way for testing, then you will know for sure. – hakamairi Jun 20 '18 at 13:08

2 Answers2

5

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.

hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • Nope, it's not working Here is the error: hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (java.util.ArrayList) values: [[@booleanParam(description=Merge master to this branch,name=merge_master,defaultValue=false), ...]] Possible solutions: tail(), tail(), wait(), last(), any(), max() – Wins May 10 '19 at 09:42
  • I literally use your code snippet. It failed on the line where it adds the `booleanParam` into `list` – Wins May 10 '19 at 10:04
  • I did try using `list.add` but still getting the same exception. Anything that I missed? – Wins May 10 '19 at 18:15
  • 1
    works for me out of the box – Marius Sep 28 '21 at 07:35
  • can this work for DSL pipeline? – WhoAmI Feb 09 '23 at 10:42
0

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!",
])
Jon Daley
  • 11
  • 3