首页 文章

快速测试Jenkins的email-ext中的Jelly模板

提问于
浏览
4

Jenkins的Email-ext允许您编写Jelly电子邮件模板 . 如何在不触发构建的情况下每次编写和测试一个?基本上,我正在寻找1秒的迭代,我可以修改Jelly脚本,在浏览器上点击刷新,它将根据硬代码项目和构建结果自动呈现模板 .

2 回答

  • 7

    在_http:// server / script /打开Jenkins脚本控制台(当这是一个实际的URL时,Stackoverflow在保存编辑时遇到问题) .

    输入以下代码并将 your-project-name 替换为您的项目名称,并将 me@me.com 替换为您的电子邮件地址:

    import hudson.model.StreamBuildListener
    import hudson.plugins.emailext.ExtendedEmailPublisher
    import java.io.ByteArrayOutputStream
    
    def projectName = "your-project-name"
    def project = Jenkins.instance.getItem(projectName)
    
    try
    {
    
      def testing = Jenkins.instance.copy(project, "$projectName-Testing")
      def build = project.lastUnsuccessfulBuild
    // see the <a href="http://javadoc.jenkins-ci.org/hudson/model/Job.html#getLastBuild()" title="Job" target="_blank">javadoc for the Job class</a> for other ways to get builds
    
    def baos = new ByteArrayOutputStream()
    def listener = new StreamBuildListener(baos)
    
    testing.publishersList.each() { p ->
      println(p)
      if(p instanceof ExtendedEmailPublisher) {
        // modify the properties as necessary here
        p.recipientList = 'me@me.com' // set the recipient list while testing
    
        // run the publisher
        p.perform((AbstractBuild<?,?>)build, null, listener)
        // print out the build log from ExtendedEmailPublisher
        println(new String( baos.toByteArray(), "UTF-8" ))
      }
    }
    
    }
    finally
    {
        if (testing != null)
        {
            testing.delete()
        }
    }
    

    消息来源:https://earl-of-code.com/2013/02/prototyping-and-testing-groovy-email-templates/

    还有一个问题可以让这更容易:

    JENKINS-9594 - Should be able to send test e-mail based on previous build

  • 7

    现在有一个选项可以在更新版本的插件中针对构建测试模板 . 当你在工作的屏幕上时,左侧应该有一个链接,上面写着电子邮件模板测试 . 它将允许您选择要再次测试的构建,它将在那里呈现模板 .

相关问题