4

I'm using below code to load a Groovy file and to pass parameter:

Pipeline Script in jenkins

@NonCPS 
def ld() { 
   def pck = load '/tmp/Provsioning/e.groovy'; 
   return pck.xmlParseData("${params.hos_nam}");
} 
node { 
   stage ('Deploying Packages'){ 
      def aby = ld(); 
   } 
}

where ${params.hos_nam} is a build parameter and the installpackage groovy follows as below

/tmp/Provsioning/e.groovy

public class ReadXMLFile {
   def xmlParseData(String g){
      installPackage(a,b,c);
      input 'proceed'
      aemRestart(b);
   }
   def installPackage(String a, String b,String c){
      //some code
   }
   def aemRestart(String a){
      //some code
   }
}

I'm not sure why the below error occurs:

an exception which occurred:
    in field val$body
    in field closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@67aecf21
    Caused: java.io.NotSerializableException:    org.codehaus.groovy.runtime.InvokerHelper$1
    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)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1785)
    at java.util.HashMap.writeObject(HashMap.java:1362)
    at sun.reflect.GeneratedMethodAccessor198.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    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.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:140)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:458)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:434)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:422)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:362)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:82)

Finished: FAILURE
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
robotTech
  • 83
  • 1
  • 8
  • Look up for a @noncps annotation in jenkins pipeline... any method with serialization have to outside of the pipeline block and be marked with that notation – OK999 Aug 17 '17 at 05:50
  • Can you give me an example on this , will be helpful – robotTech Aug 17 '17 at 06:50
  • Tried @NonCPS annotaion also but still gives the same error `public class ReadXMLFile{ public static userKnown="UserKnownHostsFile=/dev/null"; public static StrictHost='StrictHostKeyChecking=no'; @NonCPS public void xmlParseData(String host) { try { String hostName = host; File fXmlFile = new File("/tmp/Provsioning/install_config.xml");` – robotTech Aug 17 '17 at 08:23
  • you cannot load the groovy file that have the serializing component. The code block for `xmlParseData` should be in the pipeline outside of the pipeline block, with the @NonCPS annotation. For an example look at https://stackoverflow.com/questions/42295921/what-is-the-effect-on-noncps-in-a-jenkins-pipeline-script – OK999 Aug 17 '17 at 15:50
  • I have approved the script from the Script security – robotTech Aug 18 '17 at 08:38

1 Answers1

2

object that you hold in a variable between two steps of the pipeline must be Serializable.

class A{
  def f(){
    return [hello:'world']
  }
}

node{
  def a = new A()
  def b = a.f()
}

could throw NotSerializableException because class A non Serializable

to solve this put all the code that works with non serializable variables into @NonCPS annotated function:

class A{
  def f(){
    return [hello:'world'] //hashmap itself is serializable
  }
}

@NonCPS
def f1(){
  def a = new A()
  return a.f()
}

node{
  def b = f1()
}

PS: I did not check the code but just to provide you an example..

daggett
  • 26,404
  • 3
  • 40
  • 56
  • hi @daggett i tried to implement the same , but still throws the Same error . `@NonCPS def ld() { def pck = load '/tmp/Provsioning/installpackage.groovy'; pck.xmlParseData("${params.hos_nam}"); } node { stage ('Deploying Packages'){ def aby = ld(); } }` – robotTech Aug 18 '17 at 08:37
  • the return value of your `pck.xmlParseData("${params.hos_nam}")` call must be serializable. what datatype returned by this function? – daggett Aug 18 '17 at 08:41
  • it Returns nothing , i just just perform some steps defined in the function . – robotTech Aug 18 '17 at 08:49
  • now i have tried giving return type string return "Completed"; in xmlParseData function – robotTech Aug 18 '17 at 09:00
  • could you update your question with simplified example with string return... and append the new full stacktrace. – daggett Aug 18 '17 at 13:23
  • quite sure problem is in calling pipeline `input 'proceed'` step inside your class. @NonCPS means execute code block in `one shot` at one node. then you breaking execution in the middle with `input 'proceed'` and jenkins tries to serialize current step to continue later. and inside your `ReadXMLFile` class you have non-serializable fields... – daggett Aug 18 '17 at 15:44
  • Oh That's sad to hear , I want this to take user input if certain condition fails , you know any solution to achieve this ?? – robotTech Aug 20 '17 at 10:55
  • 1
    at the point of interruption you don't have to use non-serializable variables. – daggett Aug 21 '17 at 08:11