当我在Jenkins工作流程(Jenkins 1.609.1,工作流程1.8)上运行以下代码时,出现“ NotSerializableException”错误(同样在下面)。但是,如果我将“构建作业”移到“ for”范围之外,则可以正常工作(该作业已激活)。任何想法为什么这种行为?
node('master') { ws('/opt/test) { def file = "/ot.property" def line = readFile (file) def resultList = line.tokenize() for(item in resultList ) { build job: 'testjob_1' } } }
得到错误:
Running: End of Workflow java.io.NotSerializableException: java.util.ArrayList$Itr at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) .....
我thnk这是因为它试图序列化不可序列化item的迭代器resultList只要它击中了build job一步。有关使用不可序列化变量的指导,请参见此处:
item
resultList
build job
https://github.com/jenkinsci/workflow- plugin/blob/master/TUTORIAL.md#serialization-of-local- variables
作为使用工作流插件进行安全迭代的一种解决方法,您需要使用C风格的循环。尝试以下方法:
for ( int i = 0; i < resultList.size; i++ ) { etc...