Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one cancel a system groovy script

Tags:

jenkins

groovy

In Jenkins I have a system groovy script build step. Is there any way to cancel a long running script, or to have some code within the system groovy script which can tell whether its build has been cancelled?

like image 845
Christian Goetze Avatar asked Oct 31 '22 18:10

Christian Goetze


1 Answers

Jenkins will interrupt the thread which is running the script when you cancel the build.

So you can either manually check the thread's interrupt status and abort your script at certain points, e.g.

if (Thread.currentThread().interrupted()) {
  throw new InterruptedException()
}

Or you can let Groovy do that for you by including the ThreadInterrupt annotation in your script, e.g.

@groovy.transform.ThreadInterrupt

Also make sure that none of your catch blocks will swallow an InterruptedException.

like image 91
daspilker Avatar answered Nov 15 '22 05:11

daspilker