【问题标题】:How to pass input values to Scheduled Jenkins pipeline job?如何将输入值传递给计划的 Jenkins 管道作业?
【发布时间】:2020-02-15 00:28:38
【问题描述】:

我有一个没有任何构建参数的 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'
                                        )
                        ]
                )

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

【问题讨论】:

    标签: jenkins input jenkins-pipeline jenkins-groovy


    【解决方案1】:

    您可以将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}]")
                                    }
                                }
    

    Pipeline: How to add an input step, with timeout, that continues if timeout is reached, using a default value

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2020-12-05
    • 2016-08-29
    相关资源
    最近更新 更多