Question

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()
Was it helpful?

Solution

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

OTHER TIPS

If everything is working as you expect except you have to press the save button manually to make the changes stick then it sounds like you didn't call save on your Jenkins instance after you made your changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top