【问题标题】:Jenkins generate new parameters based on another parameter valueJenkins 根据另一个参数值生成新参数
【发布时间】:2021-01-31 08:23:17
【问题描述】:

我有一个名为“数据中心”的选择参数,它有两个值 DC01 和 DC02。如果用户选择 DC01,我希望显示一些其他构建参数,如果他选择 DC02,我希望显示其他参数。有什么办法吗?

如果用户选择 DC01,我想要另外两个字段,例如“主机名”和“IP 地址”,它们只是文本字段。

我尝试过使用 Active Choice Plugin,但我认为我们不能使用它生成文本字段参数。

谁能帮忙?

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-groovy


    【解决方案1】:

    我们可以使用 Active Choices Plugin 生成文本字段参数。

    我们将在这里使用两种类型的参数:Active Choices ParameterActive Choices Reactive Reference Parameter

    Active Choices 反应参考参数

    在 Active Choices Reactive Reference Parameter 中,有多种渲染选项可用,其中一种是 Formatted HTML,即在返回字符串中您可以传递 html 标签。

    因此,利用上述两个参数,以下是对您的情况有所帮助的管道代码:

    注意:保存下面的管道代码后,先点击Build Now。构建成功后,刷新页面。您将能够看到选项Build with Parameters。您还可以在作业配置页面中看到,在保存管道后构建作业后,This build is parameterized 的所有字段都会自动填充。

    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}"
              
           }
          }
        }
      }
    }
    

    截图:

    选择数据中心DC01时的输出,

    选择数据中心DC02时的输出,

    【讨论】:

    • 您的回答确实很有帮助,但我不希望这些字段显示为默认值。我希望他们有条件地出现。这是我们可以实现的吗?
    猜你喜欢
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多