动态或条件显示詹金斯工作的参数

时间:2020-09-18 01:54:03

标签: jenkins groovy

我正在尝试使用以下詹金斯作业来为作业生成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处于显示状态。

2 个答案:

答案 0 :(得分:0)

您的方案的管道脚本:

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

请找到输出的屏幕截图:

enter image description here

enter image description here

enter image description here

enter image description here

构建后管道的输出:

enter image description here

enter image description here

答案 1 :(得分:0)

这对我有用,

 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"]
                                                }
                                                '''
                                            ] 
                                    ]
                                ],
                                [$class: 'DynamicReferenceParameter', 
                                    choiceType: 'ET_FORMATTED_HTML', 
                                    description: 'enter job params',
                                    name: 'hb_job_params', 
                                    referencedParameters: 'application_services_list', 
                                    script: 
                                        [$class: 'GroovyScript', 
                                        fallbackScript: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: "return['']"
                                                ], 
                                        script: [
                                                classpath: [], 
                                                sandbox: false, 
                                                script: '''
                                                if (application_services_list.contains('heartbeat_consumer')){
                                                    return """<textarea name=\"value\" rows=\"5\" class=\"setting-input   \"></textarea>"""

                                                }
                                                '''
                                            ] 
                                    ],
                                omitValueField: true
                                ],
                            ])
                        ])
                    }
                }
            }
        }
    }

build params entered in hb_job_params are present in 'Build Parameters'

我曾经使用DynamicReferenceParameter来使用HTML生成的textarea,但是访问输入的textarea值很棘手。经过一番研究,我发现,

格式化文本输入框的技巧是格式化HTML,以反映Jenkins用于在作业提交表单中显示参数的HTML。最简单的方法是查看Jenkins用于显示现有String参数的HTML。

src:https://github.com/biouno/uno-choice-plugin/wiki/Using-Uno-Choice-for-Dynamic-Input-Text-Box-Defaults