Jenkins Groovy auto installer

Jenkins is probably the most powerful CI software that is available today, and thanks to Job DSL and Pipeline plugins, you can even create jobs via Groovy. This might seem ironic, but if you wish to add some Groovy scripts to your jobs (for example, instead of shell command, execute some Groovy script), you will need to set-up a Groovy installation. It’s still possible to execute system Groovy scripts from Jenkins Jobs, however, it’s not always what we need. You can, of course, add a Groovy installation to Jenkins manually, but since one of the key things about being DevOps is trying to automate everything – even the most trivial of steps, it would be wise to do this via simple script.

The example Jenkins Groovy installation/configuration script is based on Maven installation script for Jenkins scriptler:

//Get Jenkins instance details
def inst = Jenkins.getInstance()  
def desc = inst.getDescriptor("hudson.plugins.groovy.Groovy")

// Set up properties for our new Groovy installation
def isp = new InstallSourceProperty()
def autoInstaller = new hudson.plugins.groovy.GroovyInstaller("2.4.7")
isp.installers.add(autoInstaller)
def proplist = new DescribableList<ToolProperty<?>, ToolPropertyDescriptor>()
proplist.add(isp)

// Define and add our Groovy installation to Jenkins
def groovyinst = new hudson.plugins.groovy.GroovyInstallation("Groovy", "", proplist)
desc.setInstallations(groovyinst)

// Output current Groovy installations, to verify that our's is now present
println desc.getInstallations()

The next question is how to install this automatically? In this sense, there are two ways – you can either modify it, and execute it via the help of Jenkins Scriptler plugin, see scriptler README.md, or, you can even construct a cURL request, for example:

curl -d 'script= \
	def inst = Jenkins.getInstance() \ 
	def desc = inst.getDescriptor("hudson.plugins.groovy.Groovy") \
	def isp = new InstallSourceProperty() \
	def autoInstaller = new hudson.plugins.groovy.GroovyInstaller("2.4.7") \
	isp.installers.add(autoInstaller) \
	def proplist = new DescribableList<ToolProperty<?>, ToolPropertyDescriptor>() \
	proplist.add(isp) \
	def groovyinst = new hudson.plugins.groovy.GroovyInstallation("Groovy", "", proplist) \
	desc.setInstallations(groovyinst) \
	println desc.getInstallations()' \
http://<JENKINS_URL>/script

Leave a Reply

Your email address will not be published. Required fields are marked *