詹金斯:每天只发送一次成功电子邮件(虽然工作正在运行@hourly)

时间:2013-03-02 10:34:06

标签: jenkins jenkins-plugins email-ext

我将jenkins工作配置为每小时运行一次。我希望成功构建邮件每天只能作为电子邮件发送一次。 Email-Ext让我可以选择发送所有成功,失败等电子邮件。但我想要的是只能发送一次成功电子邮件的能力。

3 个答案:

答案 0 :(得分:2)

好吧,没有插件可以为你做到这一点。 Jenkins中的默认电子邮件功能非常简单,工作正常。虽然有Email-ext plugin,但这个可以为你做更多的事情。

首先,使用Email-ext,您可以配置特定的触发器来发送电子邮件通知 - 它可以是成功失败,这类似于Jenkins的默认行为。但是你有更精致的一个,比如第一次失败仍然失败。这将使您对Jenkins将发送电子邮件的时间和对象(收件人列表,提交者或请求者)有很大的控制权。在我的情况下,这里的良好配置将有助于Jenkins生成的电子邮件流量。您可以将特定情况下的特定电子邮件发送给特定的人员列表 - 太棒了!

另一个选项是,如果您真的不需要这种级别的控制,并且只想将电子邮件流量限制为每天一个摘要,那就是设置邮件列表。大多数邮件列表引擎都允许您将所有电子邮件流量的每日摘要发送到列表中。这应该足够了,虽然我觉得从长远来看它实际上并不是一个好选择。我肯定会尝试使用Email-ext插件。

答案 1 :(得分:1)

这是一个老问题,您可能已经找到了自己的解决方法,但我有类似的需求,我认为无论如何我都要分享我的解决方案。我试图做的是生成一个每天一次的失败状态的工作摘要电子邮件。这基本上非常类似于为单个工作发送每日一次的成功报告。

我的解决方案使用Groovy构建步骤以及Email-Ext插件的预发送脚本功能。我从上面评论中引用的Nabble thread中得到了这个想法。另请参阅Jenkins网站上的Email-Ext Recipes

这是初始groovy脚本,用于确定哪些构建失败,在执行系统Groovy脚本下配置。您可以执行类似的操作来确定构建是成功还是失败:

// List the names of jobs you want to ignore for this check
ignore = [ ]

// Find all failed and unstable jobs
failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job ->
  job.getDisplayName() != "Daily Jenkins Job Nag" &&
  !ignore.contains(job.getDisplayName()) &&
  job.isBuildable() &&
  job.lastCompletedBuild &&
  (job.lastCompletedBuild.result == hudson.model.Result.FAILURE ||
   job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE)
}

// Log the job names so the build results are legible
failed.each { job ->
  println(job.getDisplayName() +
          " " + job.lastCompletedBuild.result +
          " at build " + job.lastCompletedBuild.number +
          " (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")");
}

// Return failure if there are any failed jobs
return failed.size 

然后,在可编辑电子邮件通知部分中,我设置了Email-Ext插件,以便在失败时通知。我将内容类型设置为纯文本(文本/纯文本),将默认内容设置为空,并将以下内容设置为预发送脚本

failed = hudson.model.Hudson.instance.getView("All").items.findAll{ job ->
  job.getDisplayName() != "Daily Jenkins Job Nag" &&
  job.isBuildable() &&
  job.lastCompletedBuild &&
  (job.lastCompletedBuild.result == hudson.model.Result.FAILURE ||
   job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE)
}

def output = StringBuilder.newInstance()

output << "<html>\n"
output << " <body>\n"
output << "<p>Jenkins reports the following failed jobs:</p>"
output << "  <ul>\n"

failed.each { job ->
  url = hudson.model.Hudson.instance.rootUrl + job.url + "/" + job.lastCompletedBuild.number + "/"
  output << "   <li>"
  output << "<a href=\"" + url  + "\">" + job.displayName + "</a>"
  output << " " + job.lastCompletedBuild.result 
  output << " at build " + job.lastCompletedBuild.number
  output << " (" + job.lastCompletedBuild.timestamp.format("yyyy-MM-dd'T'HH:mm ZZZZ") + ")"
  output << "</li>\n"
}

output << "  </ul>\n"
output << " </body>\n"
output << "</html>"

msg.setContent(output.toString(), "text/html")

关键是您可以访问msg对象,即MimeMessage。您可以将MIME消息的内容设置为您想要的任何内容。

在这种情况下,我会生成一系列失败的工作,但在您的情况下,这将是您希望每日一次的成功报告收到的任何消息。根据您的需要,您可以让Email-Ext为每个构建发送结果,而不仅仅是针对失败的构建。

答案 2 :(得分:1)

如果自上次发送电子邮件以来时间不足,请如何抑制电子邮件?虽然不是所要求的,但是这样的预发送脚本可能因其简单性而值得考虑?

if (build.result != hudson.model.Result.SUCCESS) {
    cancel = true;
}
else {
  try {
    long minEmailGap = 1000 * 60 * 60 * 16; // 16 hours in milliseconds

    File file = new File("/TimestampForMyJob.txt");

    if (file.exists() == false) {
      file.createNewFile();
    }
    else {
      long currentTime = (new Date()).getTime();

      if (file.lastModified() + minEmailGap > currentTime) {
        cancel = true;
      }
      else {
        file.setLastModified(currentTime);
      }
    }
  }
  catch(IOException e) {
    // We can't tell whether the e-mail should be sent out or not, so we do nothing
    // and it just gets sent anyway - probably the best we can do with this exception.
  }
}