user797963
user797963

Reputation: 3037

Jenkins Pipeline - can't use ConfigSlurper because ConfigObject is not serializable?

I'm trying to use groovy ConfigSlurper to load an external config file that's related to a build. I can load the file, but if I try to access it in a pipeline stage, Jenkins dies with a "java.io.NotSerializableException: groovy.util.ConfigObject", but util.ConfigObject is serializable? What gives?

Is there another way to access config files in a way that can return objects so I can access config items like: buildConfig.scm.someUser?

Upvotes: 1

Views: 1650

Answers (2)

BobMcGee
BobMcGee

Reputation: 20120

Please do not use ConfigSlurper - the way it is implemented in Groovy means it will cause a serious memory leak in Pipeline. Unfortunately it's not well-documented (something I need to address today), but this is noted in the source code and gets mentioned regularly in tips.

Instead, prefer the Pipeline Utility Steps - readYaml, readProperties, readJson, etc -- these are safe constructs and will probably perform a bit better too.

Upvotes: 1

daggett
daggett

Reputation: 28634

use json format as config

readJSON step to read it

and if you have this cfg.json file

{
  "scm":{
    "someUser":"myUser"
  }
}

then this code will be valid:

def buildConfig = readJSON file: 'cfg.json'
def user = buildConfig.scm.someUser
assert user == "myUser"

or you can use yaml format and readYaml step with yaml file like this:

---
scm:
  someUser: myUser

Upvotes: 2

Related Questions