首页 > 解决方案 > Jenkins 作业参数的动态或条件显示

问题描述

我正在尝试使用以下 jenkins 作业为作业生成 Active Choices 用户可选参数。

我的要求是在选择/取消选择 heartbeat_consumer 复选框时显示/隐藏文本框。我还想将用户输入的值存储在 中heartbeat_consumer_parms,可以通过env.heartbeat_consumer_parms. 我计划在下游工作中使用这个值。

pipeline {
    agent any
        stages {
            stage('Parameters'){
                steps {
                    script {
                    properties([
                            parameters([
                            text(
                                defaultValue: '''''', 
                                name: 'application_servers',
                                description: 'Please provide semicolon delimited (;) application server list ', 
                            ),
                            [$class: 'CascadeChoiceParameter', 
                                choiceType: 'PT_CHECKBOX', 
                                description: 'Select Services',
                                name: 'application_services_list', 
                                referencedParameters: 'application_servers', 
                                script: 
                                    [$class: 'GroovyScript', 
                                    fallbackScript: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: "return['']"
                                            ], 
                                    script: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: '''
                                            if (application_servers.length() > 0){
                                                return["heartbeat_consumer", "surgeon_cloud_login", "system_configuration"]
                                            }
                                            '''
                                        ] 
                                ]
                            ]
                            ])
                        ])
                    if (application_services_list.contains('heartbeat_consumer')){
                            text(
                            defaultValue: '''''', 
                            name: 'heartbeat_consumer_parms',
                            description: 'Please provide heartbeatconsumer job parms ', 
                            )
                    }
                    }
                }
            }
        }
    }

在当前的实施中,我根本没有看到该文本框heartbeat_consumer_parms正在显示。

标签: jenkinsgroovy

解决方案


适用于您的场景的管道脚本:

properties([
                            parameters([
                                [$class: 'ChoiceParameter', 
                                    choiceType: 'PT_CHECKBOX', 
                                    description: 'Select the Application Service from the Dropdown List', 
                                    filterLength: 1, 
                                    filterable: false, 
                                    name: 'application_services_list', 
                                    script: [
                                        $class: 'GroovyScript', 
                                        fallbackScript: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: 
                                                "return['Could not get the services list']"
                                        ], 
                                        script: [
                                            classpath: [], 
                                            sandbox: false, 
                                            script: 
                                                "return['heartbeat_consumer', 'surgeon_cloud_login', 'system_configuration']"
                                        ]
                                    ]
                                ],
                                [$class: 'DynamicReferenceParameter', 
                                    choiceType: 'ET_FORMATTED_HTML', 
                                    description: 'SAdd params', 
                                    name: 'heartbeat_consumer_parms',
                                    omitValueField: 'true',
                                    referencedParameters: 'application_services_list', 
                                    script: 
                                        [$class: 'GroovyScript', 
                                        script: 'return["Could not get Information"]', 
                                        script: [
                                            script: '''
                                                    
                                                    if (application_services_list.equals("heartbeat_consumer")){
                                                    inputBox="<input type ='text' id = 'myText' name='value' >"
                                                    return inputBox
                                                    }
                                                    
                                                    '''
                                                ]
                                        ]
                                ]
                            ])
                        ])
pipeline {
    environment {
         vari = ""
  }
  agent any
  stages {
      stage ("Example") {
        steps {
         script{

          echo "${params.application_services_list}"
          echo '\n'
          echo "${params.heartbeat_consumer_parms}"
       }
      }
    }
  }
}

请找到输出的屏幕截图:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

构建后管道的输出:

在此处输入图像描述

在此处输入图像描述


推荐阅读