import hudson.model.*;
import hudson.AbortException;
import jenkins.model.Jenkins;
@NonCPS
def allNodes = jenkins.model.Jenkins.instance.nodes
def triggerJob = false
for (String node: allNodes) {
if ( node.getComputer().isOnline() && node.nodeName == "ABC" ) {
println node.nodeName + " " + node.getComputer().countBusy() + " " +
node.getComputer().isAcceptingTasks()
triggerJob = true
break
}
}
if (triggerJob) {
println("triggering build as node is available")
build job: 'jobName', parameters:
[
/** list of string Parameters **/
]
}
以上作业触发构建,但失败并显示:
发生的异常: 在现场hudson.model.Slave.launcher 原因:java.io.NotSerializableException:hudson.slaves.JNLPLauncher
答案 0 :(得分:3)
您只能使用在代码中实现Serializable
接口的类-Node
和Computer
都不能实现。
为了使用它们,您需要将各自的代码封装在@NonCPS
方法中,例如:
import hudson.model.*;
import hudson.AbortException;
import jenkins.model.Jenkins;
@NonCPS
def shallTrigger() {
def triggerJob = true
def allNodes = jenkins.model.Jenkins.instance.nodes
for (String node: allNodes) {
if ( node.getComputer().isOnline() && node.nodeName == "ABC" ) {
println node.nodeName + " " + node.getComputer().countBusy() + " " +
node.getComputer().isAcceptingTasks()
triggerJob = true
break
}
}
}
if (shallTrigger) {
println("triggering build as node is available")
build job: 'jobName', parameters:
[
/** list of string Parameters **/
]
}