Quickly testing Jelly templates in email-ext of Jenkins

断了今生、忘了曾经 提交于 2019-12-03 08:37:56

Open Jenkins script console at _http://server/script/ (Stackoverflow is having issues saving an edit when this is an actual URL).

Enter the following code and replace your-project-name with the name of your project and me@me.com with your email address:

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()
    }
}

SOURCE: https://earl-of-code.com/2013/02/prototyping-and-testing-groovy-email-templates/

There is also an issue that tracks making this easier:

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

There is now an option to test templates against builds in the more recent versions of the plugin. When you are on a job's screen, there should be a link on the left side that says Email Template Testing. It will let you select a build to test again and it will render the template right there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!