Jenkins根据另一个参数值生成新参数 [英] Jenkins generate new parameters based on another parameter value

查看:142
本文介绍了Jenkins根据另一个参数值生成新参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

如果用户选择DC01,我还需要两个字段,例如主机名"和"IP地址",它们只是文本字段.

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

我尝试使用Active Choice插件,但我认为我们无法使用该插件生成文本字段参数.

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

任何人都可以帮忙吗?

推荐答案

我们可以使用Active Choices插件生成文本字段参数.

We can generate text field parameters using Active Choices Plugin.

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

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

活动选项反应性参考参数

在"Active Choices反应参考参数"中,有多种可用的渲染选项,其中一个是Formatted HTML,即在返回字符串中,您可以传递html标签.

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:

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

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

屏幕截图:

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

Output when data center DC01 is selected,

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

Output when data center DC02 is selected,

这篇关于Jenkins根据另一个参数值生成新参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆