如何仅有条件地在 Jenkinsfile 的声明性管道中运行 -> 后失败元素?



>我有一个声明性管道,我想定义一个全局post->failure元素,该元素仅在某些分支上执行。它应该在所有阶段之后执行,以便捕获管道中的任何故障。我想使用导入的方法(例如来自其他时髦脚本(,即代替示例中所示的echo命令。

我的目标是创建一个仅在一组特定的分支上发生的通知,并且仅在管道中的任何阶段失败时才发生。

pipeline {
agent any
stages {
stage('Compile') {
steps {
...
}
}
/* ... other stages ... */
}
post {
success {
echo 'whole pipeline successful'
}
failure {
echo 'pipeline failed, at least one step failed'
}
}
}

post应该包含步骤,就像steps一样,但是步骤是命令 afaik(搜索了一个小时,没有找到定义(,这意味着我不能使用when或嵌套步骤(这可能吗 - 我也搜索了几个小时(。

我不能用 linter 来处理这个问题,因为它们都仅限于语法检查,并且不检查管道逻辑。我已经尝试了/pipeline-model-converter/validateURL,这很痛苦,因为它无法从网络 sureface 中使用,并且在邮递员中,您需要通过反复试验来接近 OAuth 身份验证。甚至没有用于验证的 Docker 映像。

您可以在post块中使用script块来执行 Groovy 语句。例如:

post {
failure {
script {
if (env.GIT_BRANCH == 'dev') {
echo 'pipeline failed'
// email...
}
}
}
}

最新更新