使用Groovy设置Jenkins中的执行程序数

s4n*_*4nk 5 groovy jenkins jenkins-plugins

我正在尝试使用Groovy设置Jenkins中的执行程序数.我找到了一个方法hudson.model.Hudson.instance.setNumExecutors(int)但它似乎并没有真正起作用.问题是在运行Groovy脚本后修改后的值出现在配置面板中,但是我必须在该面板中单击"保存"才能真正更改它.

这是代码(作为Jenkins Groovy插件的构建步骤执行):

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()
Run Code Online (Sandbox Code Playgroud)

s4n*_*4nk 7

解决方案是在打电话hudson.setNodes(hudson.getNodes())后打电话setNumExecutors().