Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly testing Jelly templates in email-ext of Jenkins

The Email-ext of Jenkins allows you to write a Jelly email template. How do you write and test one without triggering a build every time? Basically, I'm looking for a 1 second iteration where I can modify a Jelly script, hit refresh on a browser, and it will automatically render the template based upon a hard-code project and build result.

like image 362
Josh Unger Avatar asked Dec 09 '22 17:12

Josh Unger


1 Answers

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 [email protected] 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 = '[email protected]' // 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

like image 109
Josh Unger Avatar answered Dec 27 '22 12:12

Josh Unger