gpt4 book ai didi

jenkins - 如何在 Jenkinsfile 中捕获手动 UI 作业取消

转载 作者:行者123 更新时间:2023-12-02 21:25:47 25 4
gpt4 key购买 nike

我试图找到有关如何在 Jenkinsfile 管道中捕获用户在 jenkins Web UI 中取消作业时发生的错误的文档。

我还没有使用 posttry/catch/finally 方法,它们仅在构建中出现故障时才起作用。

这会导致当有人取消作业时资源不​​会被释放。

我今天拥有的是声明式管道中的脚本,如下所示:

pipeline {
stage("test") {
steps {
parallell (
unit: {
node("main-builder") {
script {
try { sh "<build stuff>" } catch (ex) { report } finally { cleanup }
}
}
}
)
}
}
}

因此,当从 UI 手动取消作业时,catch(ex)finally block 中的所有内容都会被忽略。

最佳答案

非声明式方法:

当您中止管道脚本构建时,会引发 org.jenkinsci.plugins.workflow.steps.FlowInterruptedException 类型的异常。释放catch block 中的资源并重新抛出异常。

import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException

def releaseResources() {
echo "Releasing resources"
sleep 10
}

node {
try {
echo "Doing steps..."
} catch (FlowInterruptedException interruptEx) {
releaseResources()
throw interruptEx
}
}

声明式方法(11/2019 更新):

根据 Jenkins 声明性管道文档,under post section :

cleanup

Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.

因此,无论管道是否中止,这都应该是释放资源的好地方。

def releaseResources() {
echo "Releasing resources"
sleep 10
}

pipeline {
agent none
stages {
stage("test") {
steps {
parallel (
unit: {
node("main-builder") {
script {
echo "Doing steps..."
sleep 20
}
}
}
)
}
post {
cleanup {
releaseResources()
}
}
}
}
}

关于jenkins - 如何在 Jenkinsfile 中捕获手动 UI 作业取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com