Jenkins 管道超时后重试 - 解决方法

Retry after timeout in Jenkins pipeline - workarounds

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

肯定有一些解决方法可以在超时发生后强制 retry(或替代)工作?

未触发 retry 的示例代码:

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

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

一个很好的解决方法,如 Basil Crow here 所建议的那样,是在 retrytimeout 之间插入 try - catch 以消耗超时错误 (FlowInterruptedException) 而未将其传递给 retry。一旦我们将 FlowInterruptedException 替换为自定义错误,retry 就会启动并开始正确协作,即使内部有 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

JENKINS-51454 has been fixed in jenkinsci/workflow-basic-steps-plugin#144 and released in 2.24 (Changelog).