Jenkins generate new parameters based on another parameter value

后端 未结 1 613
梦谈多话
梦谈多话 2021-01-27 10:15

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

相关标签:
1条回答
  • 2021-01-27 10:26

    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,

    0 讨论(0)
提交回复
热议问题