I'm trying to move duplicated methods in several pipeline scripts to a shared library. My first attempt, moving an enum type, worked fine. My next test is moving a method annotated with "@NonCPS". I noticed in the doc about writing shared libraries (
https://jenkins.io/doc/book/pipeline/shared-libraries/), it had the following statement:
The Groovy source files in these directories get the same “CPS transformation”
as in Scripted Pipeline.
I have no idea what that means, but I suppose it might have some relevant to my problem.
The method I'm trying to move is simply this:
@NonCPS
def computeCauseData() {
def result = ""
def causeActions = currentBuild.rawBuild.getAction(hudson.model.CauseAction)
for (action in causeActions) {
println "action[${action}] displayName[${action.displayName}]"
for (cause in action.causes) {
println "cause[${cause}] shortDescription[${cause.shortDescription}]"
result = "${cause.shortDescription}."
if (cause instanceof Cause.LegacyCodeCause) {
}
else if (cause instanceof TimerTrigger.TimerTriggerCause) {
}
else if (cause instanceof Cause.RemoteCause) {
Cause.RemoteCause remoteCause = (Cause.RemoteCause) cause
result = "${cause.shortDescription}: Addr[${remoteCause.addr}] Note[${remoteCause.note}]."
}
else if (cause instanceof Cause.UserIdCause) {
}
else if (cause instanceof SCMTrigger.SCMTriggerCause) {
SCMTrigger.SCMTriggerCause scmTriggerCause = (SCMTrigger.SCMTriggerCause) cause
println "scmTriggerCause[${scmTriggerCause}]"
}
else if (cause instanceof GitStatus.CommitHookCause) {
GitStatus.CommitHookCause commitHookCause = (GitStatus.CommitHookCause) cause
String sha1 = commitHookCause.sha1
println "sha1[${sha1}]"
}
else {
}
}
}
return result
}
This works fine in the pipeline script. When I moved it to my shared library (in "vars/computeCauseData.groovy") and commented out the definition in the pipeline script, and then ran my test, I got the following:
java.io.NotSerializableException: org.codehaus.groovy.control.ErrorCollector
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
In the past, I've avoid serialization errors by nulling out variables before they cross a scope. I tried modifying this method, simply nulling out everything but "result" right after their last use, and that made no difference. Same error.