Skip to content

Instantly share code, notes, and snippets.

@matthiasbaldi
Created September 20, 2016 08:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthiasbaldi/789e2be23bcf64977bb516e98098ba0c to your computer and use it in GitHub Desktop.
Save matthiasbaldi/789e2be23bcf64977bb516e98098ba0c to your computer and use it in GitHub Desktop.
Update multiple Jenkins job configurations by specific XML property
// this script can be used to change a specific XML property of the Jenkins config.xml of a job
// to use the script, edit the variables and run it in the Jenkins Script Console
import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
// --> please set the variable value
// set a part of the job name to generate the list of jobs
def partOfJobNameToSearch = ""
// XML property to change
def xmlProperty = "/buildWrappers/org.jvnet.hudson.plugins.m2release.M2ReleaseBuildWrapper/releaseGoals"
// Value you wanna add to the XML property above
def valueToAdd = '-Darguments="-Dmaven.javadoc.skip=true"'
// --> script - here you have nothing to edit
// load xml job configuration
def totCounter = 0
def okCounter = 0
def badCounter = 0
for (item in Jenkins.instance.items) {
if (item.name.toLowerCase().contains(partOfJobNameToSearch.toLowerCase())) {
def config = item.getConfigFile()
println("---------------------------------")
println("[INFO] loading xml: " + config)
File file = config.getFile()
String fileContents = file.getText('UTF-8')
def doc = DOMBuilder.parse(new StringReader(fileContents), false, false)
doc = doc.documentElement
// manipulate xml
use(DOMCategory) {
def properties = doc.xpath( "/maven2-moduleset" + xmlProperty, NODESET)
properties.each{ def property ->
totCounter++
// has the property already the correct value?
if (!property.text().contains(valueToAdd)) {
println("[INFO] edit property " + property.name())
property.value = property.text() + " " + valueToAdd
println("[INFO] new property value: " + property.text())
if (property.text().contains(valueToAdd)) {
println("[SUCCESS] property change successful")
okCounter++
} else {
println("[ERROR] property change not successful:")
println("--> " + item.name)
println("--> " + property.text())
badCounter++
}
} else {
println("[INFO] no change required: " + item.name)
}
}
}
// save file to filesystem
println "[INFO] save changes in config.xml"
file.withWriter { w ->
w.write(XmlUtil.serialize(doc))
}
// reload jenkins job config file
InputStream is = new FileInputStream(file);
item.updateByXml(new StreamSource(is));
item.save();
}
}
println("---------------------------------")
println(totCounter + " total items")
println(okCounter + " items with status successful :)")
println(badCounter + " items with status failure :(")
println((totCounter - badCounter - okCounter) + " items with no change")
@jayakumaria
Copy link

How to delete a property?

@matthiasbaldi
Copy link
Author

How to delete a property?

Hmmm, I am not sure.
I think you can do it by the XmlParser (or XmlSlurper): https://www.baeldung.com/groovy-xml#5-deleting-a-node
Give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment