如何取消系统groovy脚本

Chr*_*tze 5 groovy jenkins

在Jenkins中,我有一个系统groovy脚本构建步骤.有没有办法取消长时间运行的脚本,或者在系统groovy脚本中有一些代码可以判断它的构建是否已被取消?

das*_*ker 2

当您取消构建时,Jenkins 将中断正在运行脚本的线程。

因此,您可以手动检查线程的中断状态并在某些点中止脚本,例如

if (Thread.currentThread().interrupted()) {
  throw new InterruptedException()
}
Run Code Online (Sandbox Code Playgroud)

或者您可以通过在脚本中包含ThreadInterrupt注释来让 Groovy 为您完成此操作,例如

@groovy.transform.ThreadInterrupt
Run Code Online (Sandbox Code Playgroud)

还要确保您的 catch 块不会吞下 InterruptedException。