Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins timeout/abort exception

We have a Jenkins pipeline script that requests approval from the user after all the preparatory steps are complete, before it actually applies the changes.

We want to add a timeout to this step, so that if there is no input from the user then the build is aborted, and are currently working on using this kind of method:

    try {
      timeout(time: 30, unit: 'SECONDS') {
        userInput = input("Apply changes?")
      }
    } catch(err) {
      def user = err.getCauses()[0].getUser()

      if (user.toString == 'SYSTEM') {  // if it's system it's a timeout
        didTimeout = true
        echo "Build timed out at approval step"
      } else if (userInput == false) {  // if not and input is false it's the user
        echo "Build aborted by: [${user}]"
      }
    }

This code is based on examples found here: https://support.cloudbees.com/hc/en-us/articles/226554067-Pipeline-How-to-add-an-input-step-with-timeout-that-continues-if-timeout-is-reached-using-a-default-value and other places online, but I really dislike catching all errors then working out what's caused the exception using err.getCauses()[0].getUser(). I'd rather explicitly catch(TimeoutException) or something like that.

So my question is, what are the actual exceptions that would be thrown by either the approval step timing out or the userInput being false? I haven't been able to find anything in the docs or Jenkins codebase so far about this.

like image 735
Robin Avatar asked Jul 10 '18 08:07

Robin


People also ask

How do I set Jenkins timeout?

Go to Manage Jenkins and then Configure System . Under the item Global Build Time Out you can activate a global timeout which will be applied to any job. Choose your timeout strategy, the duration and add actions which should be executed at timeout.

What is timeout in Jenkins pipeline?

The declarative Jenkins Pipeline allows us to define timeout either at the pipeline level or the specific stage. This feature prevents Jenkins's job from getting stuck. However, in some cases, we want to accept that one stage may timeout, but we want to keep the remaining stages running.

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)


1 Answers

The exception class they are referring to is org.jenkinsci.plugins.workflow.steps.FlowInterruptedException.

Cannot believe that this is an example provided by CloudBeeds.

Most (or probably all?) other exceptions won't even have the getCauses() method which of course would throw another exception then from within the catch block.

Furthermore as you already mentioned it is not a good idea to just catch all exceptions.

Edit: By the way: Scrolling further down that post - in the comments - there you'll find an example catching a FlowInterruptedException.

like image 128
Joerg S Avatar answered Oct 26 '22 14:10

Joerg S