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

groovy date arithmetics do not work in pipeline

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • workflow-cps-plugin
    • None
    • Jenkins 2.7.4
      Pipeline suite plugin 2.4

      Hi,

      The following date/time arithemetics which work in groovy - do not work in a pipeline script:

      import groovy.time.*
      def startDate = new Date()
      def timeout = startDate  + 1.hours //increment 1 hour
      

      use( TimeCategory ) doesn't help.

      I get the following error:
      groovy.lang.MissingPropertyException: No such property: hours for class: java.lang.Integer

      I also wasn't able to find any workaround for this - I was only able to increment integers (as in days)

          [JENKINS-40154] groovy date arithmetics do not work in pipeline

          Sergey Galkin added a comment -

          The same picture almost
          The code 

          import groovy.time.TimeCategory
          now = new Date()
          use(TimeCategory) {
              ydate = now - 1.days
          }

          is working fine but in one perfect day stop working with error

          Started by timer
          [Pipeline] End of Pipeline
          groovy.lang.MissingPropertyException: No such property: days for class: java.lang.Integer
          Possible solutions: class
          	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
          	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
          	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
          	at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
          	at WorkflowScript.run(WorkflowScript:4)
          	at WorkflowScript.run(WorkflowScript:3)
          	at ___cps.transform___(Native Method)
          	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
          	at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
          	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
          	at sun.reflect.GeneratedMethodAccessor1794.invoke(Unknown Source)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          	at java.lang.reflect.Method.invoke(Method.java:606)
          	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
          	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
          	at com.cloudbees.groovy.cps.Next.step(Next.java:58)
          	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
          	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
          	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224)
          	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
          	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
          	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(Executors.java:471)
          	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
          	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
          	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
          	at java.lang.Thread.run(Thread.java:745)
          Finished: FAILURE
          

          Sergey Galkin added a comment - The same picture almost The code  import groovy.time.TimeCategory now = new Date() use(TimeCategory) { ydate = now - 1.days } is working fine but in one perfect day stop working with error Started by timer [Pipeline] End of Pipeline groovy.lang.MissingPropertyException: No such property: days for class: java.lang. Integer Possible solutions: class at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458) at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:4) at WorkflowScript.run(WorkflowScript:3) at ___cps.transform___(Native Method) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74) at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66) at sun.reflect.GeneratedMethodAccessor1794.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) at com.cloudbees.groovy.cps.Next.step(Next.java:58) at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236) at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224) at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63) at java.util.concurrent.FutureTask.run(FutureTask.java:262) 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(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang. Thread .run( Thread .java:745) Finished: FAILURE

          Renzo Crisóstomo added a comment - - edited

          fyi: You can move this code to a Jenkins Pipeline shared library and add a @NonCPS decorator to it to avoid this kind of sandbox problems:

          @NonCPS
          def call(Date startDate, Date endDate) {
            use(groovy.time.TimeCategory) {
              def duration = endDate - startDate
              return duration.days
            }
          }
          

          Extending with Shared Libraries: https://jenkins.io/doc/book/pipeline/shared-libraries/

          Renzo Crisóstomo added a comment - - edited fyi: You can move this code to a Jenkins Pipeline shared library and add a @NonCPS decorator to it to avoid this kind of sandbox problems: @NonCPS def call(Date startDate, Date endDate) { use(groovy.time.TimeCategory) { def duration = endDate - startDate return duration.days } } Extending with Shared Libraries:  https://jenkins.io/doc/book/pipeline/shared-libraries/

            Unassigned Unassigned
            pyrocks Mor L
            Votes:
            8 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: