Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change number of executors on existing Jenkins node by script

I wan't to able to script and change number of executors on a node(not master) that already exists. Preferably by using groovy but if there is a plugin or CLI command that could do the trick that is also interesting.

Snippet of what I am trying to do:

jenkins.model.Jenkins.instance.nodes.each { node ->
  println node.getNumExecutors()

  //How do I set the number of executors for a node?
}
like image 857
ki_ Avatar asked Sep 25 '22 09:09

ki_


1 Answers

I managed it using Slave, which is a subclass of Node.

Below a part of a method I use for that, with target_label and target_executors as parameters

  def nodes = nodesByLabel(target_label)  // requires plugin "Pipeline Utility Steps" 

  def j = Jenkins.getInstanceOrNull();

  for (int i = 0; i < nodes.size(); ++i) {

      def aSlave = (Slave) j.getNode(nodes[i])  // here cast is needed

      aSlave.setNumExecutors(target_executors.toInteger())
      aSlave.save()
      println   aSlave.getDisplayName() + "-" +  aSlave.getNumExecutors()

  }

  j.reload()
like image 198
YaP Avatar answered Oct 11 '22 12:10

YaP