Hudson / Jenkins Automation
I was looking for an easy way to script changing the email addresses on all of my hudson projects and didn’t find any solution on the interwebs. So, I though I would post mine for others to use.
Essentially, just go to the groovy console in hudson/jenkins (under Manage -> Script Console) and enter what ever groovy you like. The javadoc for the API can be found at: http://hudson-ci.org/javadoc/index.html
In my case, I needed to change the email address for every job’s notification settings. So, I used the following script:
import hudson.model.*
int count=0;
Hudson.instance.items.each{ item ->
item.each{ job ->
def configFile = new File(item.getConfigFile().toString())
def config = configFile.text
if(!config.indexOf("<recipients>")){
println "$job Not Updated"
}else{
def regex = "</recipients><recipients>[^< ]*</recipients>"
config = config.replaceAll(regex, "</recipients><recipients>somegroup@nowhere.com</recipients>")
configFile.write(config)
count++;
}
}
}
println "Updated $count Jobs"