【问题标题】:Retry after timeout in Jenkins pipeline - workarounds在 Jenkins 管道中超时后重试 - 解决方法
【发布时间】:2020-01-02 10:26:07
【问题描述】:

pipeline retry operation doesn't retry when there is a timeout inside of it 是一个已知的 Jenkins 问题。

确实有一些变通方法可以强制retry(或替代品)在超时发生后工作吗?

未触发retry 的示例代码:

retry(3) {
  timeout(time: 5, unit: 'MINUTES') {
    // Something that can fail
  }
}

除非被捕获,否则超时错误 (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) 会导致整个作业中止。

【问题讨论】:

  • 您是在编写脚本管道还是声明式管道?
  • 技术上是声明性的,但在每个阶段都插入了script 子句,因此实际上两者都可以使用。这个script { retry { timeout 的目的是在几分钟后中断docker push 阶段,该阶段有时会挂起数小时。
  • id 查看为什么它会挂几个小时而不是解决它。创建技术部门并不好玩:)
  • 我做到了,但这似乎与 docker 本身缺乏超时有关,这超出了我的控制范围,参见例如others having the same problem 与码头工人。
  • 你可以创建一个函数来围绕你的 docker push 和我们一个内部计数器来计数到 5 分钟,如果 docker 没有正确退出再试一次。

标签: jenkins timeout jenkins-pipeline


【解决方案1】:

正如Basil Crow here 建议的那样,一个好的解决方法是在retryretrytimeout 之间插入try - catch 以消耗超时错误(FlowInterruptedException)而不通过它到retry。一旦我们用自定义错误替换 FlowInterruptedExceptionretry 就会启动并开始正确合作,即使里面有 timeout

例子:

import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException

retry(3) {

  try {

      timeout(time: 5, unit: 'MINUTES') {

        // something that can fail

      } // timeout ends

  } catch (FlowInterruptedException e) {
      // we re-throw as a different error, that would not 
      // cause retry() to fail (workaround for issue JENKINS-51454)
      error 'Timeout!'

  } // try ends

} // retry ends

【讨论】:

  • 但您应该知道:如果用户在您等待超时时停止执行,也会创建一个FlowInterruptedException...
【解决方案2】:

【讨论】:

  • 非常感谢@basil-crow,好久不见了:)
猜你喜欢
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 2019-09-01
  • 2018-08-10
  • 2018-07-26
  • 1970-01-01
  • 2023-03-05
  • 2014-05-09
相关资源
最近更新 更多