你应该能够结合“重试”和“输入”来实现那个功能。 大概就是这样。
stage('deploy-test') {
try {
build 'yourJob'
} catch(error) {
echo "First build failed, let's retry if accepted"
retry(2) {
input "Retry the job ?"
build 'yourJob'
}
}
}
如果您希望在没有验证者的情况下完成输入,也可以使用超时功能。还有一个可能有用但我还没有使用过的选项是waitUntil。
编辑: WaitUntil 似乎是最好的选择,您应该稍微尝试一下,但像这样的代码会更加简洁:
stage('deploy-test') {
waitUntil {
try {
build 'yourJob'
} catch(error) {
input "Retry the job ?"
false
}
}
}
顺便提一下,这里有所有步骤的文档 https://jenkins.io/doc/pipeline/steps
这个带有漂亮的递增等待时间
stage('deploy-test') {
def retryAttempt = 0
retry(2) {
if (retryAttempt > 0) {
sleep(1000 * 2 + 2000 * retryAttempt)
}
retryAttempt = retryAttempt + 1
input "Retry the job ?"
build 'yourJob'
}
}
sleep()
默认的时间单位是秒,所以除非你想要等待超过一小时,否则请指定sleep(..., unit:"MILLISECONDS")
或使用更少的秒数。 - Carl Walshstage
块的顶部放置一个retry
:期望在第423行,第17列的阶段“代码覆盖率”中有“steps”,“stages”或“parallel”之一。
- jcollumstage('test') { options { retry(2) } steps { echo "hello" } }
,你可以将它放在步骤之外。 - Chris Maggiulli这个Gist(不是我的)是我在尝试实现这个功能时找到的较好选项之一。https://gist.github.com/beercan1989/b66b7643b48434f5bdf7e1c87094acb9
我将其改为了一个共享库中的方法,只需重试或中止满足我的需求。还添加了最大重试次数,并使超时时间变量化,以便我们可以根据需要更改它。
package com.foo.bar.jenkins
def class PipelineHelper {
def steps
PipelineHelper(steps) {
this.steps = steps
}
void retryOrAbort(final Closure<?> action, int maxAttempts, int timeoutSeconds, final int count = 0) {
steps.echo "Trying action, attempt count is: ${count}"
try {
action.call();
} catch (final exception) {
steps.echo "${exception.toString()}"
steps.timeout(time: timeoutSeconds, unit: 'SECONDS') {
def userChoice = false
try {
userChoice = steps.input(message: 'Retry?', ok: 'Ok', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Check to retry from failed stage']])
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
userChoice = false
}
if (userChoice) {
if (count <= maxAttempts) {
steps.echo "Retrying from failed stage."
return retryOrAbort(action, maxAttempts, timeoutMinutes, count + 1)
} else {
steps.echo "Max attempts reached. Will not retry."
throw exception
}
} else {
steps.echo 'Aborting'
throw exception;
}
}
}
}
}
使用示例,最多重试2次,等待60秒以获取输入。
def pipelineHelper = new PipelineHelper(this)
stage ('Retry Example'){
pipelineHelper.retryOrAbort({
node{
echo 'Here is an example'
throw new RuntimeException('This example will fail.')
}
}, 2, 60)
}
只需记得将节点放在闭包内,以便等待输入不会阻塞执行器。
如果您使用付费版的Jenkins Enterprise Cloudbees,则可以使用Checkpoint插件更好地处理此问题,但不打算为开源Jenkins发布该插件(JENKINS-33846)。