我们有几个 jenkins 管道作业设置为“来自 scm 的管道”,它们从 github check out jenkins 文件并运行它。 jenkinsfile 中有足够的基于 try/catch 的错误处理来捕获错误条件并通知正确的 channel 。 This blog post深入探讨了如何实现这一目标。
但是,如果首先在获取 jenkinsfile 时出现问题,该作业将无声无息地失败。如何在管道启动之前生成来自一般作业启动失败的通知?
最佳答案
Jenkins SCM 管道没有任何类似于 catch/finally
的执行条款如果 Jenkinsfile
将被调用加载失败,我认为将来不会有。
然而有这个global-post-script它在 Jenkins 上的每个作业的每次构建之后运行 groovy 脚本。您必须将该脚本放在 $JENKINS_HOME/global-post-script/
中目录。
使用它,您可以根据失败的项目和/或失败的原因/异常向管理员发送通知或电子邮件。
可以放在脚本中的示例代码
if ("$BUILD_RESULT" != 'SUCCESS') {
def job = hudson.model.Hudson.instance.getItem("$JOB_NAME")
def build = job.getBuild("$BUILD_NUMBER")
def exceptionsToHandle = ["java.io.FileNotFoundException","hudson.plugins.git.GitException"]
def foundExection = build
.getLog()
.split('\n')
.toList()
.stream()
.filter{ line ->
!line.trim().isEmpty() && !exceptionsToHandle.stream().filter{ex -> line.contains(ex)}.collect().isEmpty()
}
.collect()
.size() > 0;
println "do something with '$foundExection'"
}
关于jenkins - 关于 jenkins 作业失败的通知 - 来自 scm 的管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53309317/