常规用法
在某些构建过程中下,需要用户输入参数,此时可以使用 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 版本插件。
参考文献
WikiNotes/在构建过程中,提示用户输入
Pipeline Syntax/input
Jenkins Declarative Pipeline: How to read choice from input step?
本文介绍了在Jenkins Pipeline中如何使用`input`步骤进行用户交互输入,包括在常规用法和`script`块中的应用,并详细说明了参数处理方式。当`parameters`只有一个时,返回值为用户输入内容;如果有多个参数,则返回一个MAP对象。同时,注意`input`的`id`参数在某些情况下可能是必需的。文章还提及了一个常见问题,即在Pipeline 2.5版本中`input`必须放在`steps`块内,而在2.6版本及以上则可以在`stage`中直接使用。解决方案是升级到Pipeline 2.6插件。
1859

被折叠的 条评论
为什么被折叠?



