Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap

后端 未结 12 783
长发绾君心
长发绾君心 2020-11-27 03:08

Solved: Thanks to below answer from S.Richmond. I needed to unset all stored maps of the groovy.json.internal.LazyMap type whi

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 03:34

    Use JsonSlurperClassic instead.

    Since Groovy 2.3 (note: Jenkins 2.7.1 uses Groovy 2.4.7) JsonSlurper returns LazyMap instead of HashMap. This makes new implementation of JsonSlurper not thread safe and not serializable. This makes it unusable outside of @NonDSL functions in pipeline DSL scripts.

    However you can fall-back to groovy.json.JsonSlurperClassic which supports old behavior and could be safely used within pipeline scripts.

    Example

    import groovy.json.JsonSlurperClassic 
    
    
    @NonCPS
    def jsonParse(def json) {
        new groovy.json.JsonSlurperClassic().parseText(json)
    }
    
    node('master') {
        def config =  jsonParse(readFile("config.json"))
    
        def db = config["database"]["address"]
        ...
    }    
    

    ps. You still will need to approve JsonSlurperClassic before it could be called.

提交回复
热议问题