Groovy 如何在Jenkins管道中为失败的阶段实现重试选项?
我有一个包含多个阶段的Jenkins文件,其中一个实际上是另一个作业(部署作业),在某些情况下可能会失败 我知道我可以使用Jenkinsfile进行提示,但我真的不知道如何为这个作业实现重试机制 我希望能够单击失败的阶段并选择重试。Groovy 如何在Jenkins管道中为失败的阶段实现重试选项?,groovy,jenkins-workflow,jenkins-pipeline,jenkinsfile,Groovy,Jenkins Workflow,Jenkins Pipeline,Jenkinsfile,我有一个包含多个阶段的Jenkins文件,其中一个实际上是另一个作业(部署作业),在某些情况下可能会失败 我知道我可以使用Jenkinsfile进行提示,但我真的不知道如何为这个作业实现重试机制 我希望能够单击失败的阶段并选择重试。 您应该能够结合重试+输入来完成此操作 诸如此类 stage('deploy-test') { try { build 'yourJob' } catch(error) { echo "First build failed, let's
您应该能够结合重试+输入来完成此操作 诸如此类
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'
}
}
}
如果希望在无人验证的情况下完成输入,也可以使用timeout。
还有一个waitUntil可能有用,但我还没有使用它
编辑:
Waitill看起来绝对是最好的,你应该玩一玩,但这样更干净:
stage('deploy-test') {
waitUntil {
try {
build 'yourJob'
} catch(error) {
input "Retry the job ?"
false
}
}
}
顺便说一下,这里有所有的步骤这个要点(不是我的)是我在尝试实现这个功能时发现的更好的选择之一
将其更改为共享库中的方法,该方法只是根据我的需要重试或中止。还添加了max retries并设置了timeout变量,以便我们可以根据需要它的作业或阶段更改它
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;
}
}
}
}
}
只要记住将节点放在闭包中,这样等待输入就不会阻塞执行器
如果你有付费的jenkins enterprise Cloudbees,它有一个检查点插件,可以更好地处理这个问题,但它不打算为开源jenkins()发布。这个插件需要一个很好的增量等待
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'
}
}
是否要添加重试提示?我怀疑。哦,不,你是对的。我会更新我的答案!是否可以仅为重试部分启用超时?我可能希望有一个不同的超时工作。我还没有接受这个答案,因为我没有找到一个好的解决方案。理想情况下,重试选项应该在作业完成之后。假设此作业是由PR上的GitHub钩子触发的。我更希望看到GitHub上的失败,而不是在出现错误时没有答案。在使用WaitUtil{}和构建管道步骤进行测试时,我发现我需要显式返回true,因为该步骤不返回布尔类型。不再按发布方式工作,出现错误:未知阶段部分“Waitill”。从版本0.5开始,阶段中的步骤必须位于“步骤”块中。此总体功能请求位于。它(令人失望地)仅为中的声明性管道选择。
sleep()
已经,因此除非您希望第一次等待超过一小时,否则请指定睡眠(…,单位:“毫秒”)
或使用更少的秒数。我认为您不能再将重试
放在阶段
块的顶部:预期的“步骤”之一,第423行第17列的“阶段”或“并行”阶段“代码覆盖率”。
只有当我将其放在步骤之后时,它才对我有效。正如其他答案所示,通常最好利用Jenkins build SDL设施
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' } }