Identify when retry happened in Jenkins pipeline

Viewed 103

I have implemented retries around some code in Jenkins pipeline.

  retry(2) {
    // code
  }

Is there a way to identify when a retry has happened? Besides just manually checking console logs. Would like to identify builds which have retried some flakey code and send notification.

1 Answers

I don't know of a way to know that inside the retry{ } block.

You could try a fairly generic solution (even maybe as a global var in a shared pipeline library), such as using a try catch block (since the retry will wait for an exception):

int retryAttempts = 0
retry(2) {
  try {
    if (retryAttempts>0) {
      // a retry is occurring
      // Do pre-retry logic, if needed
      ...
    }
    // Do stuff
    .....
  } catch (e) {
    retryAttempts++ // a retry WILL occur
    throw e // rethrow to trigger retry
  }
}
if (retryAttempts>0) {
  // a retry has occurred
  // Do post-retry logic, if needed
  ...
}
Related