使用 Jenkins 管道继续使用默认设置的超时输入步骤
Input step with timeout which continues with default settings using Jenkins pipeline
所需 Jenkins 管道的描述:
- 输入等待1分钟("Is this a release build? yes/no")
- 如果输入是,则执行发布类型构建(带有构建号和部署)
- 如果超时或用户拒绝,则进行测试构建
- 如果用户按下中止执行正常中止
我当前的代码是:
try {
timeout(1) {
input message: 'Do you want to release this build?',
parameters: [[$class: 'BooleanParameterDefinition',
defaultValue: false,
description: 'Ticking this box will do a release',
name: 'Release']]
}
} catch (err) {
def hi = err.getCauses()
echo "Exception thrown:\n ${hi}"
echo err.getLocalizedMessage()
echo err.getCause()
echo err.toString()
echo err.getClass().getName()
}
但这给出了相同的(据我所知)行为并捕获了用户按下 "Abort" 和输入超时的错误。
这是超时的输出:
[Pipeline] timeout
[Pipeline] {
[Pipeline] input
Input requested
[Pipeline] }
[Pipeline] // timeout
[Pipeline] echo
Exception thrown:
[org.jenkinsci.plugins.workflow.support.steps.input.Rejection@5ac94906]
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] End of Pipeline
Finished: SUCCESS
当我按'Abort'时,除了Rejection@
后面的十六进制外,其他都是一样的
如果输入步骤可以在超时后选择继续使用默认选项,那就太好了。
编辑:向错误添加更多打印内容以尝试确定其类型
您可以提取因异常原因而被拒绝的用户的信息。 org.jenkinsci.plugins.workflow.support.steps.input.Rejection 有一个 getUser() 方法(其中 returns 'SYSTEM' 用于超时和完整用户名用于中止)。
try {
timeout(time: 15, unit: 'SECONDS') {
input message: 'Do you want to release this build?',
parameters: [[$class: 'BooleanParameterDefinition',
defaultValue: false,
description: 'Ticking this box will do a release',
name: 'Release']]
}
} catch (err) {
def user = err.getCauses()[0].getUser()
echo "Aborted by:\n ${user}"
}
请注意,这需要 groovy 沙箱的批准(管理 Jenkins > 进程中脚本批准)。
我最近实施了类似的行为。
此 声明式管道 代码要求在启动时提供 BRANCH_TO_BUILD
参数(仅当手动启动时,否则将使用默认值)然后 [=32= 的交互式输入] 分支已激活。
所以用户有几个选择:
- 保持无动作(将使用默认值)
- 确认默认值
- 输入新值
- 按[中止]中止执行
是的,记得确认 Groovy 功能如 Blazej 所写
pipeline {
agent none
parameters {
string(name: 'BRANCH_TO_BUILD', defaultValue: "develop", description: 'GIT branch to build')
}
environment {
GIT_URL = 'ssh://user@git/repo.git'
BRANCH_TO_BUILD_DEFAULT = 'develop'
BRANCH_TO_BUILD_REQUESTED = "${params.BRANCH_TO_BUILD}"
}
stage('Configure the build') {
agent none
steps {
echo "Prompt a user for the branch to build (default: ${BRANCH_TO_BUILD_DEFAULT})"
script {
try {
timeout(time:30, unit:'SECONDS') {
BRANCH_TO_BUILD_REQUESTED = input(
message: 'Input branch to build',
parameters: [
[$class: 'TextParameterDefinition',
defaultValue: BRANCH_TO_BUILD_DEFAULT,
description: 'Branch name', name: 'Enter branch name (or leave default) and press [Proceed]:']
])
echo ("User has entered the branch name: " + BRANCH_TO_BUILD_REQUESTED)
}
} catch(err) { // timeout reached or input Aborted
def user = err.getCauses()[0].getUser()
if('SYSTEM' == user.toString()) { // SYSTEM means timeout
echo ("Input timeout expired, default branch will be used: " + BRANCH_TO_BUILD_DEFAULT)
BRANCH_TO_BUILD_REQUESTED = BRANCH_TO_BUILD_DEFAULT
} else {
echo "Input aborted by: [${user}]"
error("Pipeline aborted by: [${user}]")
}
}
}
}
}
stage('Checkout') {
agent{node {label 'worker-1'}}
steps {
echo "Checkout will be done for Git branch: ${BRANCH_TO_BUILD_REQUESTED}"
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_TO_BUILD_REQUESTED}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: ${GIT_URL]}]])
}
}
}
所需 Jenkins 管道的描述:
- 输入等待1分钟("Is this a release build? yes/no")
- 如果输入是,则执行发布类型构建(带有构建号和部署)
- 如果超时或用户拒绝,则进行测试构建
- 如果用户按下中止执行正常中止
我当前的代码是:
try {
timeout(1) {
input message: 'Do you want to release this build?',
parameters: [[$class: 'BooleanParameterDefinition',
defaultValue: false,
description: 'Ticking this box will do a release',
name: 'Release']]
}
} catch (err) {
def hi = err.getCauses()
echo "Exception thrown:\n ${hi}"
echo err.getLocalizedMessage()
echo err.getCause()
echo err.toString()
echo err.getClass().getName()
}
但这给出了相同的(据我所知)行为并捕获了用户按下 "Abort" 和输入超时的错误。
这是超时的输出:
[Pipeline] timeout
[Pipeline] {
[Pipeline] input
Input requested
[Pipeline] }
[Pipeline] // timeout
[Pipeline] echo
Exception thrown:
[org.jenkinsci.plugins.workflow.support.steps.input.Rejection@5ac94906]
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] End of Pipeline
Finished: SUCCESS
当我按'Abort'时,除了Rejection@
后面的十六进制外,其他都是一样的如果输入步骤可以在超时后选择继续使用默认选项,那就太好了。
编辑:向错误添加更多打印内容以尝试确定其类型
您可以提取因异常原因而被拒绝的用户的信息。 org.jenkinsci.plugins.workflow.support.steps.input.Rejection 有一个 getUser() 方法(其中 returns 'SYSTEM' 用于超时和完整用户名用于中止)。
try {
timeout(time: 15, unit: 'SECONDS') {
input message: 'Do you want to release this build?',
parameters: [[$class: 'BooleanParameterDefinition',
defaultValue: false,
description: 'Ticking this box will do a release',
name: 'Release']]
}
} catch (err) {
def user = err.getCauses()[0].getUser()
echo "Aborted by:\n ${user}"
}
请注意,这需要 groovy 沙箱的批准(管理 Jenkins > 进程中脚本批准)。
我最近实施了类似的行为。
此 声明式管道 代码要求在启动时提供 BRANCH_TO_BUILD
参数(仅当手动启动时,否则将使用默认值)然后 [=32= 的交互式输入] 分支已激活。
所以用户有几个选择:
- 保持无动作(将使用默认值)
- 确认默认值
- 输入新值
- 按[中止]中止执行
是的,记得确认 Groovy 功能如 Blazej 所写
pipeline {
agent none
parameters {
string(name: 'BRANCH_TO_BUILD', defaultValue: "develop", description: 'GIT branch to build')
}
environment {
GIT_URL = 'ssh://user@git/repo.git'
BRANCH_TO_BUILD_DEFAULT = 'develop'
BRANCH_TO_BUILD_REQUESTED = "${params.BRANCH_TO_BUILD}"
}
stage('Configure the build') {
agent none
steps {
echo "Prompt a user for the branch to build (default: ${BRANCH_TO_BUILD_DEFAULT})"
script {
try {
timeout(time:30, unit:'SECONDS') {
BRANCH_TO_BUILD_REQUESTED = input(
message: 'Input branch to build',
parameters: [
[$class: 'TextParameterDefinition',
defaultValue: BRANCH_TO_BUILD_DEFAULT,
description: 'Branch name', name: 'Enter branch name (or leave default) and press [Proceed]:']
])
echo ("User has entered the branch name: " + BRANCH_TO_BUILD_REQUESTED)
}
} catch(err) { // timeout reached or input Aborted
def user = err.getCauses()[0].getUser()
if('SYSTEM' == user.toString()) { // SYSTEM means timeout
echo ("Input timeout expired, default branch will be used: " + BRANCH_TO_BUILD_DEFAULT)
BRANCH_TO_BUILD_REQUESTED = BRANCH_TO_BUILD_DEFAULT
} else {
echo "Input aborted by: [${user}]"
error("Pipeline aborted by: [${user}]")
}
}
}
}
}
stage('Checkout') {
agent{node {label 'worker-1'}}
steps {
echo "Checkout will be done for Git branch: ${BRANCH_TO_BUILD_REQUESTED}"
checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_TO_BUILD_REQUESTED}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: ${GIT_URL]}]])
}
}
}