【问题标题】:Jenkins - Email notifications詹金斯 - 电子邮件通知
【发布时间】:2017-11-24 00:02:30
【问题描述】:

我正在尝试使用带有 email-ext 插件的 Groovy 自定义电子邮件。当我向这些电子邮件添加新功能时,我可能会在脚本中引入错误,从而收到包含 StackTrace 的错误邮件。因此,我希望能够发送有关已完成工作的通知,因为我的工作可能需要很多小时(目前超过 4 个)。
有没有办法让 jenkins 发送关于已完成作业的通知(使用 Groovy 或任何其他脚本语言)?

【问题讨论】:

  • Email-ext 插件允许您根据构建的结果使用不同的模板(带有果冻脚本)。您可以将插件配置为在构建失败、成功以及其他情况下发送不同的邮件...这不是您要找的吗?
  • 嗨,我已经配置了这些触发器,但我需要为链接到这些触发器的电子邮件添加新功能,因此我希望能够在已通过的作业上发送这些通知。
  • 您是指 [email-ext][1] 插件中的触发功能吗? [1]:stackoverflow.com/questions/8321649/…

标签: notifications jenkins


【解决方案1】:

找到解决办法:

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import groovy.text.Template
import groovy.text.SimpleTemplateEngine
import javax.mail.*
import javax.mail.internet.*

//-------------- Job params --------------
projectName="YourProjectName";
buildNum = 10;
templateName="groovy-html-cobertura.template";
recipients="someone@somewhere.com";
sender="jenkins@somewhere.com";
smtpHost="mysmtphost";
//------------End Job params -------------

for (hudson.model.AbstractProject p : hudson.model.Hudson.instance.projects) {
  if(p.name.equals(projectName)){
    for (hudson.model.AbstractBuild b : p.getBuilds() ) {
      if(b.getNumber() == buildNum){
        println b;
        b.reload();
        def binding = [ "build" : b, 
                        "project" : b.getProject(),
                        "rooturl" : hudson.model.Hudson.getInstance().getRootUrl(),
                        "it" : new     hudson.plugins.emailext.plugins.content.ScriptContentBuildWrapper(b),
                        "spc" : "  " ]
        def engine = new SimpleTemplateEngine()
        java.io.File file = new java.io.File(hudson.model.Hudson.getInstance   ().getRootPath().getBaseName()+"/email-templates/"+templateName);
        mailBody = engine.createTemplate(file.getText()).make(binding).toString();
        port = 25
        props = new Properties()
        props.put('mail.smtp.host', smtpHost)
        props.put('mail.smtp.port', port.toString())
        session = Session.getDefaultInstance(props, null)

        // Construct the message
        msg = new MimeMessage(session)
        msg.from = new InternetAddress(sender)
        msg.sentDate = new Date()
        msg.subject = 'Template Test'
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients))
        msg.setHeader('Organization', 'i-BP')
        msg.setContent(mailBody,
                   'text/html')
        // Send the message
        Transport.send(msg)
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多