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

查看:23
本文介绍了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 Plugin,但我认为我们不能使用它生成文本字段参数.

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

谁能帮忙?

推荐答案

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

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 反应参考参数

在 Active Choices Reactive Reference Parameter 中,有多种渲染选项可用,其中一种是 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 '
'
          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天全站免登陆