「Jenkins Pipeline」- 在构建过程中,提示用户输入 @20210415

常规用法

在某些构建过程中下,需要用户输入参数,此时可以使用 input 步骤:

pipeline {
    agent any
    stages {
        stage('Example') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "alice,bob"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}

在 script 中

还可以在 script 语句块中使用:

pipeline {
    agent any
    stages {
        stage("foo") {
            steps {
                script {
                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
                }
                echo "${env.RELEASE_SCOPE}"
            }
        }
    }
}

注意事项:
1)如果 parameters 具有单个参数,此时 input 返回为用户输入内容;
2)如果 parameters 具有多个参数,此时返回 MAP 对象,需要通过 name 取值;

注意事项

步骤 input 具有 id 参数,虽然在官方文档中说该 id 参数是可选的,但是在实际使用过程中,该参数需要指定(有些时候又不需要)。

常见问题汇总

Unknown stage section "input"

The new things arriving in Declarative Pipeline!

问题描述:
在 Jenkinsfile 的 stage 中使用 input 产生如下错误:

Unknown stage section "input". Starting with version 0.5, steps in a stage must be in a steps block.

问题原因:
使用 Pipeline 2.5 插件,只允许在 steps 中使用 input 步骤。使用 Pipeline 2.6 插件,可以在 stage 中使用 input 步骤。因为 2.5 是 Feb 01, 2017 发布的,而在 stage 中使用 input 步骤这个特性是 2018/04 前一个周发布的。

解决办法:
升级使用 Pipeline 2.6 版本插件。

相关文章

「Jenkins Pipeline」- 发送 Slack 通知
「Jenkins Pipeline」- 凭证的处理
「Jenkins Pipeline」- SSH
「Jenkins Pipeline」- 发送 HTTP 请求
「Jenkins Pipeline」- 发送邮件

参考文献

Pipeline Syntax/input
Jenkins Declarative Pipeline: How to read choice from input step?


posted @ 2021-04-15 16:05  研究林纳斯写的  阅读(399)  评论(0编辑  收藏  举报