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

On Wednesday, January 13, 2021 at 12:19:40 PM UTC+1 r.fuereder wrote:

> 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
>
>  
>
>  
>
> *From:* [email protected] <[email protected]> *On 
> Behalf Of *[email protected]
> *Sent:* Mittwoch, 13. Jänner 2021 10:22
> *To:* Jenkins Users <[email protected]>
> *Subject:* how to avoid cps error using XmlSlurper() method in jenkins 
> pipeline
>
>  
>
> 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.
>
> -- 
> 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 [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/jenkinsci-users/3764c601-8250-44f8-875a-ea0ec35321aan%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/jenkinsci-users/3764c601-8250-44f8-875a-ea0ec35321aan%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/c9e4df8a-e785-415f-a104-077dc64290fdn%40googlegroups.com.

Reply via email to