Jenkins generate new parameters based on another parameter value

泄露秘密 提交于 2021-02-17 05:01:31

问题


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?


回答1:


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:

Output when data center DC01 is selected,

Output when data center DC02 is selected,



来源:https://stackoverflow.com/questions/64395488/jenkins-generate-new-parameters-based-on-another-parameter-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!