Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry after timeout in Jenkins pipeline - workarounds

It's a known Jenkins issue that pipeline retry operation doesn't retry when there is a timeout inside of it.

Surely there are some workarounds to force retry (or substitute) to work after a timeout occurs?

Sample code where retry is not triggered:

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

Unless caught, the timeout error (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) causes the entire job to abort.

like image 829
mirekphd Avatar asked Jan 02 '20 10:01

mirekphd


2 Answers

A good workaround, as suggested by Basil Crow here is to insert try - catch between retry and timeout to consume the timeout error (FlowInterruptedException) without passing it to retry. As soon as we replace FlowInterruptedException with a custom error, retry kicks in and starts cooperating correctly even with timeout inside.

Example:

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

like image 51
mirekphd Avatar answered Sep 20 '22 12:09

mirekphd


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

like image 33
Basil Crow Avatar answered Sep 18 '22 12:09

Basil Crow