首页 > 解决方案 > 如何将输入值传递给计划的 Jenkins 管道作业?

问题描述

我有一个没有任何构建参数的 Jenkins 管道。但input里面有模板。因此,当使用 开始作业时Build now,它会被触发并等待用户输入,Input Requested并且将选项填充为选项。

当我手动运行时,这工作正常。但是,我想为input选择变量启用默认值,这样当计划的作业开始时,它不会等待用户输入(通过Input Requested)并继续使用默认值。

我的输入模板看起来像这样。

env.cluster_to_select = input(
                        id: 'cluster_to_select', message: 'Select a choice',
                        parameters: [
                                 choice(name: 'clusters',
                                       choices: env.list_files, //can take from populated string
                                       description: 'Based on cluster selection, nodes are shown'
                                        )
                        ]
                )

如何知道作业是否通过调度程序触发并传递输入的默认值。

标签: jenkinsinputjenkins-pipelinejenkins-groovy

解决方案


您可以将其包装input在一个timeout块中,该块本身应该在一个try块中。然后捕获异常,检查它是否超时或被用户中止,并相应地使用默认参数或中止构建。

env.cluster_to_select = try {
                            timeout(time: 120, unit: 'SECONDS') {
                                input(
                                    id: 'cluster_to_select', message: 'Select a choice',
                                    parameters: [
                                        choice(name: 'clusters',
                                            choices: env.list_files, //can take from populated string
                                            description: 'Based on cluster selection, nodes are shown'
                                            )
                                        ]
                                    )
                                }
                            }   catch(err) { // timeout reached or input aborted by user
                                def user = err.getCauses()[0].getUser()
                                if (user.toString() == 'SYSTEM') { // SYSTEM is timeout
                                    echo('Using default parameters')
                                } else {
                                    currentBuild.result = 'ABORTED'
                                    error("Pipeline aborted by: [${user}]")
                                }
                            }

请参阅管道:如何使用默认值添加一个带超时的输入步骤,如果达到超时,该步骤将继续


推荐阅读