Some Jenkins Groovy magic
Date April 19th, 2016 Author Vitaly Agapov
— Groovy
This is a short post with some Groovy snippets which can be used in Jenkins to perform some fancy stuff. They are tested in the real life and have proven their worth. I believe this is not the final release of this article 'cuz I am going to update it with some new examples as soon as I prepare them.
Let's see them.
Rename Jenkins views right from the build job
This code when performed as "Execute system Groovy script" step can change the names of your Jenkins views. This is very useful if you'd like to have dynamically updated views.
import hudson.model.* def cons_view_name def cons_codename def dev_codename=build.envVars['Codename']; def dev_version=build.envVars['Dev_version']; def cons_version=build.envVars['Cons_version']; def Revision = build.buildVariableResolver.resolve("Revision") def project = 'MyProj' println "Hello from Groovy magic" Hudson.instance.getViews().each { if (it.name.startsWith("$project DEV")) { println "$project DEV view has name " + it.name cons_codename=it.name.split(' ')[3] println "cons_codename=$cons_codename" println "Renaming " + it.name + " view to $project DEV $dev_version $dev_codename" it.rename("$project DEV $dev_version $dev_codename") it.save() } if (it.name.startsWith("$project CONS")) { println "$project CONS view has name " + it.name cons_view_name=it.name } } println "Renaming $cons_view_name view to $project CONS $cons_version $cons_codename" Hudson.instance.getView("$cons_view_name").rename("$project CONS $cons_version $cons_codename") Hudson.instance.getView("$cons_view_name").save()
Show job parameter with the list of Git tags or branches
I know that there is such an option as Git parameter to allow user to choose some Git branch or tag. But I don't like this feature much. First, it works only after the initial job run and doesn't work after deleting the workspace. Second, it doesn't allow us to make sorting, filtering, seding or some other stuff with the list. But Groovy magic make do everything we want. Just use it the Dynamic Choice Parameter. Behold the simple example.
proc1 = ['/bin/bash', '-c', "/usr/bin/git ls-remote -t gitserver:myrepo.git"].execute() proc2 = ['/bin/bash', '-c', "awk '{print \$2}' | sed s%^refs/tags/%% | grep ^[0-9]*.[0-9]*-[0-9]*\$ | sort -V | tac"].execute() all = proc1 | proc2 def list = all.text.readLines()
This example will show the tags. Use git ls-remote -h to get the list of remote heads.
Drop builds from the queue
Sometimes it could be useful for a job to drop other build of this job from the queue. And of course this can be done automatically. Groovy.
println("Analysing Jenkins queue..."); jobName = Thread.currentThread().executable.toString().split()[0]; // jobName = "MYPROJECT"; println("Current Job name is: " + jobName); Integer queueLimit = 1; Integer queueCount = 0; for (item in hudson.model.Hudson.instance.getQueue().getItems()) { println (item.task.getName()); if ( item.task.getName().equals(jobName) ) { println("Job found with name " + jobName); queueCount = queueCount + 1 } } println ("Found " + queueCount + " jobs in queue"); if ( queueCount >= queueLimit ) { println("Queue Limit is excedeed. Killing all jobs in queue: " + jobName); for (item in hudson.model.Hudson.instance.getQueue().getItems()) { println (item.task.getName()); if ( item.task.getName().equals(jobName) ) { item.doCancelQueue() } } }
Abort current build
We could also need to cancel this job for some reason. Then just:
build.getExecutor().interrupt(Result.ABORTED)
Delete file from workspace
This could be useful for example if you have prepared the bulk property files for triggering some project multiple times with different parameters but at some point in your Groovy script you have decided to cancel some of them. Maybe you have found the jobs in the queue or for some other reason.
file = build.envVars['WORKSPACE'] + "/" + filename + ".bulk" println file targetFile = new File(file) targetFile.delete()
Get the parameters of queued job
I have shown how to get the list of the jobs in the queue. But what to do if you need to know if there is a job in the queue with some appropriate value of one parameter? For example, you may trigger some jobs with “BRANCH” parameters and you don’t want to trigger another one with the same branch. So we need to iterate over the queue and get the task parameters.
import hudson.model.* jobName = "Some_Job"; def branches = build.envVars['BRANCHES']; for (item in hudson.model.Hudson.instance.getQueue().getItems()) { if ( item.task.getName().equals(jobName) ) { def parameters = item.actions.find{ it instanceof ParametersAction }?.parameters // We can also iterate over the parameters //parameters.find{ it.name == 'BRANCH'}?.each { } def branch = parameters.find{ it.name == 'BRANCH'}?.value if (branches.contains(branch)) { println("Killing this job in queue") item.doCancelQueue() } } }
Tags: Groovy, Jenkins
Category:
Linux |
No comments »