vishwanath jayanth
vishwanath jayanth

Reputation: 143

Jenkins generate new parameters based on another parameter value

I have a choice parameter called 'Data Center' which has two values DC01 and DC02. If user chooses DC01 I want few other build parameters to show up, if he chooses DC02 I want other parameters to show up. Is there any way to do it?

If user chooses DC01, I want two more fields such as 'hostname' and 'IP address' which are just text fields.

I have tried using Active Choice Plugin, but I don't think we can generate text field parameters using that.

Can anyone help?

Upvotes: 6

Views: 15723

Answers (1)

Sourav
Sourav

Reputation: 3392

We can generate text field parameters using Active Choices Plugin.

We will use two types of parameters here: Active Choices Parameter and Active Choices Reactive Reference Parameter

Active Choices Reactive Reference Parameter

In Active Choices Reactive Reference Parameter, there are wide variety of rendering options available, of which one is Formatted HTML i.e. in the return string you can pass the html tags.

So, making use of the above two parameters, here is the pipeline code that will help in your case:

Note: After saving the below pipeline code, first Click on Build Now. After the build got success, then refresh the page. You will be able to see the option Build with Parameters. You can also see in the Job Configuration page, all the fields of This build is parameterized gets populated automatically once you build the job after saving the pipeline.

properties([
                            parameters([
                                [$class: 'ChoiceParameter', 
                                    choiceType: 'PT_CHECKBOX', 
                                    description: 'Select the Application Service from the Dropdown List', 
                                    filterLength: 1, 
                                    filterable: false, 
                                    name: 'data_center', 
                                    script: [
                                        $class: 'GroovyScript', 
                                        fallbackScript: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: 
                                                "return['Could not get the services list']"
                                        ], 
                                        script: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: 
                                                "return['DC01', 'DC02', 'DC03']"
                                        ]
                                    ]
                                ],
                                [$class: 'DynamicReferenceParameter', 
                                    choiceType: 'ET_FORMATTED_HTML', 
                                    description: 'enter job params',
                                    name: 'hostname', 
                                    referencedParameters: 'data_center', 
                                    script: 
                                        [$class: 'GroovyScript', 
                                        fallbackScript: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: "return['']"
                                                ], 
                                        script: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: '''
                                                if (data_center.contains('DC01')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                } else 
                                                if (data_center.contains('DC02')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                }
                                                '''
                                            ] 
                                    ],
                                omitValueField: true
                                ],
                                [$class: 'DynamicReferenceParameter', 
                                    choiceType: 'ET_FORMATTED_HTML', 
                                    description: 'enter job params',
                                    name: 'ipaddress', 
                                    referencedParameters: 'data_center', 
                                    script: 
                                        [$class: 'GroovyScript', 
                                        fallbackScript: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: "return['']"
                                                ], 
                                        script: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: '''
                                                if (data_center.contains('DC01')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                } else 
                                                if (data_center.contains('DC02')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                }
                                                '''
                                            ] 
                                    ],
                                omitValueField: true
                                ],
                                [$class: 'DynamicReferenceParameter', 
                                    choiceType: 'ET_FORMATTED_HTML', 
                                    description: 'enter job params',
                                    name: 'port_number', 
                                    referencedParameters: 'data_center', 
                                    script: 
                                        [$class: 'GroovyScript', 
                                        fallbackScript: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: "return['']"
                                                ], 
                                        script: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: '''
                                                if (data_center.contains('DC02')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                }
                                                '''
                                            ] 
                                    ],
                                omitValueField: true
                                ]                               
                            ])
                        ])
pipeline {
    environment {
         vari = ""
  }
  agent any
  stages {
      stage ("Example") {
        steps {
         script{

          echo "${params.data_center}"
          echo '\n'
          echo "${params.hostname}"
          echo "${params.ipaddress}"
          echo "${params.port_number}"
          
       }
      }
    }
  }
}

Screenshots:

enter image description here

Output when data center DC01 is selected,

enter image description here

enter image description here

Output when data center DC02 is selected,

enter image description here

enter image description here

Upvotes: 8

Related Questions