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)

          [JENKINS-45183] Issues with closures

          Andrew Bayer added a comment -

          Can you please include your Jenkinsfile?

          Andrew Bayer added a comment - Can you please include your Jenkinsfile?

          Yes, please see attached.

          Brittany Berwanger added a comment - Yes, please see attached.

          Andrew Bayer added a comment -

          Huh. Interesting - something's going wrong in the compilation of the Groovy file. Could you try the following, just to determine whether the root cause of the error is specific to Declarative - in a new Pipeline job, use this as the script:

          @Library('jenkins-shared-library') _
          
          node {
            getNextAvailablePortNumber()
          }
          

          Andrew Bayer added a comment - Huh. Interesting - something's going wrong in the compilation of the Groovy file. Could you try the following, just to determine whether the root cause of the error is specific to Declarative - in a new Pipeline job, use this as the script: @Library( 'jenkins-shared-library' ) _ node { getNextAvailablePortNumber() }

          Andrew Bayer added a comment -

          Ah-ha - the obfuscatedness of the error is Declarative's fault, but the actual error isn't - I just did a test myself and got the following:

          .../libs/jenkins-shared-library/vars/getNextAvailablePortNumber.groovy: 13: unable to resolve class Pattern 
           @ line 13, column 9.
             Pattern pattern = Pattern.compile(/\:(.*?)\:/)
                     ^
          

          Adding import java.util.regex.Pattern at the top of getNextAvailablePortNumber.groovy fixed that.

          Andrew Bayer added a comment - Ah-ha - the obfuscatedness of the error is Declarative's fault, but the actual error isn't - I just did a test myself and got the following: .../libs/jenkins-shared-library/vars/getNextAvailablePortNumber.groovy: 13: unable to resolve class Pattern @ line 13, column 9. Pattern pattern = Pattern.compile(/\:(.*?)\:/) ^ Adding import java.util.regex.Pattern at the top of getNextAvailablePortNumber.groovy fixed that.

          Was just about to respond with that discovery myself! When I saw that error I added the import and everything works fine. Thank you for the help, I've only worked with the Declarative syntax so didn't think to test in classic. I will add that trick to future troubleshooting steps.

          Brittany Berwanger added a comment - Was just about to respond with that discovery myself! When I saw that error I added the import and everything works fine. Thank you for the help, I've only worked with the Declarative syntax so didn't think to test in classic. I will add that trick to future troubleshooting steps.

          Andrew Bayer added a comment -

          gleefulcoder Great! I'm going to leave this open for me to find a way to expose the compilation error better from in Declarative. =)

          Andrew Bayer added a comment - gleefulcoder Great! I'm going to leave this open for me to find a way to expose the compilation error better from in Declarative. =)

          Liam Newman added a comment -

          Bulk closing resolved issues.

          Liam Newman added a comment - Bulk closing resolved issues.

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

              Created:
              Updated:
              Resolved: