how to avoid cps error using XmlSlurper() method in jenkins pipeline

20 views
Skip to first unread message

jfern...@forgottenempires.net

unread,
Jan 13, 2021, 4:21:31 AM1/13/21
to Jenkins Users

I am using a simple test pipeline to get a username out of a xml file, I have written a simple function using the XmlSlurper method and I have included the @NonCPS annotation at the beginning of the function but I keep getting an error, even though the function seems to work as it prints out in the console the username I am trying to find but it finishes the build with failure status. This is the pipeline:

```

import groovy.xml.*

import jenkins.model.Jenkins



@NonCPS

def findComitter(){

committer = ""

found = false

file = new XmlSlurper().parse("C:/Users/User/.jenkins/jobs/Phoenix_pipeline/builds/288/changelog6429015253614530280.xml")

file.entry.each { entry ->

entry.changenumber.each { changenumber ->

changenumber.children().each { tag ->

if(tag.name() == "changeUser" && found != true){

committer = tag.text()

found = true

}

}

}

}

println committer

//return committer.toString()

}


pipeline {

agent any


stages {

stage('test') {

steps {

findComitter()

}

}

}

}


```


this is the output I get, as you can see the function call seems to work as it get to the ```println commiter``` step but then I get an error and failure build status:

```

00:00:00.021 [Pipeline] Start of Pipeline

00:00:00.121 [Pipeline] node

00:00:00.129 Running on Jenkins in C:\Users\User\.jenkins\workspace\test

00:00:00.140 [Pipeline] {

00:00:00.157 [Pipeline] stage

00:00:00.161 [Pipeline] { (Hello)

00:00:00.180 [Pipeline] echo

00:00:00.182 Hello World

00:00:00.202 [Pipeline] echo

00:00:00.204 jaydenm

00:00:00.207 [Pipeline] }

00:00:00.220 [Pipeline] // stage

00:00:00.230 [Pipeline] }

00:00:00.242 [Pipeline] // node

00:00:00.264 [Pipeline] End of Pipeline

00:00:00.293 an exception which occurred:

00:00:00.293 in field org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript.script

00:00:00.293 in object org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript@49af372

00:00:00.293 in field groovy.lang.Closure.delegate

00:00:00.293 in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@18d98e8e

00:00:00.293 in field groovy.lang.Closure.delegate

00:00:00.293 in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@43324839

00:00:00.293 in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures

00:00:00.293 in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@76d386ff

00:00:00.293 in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@76d386ff

00:00:00.293 Caused: java.io.NotSerializableException: groovy.util.slurpersupport.NodeChild

```


I have been looking through the SO threads and all the info I got is to use the @NonCPS annotation to use any non-serializable objects, which I have done.

Reinhold Füreder

unread,
Jan 13, 2021, 6:19:40 AM1/13/21
to jenkins...@googlegroups.com

Hi jfernandez,

 

Naïve guess(es):

  • Are you maybe missing this:

import com.cloudbees.groovy.cps.NonCPS

    • though not sure if the annotation would/should not then lead to an error instead already?
  • Or is this a declarative pipeline and you are missing the script { … } step based wrapping of calling your findCommitter() method (assuming that calling it when being declared in the declarative pipeline is even allowed like this)?

steps {

  script {

    findComitter()

  }

}

 

HTH Reinhold

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3764c601-8250-44f8-875a-ea0ec35321aan%40googlegroups.com.

jfern...@forgottenempires.net

unread,
Jan 13, 2021, 7:50:52 AM1/13/21
to Jenkins Users
Thanks for the answer. I found the issue which it was I was creating global objects within the findcommitter declaration, adding def before all the variables sorted the problelm
Reply all
Reply to author
Forward
0 new messages