uptoNoGood
uptoNoGood

Reputation: 586

Can jenkins extended choice parameter be made dependent on another parameter's value?

I am using extended choice parameter with JSON parameter type in my declarative Jenkins pipeline. I have found it very good for providing custom UI for parameters and it returns a json based on user inputs.

I have a use case where what options are shown to user depends upon another parameter's value. I can achieve such functionality with active choice parameter but then I am stuck with radio buttons, checkbox, html input etc.

I found a suitable option here where I can make a property inside json dependent on another property:

{
  "title": "An object",
  "type": "object",
  "properties": {
    "fieldOne": {
      "title": "I should be changed to 'foo'",
      "type": "string",
      "enum": ["foo","bar"],
      "default": "bar"
    },
    "depender1": {
      "title": "I depend on fieldOne to be 'foo'",
      "type": "string",
      "enum": ["lorem","ipsum"],
      "options": {
        "dependencies": {
          "fieldOne": "foo"
        }
      }
    },
    "depender2": {
      "title": "I depend on fieldOne to be 'bar'",
      "type": "string",
      "enum": ["dolor", "sit"],
      "options": {
        "dependencies": {
          "fieldOne": "bar"
        }
      }
    }
  }
}

This works great when I try it hereexample

But when I try the same on jenkins, it doesn't work. It shows all 3 textboxes. I saw the option of watching other params too but I couldn't find how to use it as an if else for my parameter.

This is a simple example, what I want to achieve requires UI of a dropdown-1 + Array(dropdown-2 + text field+text-field) where in array's text-field depend on value of dropdown-1, I cannot create the same UI in active choice.

Does any one know how options.dependencies could work in jenkins or same could be achieved using watch/other plugins?

Upvotes: 5

Views: 3948

Answers (1)

Psychozoic
Psychozoic

Reputation: 607

if i got your question right, so you want to make it more smart way to select parameters.

So you can do it via groovy script.

Here is my example you can see on pic: freestyle job config Sorry, but i don't know how to better show freestyle job config.

So, logic is quite simple: i'm collecting JSON on first parameter, and doing some parsing for it. and then im using Environmets variable to show it's contents, depending on result from first part.

ps. i'm struggling right now with variable Hosts, as i don't know how to pass it in final steps without asking user for input. But i believe you got the idea how you can do it.

Upvotes: 0

Related Questions