Friday, September 2, 2011

Jenkins script for mass-update of email recipients


This is a handy Jenkins Groovy script that can be used to update all the mail recipients for your Maven projects.  If you have a ton of projects, this can be way-faster than changing them each by hand.  I'm putting this here for the Googling and my own notes.

import hudson.plugins.emailext.*
import hudson.model.*
import hudson.maven.*
import hudson.maven.reporters.*
import hudson.tasks.*


// For each project
for(item in Hudson.instance.items) {
  println("JOB : "+item.name);
  for( reporter in item.reporters) {
    if(reporter instanceof MavenMailer) {
      reporter.recipients = "new@email"
    }
  }
}

1 comment:

Josh Kehoe said...

Thanks for putting me on the right path! Your version didn't work for me... probably because I'm on a newer version (v1.481). Using your post and an example script from Jenkins, I came up with the following:
--------


import hudson.plugins.emailext.*
import hudson.model.*
import hudson.maven.*
import hudson.maven.reporters.*
import hudson.tasks.*

// For each project
for(item in Hudson.instance.items) {
println("JOB : "+item.name);
// Find current recipients defined in project
if(!(item instanceof ExternalJob)) {
if(item instanceof MavenModuleSet) {
println(">MAVEN MODULE SET");
// Search for Maven Mailer Reporter
println(">>Reporters");
for(reporter in item.reporters) {
if(reporter instanceof MavenMailer) {
println(">>> reporter : "+reporter+" : "+reporter.recipients);
reporter.recipients = "DL-HELLO-Devs@jenkins.com DL-HelloDevTeam@jenkins.com"
}
}
} else
if(item instanceof FreeStyleProject) {
println(">FREESTYLE PROJECT");
}
println(">>Publishers");
for(publisher in item.publishersList) {
// Search for default Mailer Publisher (doesn't exist for Maven projects)
if(publisher instanceof Mailer) {
println(">>> publisher : "+publisher+" : "+publisher.recipients);
publisher.recipients = "DL-HELLO-Devs@jenkins.com DL-HelloDevTeam@jenkins.com"
} else
// Or for Extended Email Publisher
if(publisher instanceof ExtendedEmailPublisher) {
println(">>> publisher : "+publisher+" : "+publisher.recipientList);
publisher.recipientList = "DL-HELLO-Devs@jenkins.com DL-HelloDevTeam@jenkins.com"
}
}
} else {
println("External Jobs cannot have MailNotificationsRecipients")
}
println("\n=======\n");
}