如何在Jenkinsfile中捕获手动UI取消作业 [英] How to catch manual UI cancel of job in Jenkinsfile
问题描述
我试图找到有关如何在Jenkinsfile管道中捕获用户在jenkins Web UI中取消作业时发生的错误的文档.
I've tried to find documentation about how in a Jenkinsfile pipeline catching the error that occurs when a user cancels a job in jenkins web UI.
我没有使用post
或try/catch/finally
方法,它们仅在构建中出现故障时才起作用.
I haven't got the post
or try/catch/finally
approaches to work, they only work when something fails within the build.
这导致有人取消工作时无法释放资源.
This causes resources not to be free'd up when someone cancels a job.
我今天拥有的是声明性管道中的脚本,如下所示:
What I have today, is a script within a declarative pipeline, like so:
pipeline {
stage("test") {
steps {
parallell (
unit: {
node("main-builder") {
script {
try { sh "<build stuff>" } catch (ex) { report } finally { cleanup }
}
}
}
)
}
}
}
因此,当从UI中手动取消作业时,将忽略catch(ex)
和finally
块中的所有内容.
So, everything within catch(ex)
and finally
blocks is ignored when a job is manually cancelled from the UI.
推荐答案
非声明性方法:
中止管道脚本生成时,将引发类型为org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
的异常.在catch
块中释放资源,然后重新引发异常.
When you abort pipeline script build, exception of type org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
is thrown. Release resources in catch
block and re-throw the exception.
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
def releaseResources() {
echo "Releasing resources"
sleep 10
}
node {
try {
echo "Doing steps..."
sleep 20
} catch (FlowInterruptedException interruptEx) {
releaseResources()
throw interruptEx
}
}
声明性方法:
相同,但在stage
的steps
中的script {}
块内.不是最巧妙的解决方案,而是我已经测试并可以正常工作的解决方案.
The same, but within a script {}
block in the steps
of the stage
. Not the neatest solution but the one that I've tested and got working.
这篇关于如何在Jenkinsfile中捕获手动UI取消作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!