简体   繁体   中英

Jenkins pipeline CPS issue with parallel jobs

I am trying to create a simple job that runs on all online nodes that have a certain label. I can get the node names and would expect the below code to work. However I get the below exception.

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

The job is configured to run without the sandbox enabled. Exception:

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

You have to extract the non-serializable code into a method annotated with @NonCPS , maybe similar to the following:

@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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM