Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-45183

Issues with closures

XMLWordPrintable

      This all started with a quest to figure out if I could modify variables declared in the environment node programatically. I've attempted numerous ways of doing so, the last of which was declaring functions in the vars section of my shared code library (getNextAvailablePortNumber.groovy) as I noticed they were available as global variables in the pipeline. I've pared down the code to what you see below to try to get to the root of the problem and it will not run.

       

      def call() {
      def portsInUseOnServer = {
      def cmd = """Invoke-Command `
      -ComputerName ${SERVER} `
      -ScriptBlock { `
      Import-Module WebAdministration; `
      Get-WebBinding | Select bindingInformation `
      }"""
      
      def output = powershell([script: cmd, returnStdout: true])
      def lines = output.split(/\n/).drop(3).dropRight(2)
      
      Pattern pattern = Pattern.compile(/\:(.*?)\:/)
      return lines.collect { pattern.matcher(it)[0][1].toInteger() }
      }
      portsInUseOnServer().each { println it }
      }

       

      With the exception of output being defined differently, this code works perfectly in my local groovy dev environment. The code also works exactly as-is in Jenkins when defined in a standard method in a class in the src directory of my shared code library. However, when calling this code from my Jenkinsfile the build fails with the following output (some lines removed due to description length limit):

       

      an exception which occurred:
      	in field collector
      	in field abnormal
      	in field outcome
      	in field body
      	in field step
      	in field thread
      	in field this$0
      	in field returnAddress
      	in field parent
      	in field caller
      ...
      ...
      ...
      	in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@441c1b
      Caused: 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.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.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.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      	at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      ...
      ...
      ...
      	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(Unknown Source)
      	at java.util.HashMap.internalWriteEntries(Unknown Source)
      	at java.util.HashMap.writeObject(Unknown Source)
      	at sun.reflect.GeneratedMethodAccessor203.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      	at java.lang.reflect.Method.invoke(Unknown Source)
      	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)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:242)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:230)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      	at java.util.concurrent.FutureTask.run(Unknown Source)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      	at java.util.concurrent.FutureTask.run(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      	at java.lang.Thread.run(Unknown Source)

            abayer Andrew Bayer
            gleefulcoder Brittany Berwanger
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: