我正在尝试创建一个在具有特定标签的所有在线节点上运行的简单作业。我可以获取节点名称,并期望下面的代码可以工作。 但是我得到以下例外。
def newJob(x)
{
return {
node(x) {
bat "dir"
}
}
}
def jobs = [:]
def lgroups = Jenkins.instance.getLabel('win7')
for (g in lgroups) {
for (node in g.getNodes()) {
def nodeName = node.getNodeName()
if (Jenkins.instance.getNode(nodeName).toComputer().isOnline())
{
jobs["${nodeName}_BS"] = newJob(nodeName)
}
}
}
parallel jobs
作业配置为在未启用沙箱的情况下运行。 例外:
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@90def78
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.LoopBlockScopeEnv@32864b7a
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@5a2e37ea
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@fb260f7
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.LoopBlockScopeEnv@fa692c0
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@15ffb7f2
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@60b6bff4
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@4e29d7b4
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@1fa0ac69
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@1efd7828
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@157768c5
in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
in object com.cloudbees.groovy.cps.impl.CpsClosureDef@6c20bc58
in field com.cloudbees.groovy.cps.impl.CpsClosure.def
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@757fe932
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@31a3450e
in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
in object com.cloudbees.groovy.cps.impl.CpsClosureDef@4d20e584
in field com.cloudbees.groovy.cps.impl.CpsClosure.def
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@59bab622
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5fa55074
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5fa55074
Caused: java.io.NotSerializableException: hudson.model.labels.LabelAtom
答案 0 :(得分:1)
您必须将不可序列化的代码提取到使用@NonCPS
注释的方法中,可能类似于以下内容:
@NonCPS
def getParallel() {
def jobs = [:]
def lgroups = Jenkins.instance.getLabel('win7')
for (g in lgroups) {
for (node in g.getNodes()) {
def nodeName = node.getNodeName()
if (Jenkins.instance.getNode(nodeName).toComputer().isOnline())
{
jobs["${nodeName}_BS"] = newJob(nodeName)
}
}
}
return jobs
}
parallel getParallel()