关于 jenkins 作业失败的通知 - 来自 scm 的管道

RaG*_*aGe 2 notifications jenkins jenkins-pipeline

我们有几个 jenkins 管道作业设置为“来自 scm 的管道”,它们从 github 检出 jenkins 文件并运行它。jenkinsfile 中有足够的基于 try/catch 的错误处理来捕获错误条件并通知正确的通道。这篇博文深入探讨了如何实现这一目标。

但是,如果首先在获取 jenkinsfile 时出现问题,该作业将无声无息地失败。如何在管道启动之前生成来自一般作业启动失败的通知?

Yog*_*esh 6

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'"
}
Run Code Online (Sandbox Code Playgroud)