5

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?

uptoNoGood
  • 566
  • 5
  • 20
  • 1
    How do you integrate this editor in Jenkins? – zett42 Mar 30 '20 at 10:13
  • editor isn't integrated in jenkins but you can provide json schema as a json parameterfor extended choice param, you can see example here https://plugins.jenkins.io/extended-choice-parameter/. Json param groovy script should return a JSON object that corresponds to the "options" object referred to in json-editor – uptoNoGood Mar 30 '20 at 10:16
  • 1
    It's unclear what exactly you're trying to achieve. If you want your Jenkins "build with parameters" page to be interactive, the only option is to use the Active Choice plugin and return a groovy script that will return a JavaScript which your browser will then execute. Alternatively, you may want to run your own frontend to Jenkins and, once your interactive page has settled on the parameters, trigger the job on Jenkins with these parameters. – MaratC Mar 30 '20 at 13:24
  • I want my extended choice parameter to depend on some other parameter, I found a way to do this as shown above, and while it works when i try in json-editor, the same functionality doesn't work on jenkins. The "watch" functionality works in jenkins but from what I saw, i cannot use it to do conditional work. I want to do this in jenkins itself. – uptoNoGood Mar 31 '20 at 04:22

1 Answers1

0

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.

Psychozoic
  • 607
  • 4
  • 9
  • 24