Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jinlxz/3437d1335a6113ddecb240d35c181a08 to your computer and use it in GitHub Desktop.
Save jinlxz/3437d1335a6113ddecb240d35c181a08 to your computer and use it in GitHub Desktop.
a groovy script to test your email extension templates cf. https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+pluginto do that just * replace the projectName and p.recipientList values in the script below* run this script through http://localhost:8080/jenkins/script ** copy and paste it** click "run"
import hudson.model.StreamBuildListener
import hudson.plugins.emailext.ExtendedEmailPublisher
import java.io.ByteArrayOutputStream
def projectName = "your-project-name-here"
def oriProject=Jenkins.instance.getItem(projectName)
if(oriProject==null){
println "oriProject is null, exit."
return 1
}
Jenkins.instance.copy(Jenkins.instance.getItem(projectName), "$projectName-Testing");
def project = Jenkins.instance.getItem(projectName)
try {
def testing = Jenkins.instance.getItem("$projectName-Testing")
def build = project.lastBuild
// or def build = project.lastFailedBuild
// 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) {
// cleanup the test job
testing.delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment