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 :(