【问题标题】:Quickly testing Jelly templates in email-ext of Jenkins在 Jenkins 的 email-ext 中快速测试 Jelly 模板
【发布时间】:2024-04-28 14:55:02
【问题描述】:

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

【问题讨论】:

    标签: jenkins jenkins-plugins jelly email-ext


    【解决方案1】:

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

    输入以下代码并将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

    【讨论】:

    • 这个答案几乎没有帮助。仅链接到错误跟踪器根本无法回答问题。
    • @Richard 我使用几秒钟前刚刚运行的脚本更新了答案。希望这对您来说现在是满意的。
    【解决方案2】:

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

    【讨论】:

      最近更新 更多