2

My jenkins pipeline content is :

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                script {
                    repos = [
                        'a': '1',
                        'b': '2',
                        'c': '3']
                    for (i in repos) {
                        echo "${i.key}, ${i.value}"
                        sh "echo test"
                    }
                }
            }
        }
    }
}

And I got an error on build :

Caused: java.io.NotSerializableException: java.util.LinkedHashMap$Entry
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1793)
    at java.util.HashMap.writeObject(HashMap.java:1363)
    at sun.reflect.GeneratedMethodAccessor176.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:156)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:191)
...
Finished: FAILURE

If repos is list, run ok.

I can't fix the error, Hopefully someone can answer that.

Thank you for your answer.

just-Luka
  • 377
  • 1
  • 6
  • 19
jugggao
  • 43
  • 1
  • 5

2 Answers2

3

try to replace for(i in repos){...} to repos.each{k,v-> ...}

repos.each{k,v->
    echo "${k}, ${v}"
    sh "echo test"
}

the problem with for(i in repos){...}:

This statement iterates through map entries. And each entry i has type of Map.Entry which is not serializable.

Pipeline tries to persist the status of all variables because each next pipenine command (echo, sh) could be theoretically executed on another jenkins node.

on other hand when using repos.each{k,v->...} you are iterating map with simple serializable values in your case.

daggett
  • 26,404
  • 3
  • 40
  • 56
  • I just started learning groovy, always looking for the same syntax as python: `for k,v in repos`. thank you for the anwer. – jugggao Mar 29 '20 at 13:59
  • It is not working in my case. Now I am getting `java.io.NotSerializableException: java.util.ArrayList$Itr` – Arun Aug 19 '20 at 15:49
  • this question was about exact case with HashMap objet. and not about ArrayList. better to ask own question. but before this - please read about `CPS` and `@NonCPS`: https://www.jenkins.io/doc/book/pipeline/cps-method-mismatches/#common-problems-and-solutions – daggett Aug 19 '20 at 16:33
-1

Please try more latest version of Jenkins.