Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting number of executors in Jenkins using Groovy

I'm trying to set the number of executors in Jenkins using Groovy. I've found a method hudson.model.Hudson.instance.setNumExecutors(int) but it doesn't seem to really work. The problem is that the modified value appears in the configuration panel after I run the Groovy script, but I have to click 'Save' in that panel to really have it changed.

Here's the code (executed as a build step with Jenkins Groovy plugin):

import hudson.model.*

// Initial number of executors is 1, let's increase the number of executors to 2
Hudson hudson = Hudson.getInstance()
hudson.setNumExecutors(2)
hudson.save()

def job = hudson.getJob("some_other_job")
def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))
subBuild = future.get()

// Set the number of executors back to 1
hudson.setNumExecutors(1)
hudson.save()
like image 428
s4nk Avatar asked Feb 24 '14 18:02

s4nk


1 Answers

The solution is to call hudson.setNodes(hudson.getNodes()) after calling setNumExecutors().

like image 91
s4nk Avatar answered Nov 15 '22 04:11

s4nk