Jenkins 根据另一个参数值生成新参数

vis*_*nth 5 jenkins jenkins-groovy jenkins-pipeline

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

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

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

有人可以帮忙吗?

Sou*_*tta 7

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

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

主动选择 反应参考参数

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

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

注意:保存以下管道代码后,首先单击Build Now。构建成功后,刷新页面。您将能够看到该选项Build with ParametersThis 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}"
          
       }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

截图:

在此输入图像描述

DC01选择数据中心时输出,

在此输入图像描述

在此输入图像描述

DC02选择数据中心时输出,

在此输入图像描述

在此输入图像描述

  • 您的回答确实很有帮助,但我不希望默认显示这些字段。我希望他们有条件地出现。这是我们能够实现的吗? (2认同)