1

I'm trying to run this pipeline script

#!groovy @NonCPS def findCommand(filePath) { def file = new File(filePath) def text = file.getText() def components = new XmlSlurper().parseText( text ) def map = new HashMap<>() components.component.each { def component-> map << ["${component.@application}" : [:]] def componentMap = map.get("${component.@application}") component.environments.environment.each { def environment-> componentMap << ["${environment.@name}" : [:]] def actionMap = componentMap.get("${environment.@name}") environment.actions.action.each { def action-> actionMap << ["${action.@toDo}" : action] } } } components = null text = null file = null return map } pipeline { agent any stages { stage('Build') { steps { echo 'Building..' echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}" echo "Parameters: ${params.environment} ${params.actions} ${params.applications}" script{ try { map = findCommand("XXX.xml") } catch (Exception e) { echo "Catching the NonCPS Exception"; } echo "${map}" } build job: "sample echo", parameters: [ [$class: 'StringParameterValue', name: 'application', value: "${params.application}"], [$class: 'StringParameterValue', name: 'environment', value: "${params.environment}"] ] } } } post { failure { echo "Build failed..." } } } 

I know I'm using XmlSlurper, but I've also specified the code-block to be @NonCPS. However, this is still throwing a java.IO.NonSerializableException.

I also tried using a try-catch block to catch the exception, but that didn't seem to work either. I'd gladly appreciate some help in how I could make the build completely cleanly. Thanks :(

1 Answer 1

2

The answer was in the exception. It's quite weird, but changing

components.component.each { def component-> map << ["${component.@application}" : [:]] def componentMap = map.get("${component.@application}") component.environments.environment.each { def environment-> componentMap << ["${environment.@name}" : [:]] def actionMap = componentMap.get("${environment.@name}") environment.actions.action.each { def action-> actionMap << ["${action.@toDo}" : action] } } } 

to

components.component.each { def component-> def application = "${component.@application}".toString() map << [ (application) : [:]] def componentMap = map.get(application) component.environments.environment.each { def environment-> def name = "${environment.@name}".toString() componentMap << [ (name) : [:]] def actionMap = componentMap.get(name) environment.actions.action.each { def action-> def toDo = "${action.@toDo}".toString() def actionString = action.toString() actionMap << [ (toDo) : (actionString)] } } } 

solved the issue. It had to do with the (non serializable) XMLSlurper data structure type still being carried into my HashMap.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. this helped me. I was adding items of type parser.parseText(xml) to a list and experienced something similar. Now, I'm doing a toString() and it's working fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.