与jenkins脚本控制台相比,在尝试对列表进行排序时,在jenkins管道中得到了不同的结果。
詹金斯版本: 2.134
常规版本: 2.4.11
代码
def items = [[1, "09-Aug-2018 11:13" ],
[2, "11-Jul-2018 13:27" ],
[3, "02-Oct-2018 15:48" ],
[4, "03-Sep-2018 14:27" ],
[5, "08-Sep-2018 10:07" ],
[6, "11-Jul-2018 14:06" ],
[7, "08-Sep-2018 10:16" ],
[8, "09-Aug-2018 09:53" ],
[9, "16-Jul-2018 12:59" ],
[10, "08-Aug-2018 15:14" ],
[11, "12-Jul-2018 09:04" ],
[12, "08-Aug-2018 14:32" ],
[13, "08-Sep-2018 10:59" ],
[14, "08-Sep-2018 09:50" ],
[15, "16-Aug-2018 09:12" ],
[16, "19-Sep-2018 09:59" ],
[17, "18-Jul-2018 10:25" ],
[18, "11-Sep-2018 14:45" ],
[19, "12-Jul-2018 09:36" ],
[20, "02-Oct-2018 09:18" ]]
def itemsDateNotString = items.collect { [it[0], new Date().parse("dd-MMM-yyyy H:m", it[1])] }
println(itemsDateNotString)
def itemsSorted = itemsDateNotString.sort{ it[1] }
println(itemsSorted)
def itemsSortedReversed = itemsSorted.reverse()
println(itemsSortedReversed)
在笔记本电脑和Jenkins脚本控制台上的结果:
itemsDateNotString
[[1, Thu Aug 09 11:13:00 GMT 2018], [2, Wed Jul 11 13:27:00 GMT 2018], [3, Tue Oct 02 15:48:00 GMT 2018], [4, Mon Sep 03 14:27:00 GMT 2018], [5, Sat Sep 08 10:07:00 GMT 2018], [6, Wed Jul 11 14:06:00 GMT 2018], [7, Sat Sep 08 10:16:00 GMT 2018], [8, Thu Aug 09 09:53:00 GMT 2018], [9, Mon Jul 16 12:59:00 GMT 2018], [10, Wed Aug 08 15:14:00 GMT 2018], [11, Thu Jul 12 09:04:00 GMT 2018], [12, Wed Aug 08 14:32:00 GMT 2018], [13, Sat Sep 08 10:59:00 GMT 2018], [14, Sat Sep 08 09:50:00 GMT 2018], [15, Thu Aug 16 09:12:00 GMT 2018], [16, Wed Sep 19 09:59:00 GMT 2018], [17, Wed Jul 18 10:25:00 GMT 2018], [18, Tue Sep 11 14:45:00 GMT 2018], [19, Thu Jul 12 09:36:00 GMT 2018], [20, Tue Oct 02 09:18:00 GMT 2018]]
itemsSorted:
[[2, Wed Jul 11 13:27:00 GMT 2018], [6, Wed Jul 11 14:06:00 GMT 2018], [11, Thu Jul 12 09:04:00 GMT 2018], [19, Thu Jul 12 09:36:00 GMT 2018], [9, Mon Jul 16 12:59:00 GMT 2018], [17, Wed Jul 18 10:25:00 GMT 2018], [12, Wed Aug 08 14:32:00 GMT 2018], [10, Wed Aug 08 15:14:00 GMT 2018], [8, Thu Aug 09 09:53:00 GMT 2018], [1, Thu Aug 09 11:13:00 GMT 2018], [15, Thu Aug 16 09:12:00 GMT 2018], [4, Mon Sep 03 14:27:00 GMT 2018], [14, Sat Sep 08 09:50:00 GMT 2018], [5, Sat Sep 08 10:07:00 GMT 2018], [7, Sat Sep 08 10:16:00 GMT 2018], [13, Sat Sep 08 10:59:00 GMT 2018], [18, Tue Sep 11 14:45:00 GMT 2018], [16, Wed Sep 19 09:59:00 GMT 2018], [20, Tue Oct 02 09:18:00 GMT 2018], [3, Tue Oct 02 15:48:00 GMT 2018]]
itemsSortedReversed:
[[3, Tue Oct 02 15:48:00 GMT 2018], [20, Tue Oct 02 09:18:00 GMT 2018], [16, Wed Sep 19 09:59:00 GMT 2018], [18, Tue Sep 11 14:45:00 GMT 2018], [13, Sat Sep 08 10:59:00 GMT 2018], [7, Sat Sep 08 10:16:00 GMT 2018], [5, Sat Sep 08 10:07:00 GMT 2018], [14, Sat Sep 08 09:50:00 GMT 2018], [4, Mon Sep 03 14:27:00 GMT 2018], [15, Thu Aug 16 09:12:00 GMT 2018], [1, Thu Aug 09 11:13:00 GMT 2018], [8, Thu Aug 09 09:53:00 GMT 2018], [10, Wed Aug 08 15:14:00 GMT 2018], [12, Wed Aug 08 14:32:00 GMT 2018], [17, Wed Jul 18 10:25:00 GMT 2018], [9, Mon Jul 16 12:59:00 GMT 2018], [19, Thu Jul 12 09:36:00 GMT 2018], [11, Thu Jul 12 09:04:00 GMT 2018], [6, Wed Jul 11 14:06:00 GMT 2018], [2, Wed Jul 11 13:27:00 GMT 2018]]
但是,当我将这段代码作为jenkins管道的一部分运行时,它会发出异常提示,结果如下:
itemsDateNotString
[[1, Thu Aug 09 11:13:00 GMT 2018], [2, Wed Jul 11 13:27:00 GMT 2018], [3, Tue Oct 02 15:48:00 GMT 2018], [4, Mon Sep 03 14:27:00 GMT 2018], [5, Sat Sep 08 10:07:00 GMT 2018], [6, Wed Jul 11 14:06:00 GMT 2018], [7, Sat Sep 08 10:16:00 GMT 2018], [8, Thu Aug 09 09:53:00 GMT 2018], [9, Mon Jul 16 12:59:00 GMT 2018], [10, Wed Aug 08 15:14:00 GMT 2018], [11, Thu Jul 12 09:04:00 GMT 2018], [12, Wed Aug 08 14:32:00 GMT 2018], [13, Sat Sep 08 10:59:00 GMT 2018], [14, Sat Sep 08 09:50:00 GMT 2018], [15, Thu Aug 16 09:12:00 GMT 2018], [16, Wed Sep 19 09:59:00 GMT 2018], [17, Wed Jul 18 10:25:00 GMT 2018], [18, Tue Sep 11 14:45:00 GMT 2018], [19, Thu Jul 12 09:36:00 GMT 2018], [20, Tue Oct 02 09:18:00 GMT 2018]]
itemsSorted:
Wed Jul 11 13:27:00 GMT 2018
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.Date.reverse() is applicable for argument types: () values: []
Possible solutions: every(), every(groovy.lang.Closure), before(java.util.Date), parse(java.lang.String), parse(java.lang.String, java.lang.String), parse(java.lang.String, java.lang.String, java.util.TimeZone)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
at tmpDeleteArtifacts.printItems(<PATH>)
at tmpDeleteArtifacts.call(<PATH>)
at standardPipeline.call(<PATH>)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77)
at sun.reflect.GeneratedMethodAccessor251.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:182)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
提供了该代码可在jenkins脚本控制台上运行的情况-我不确定为什么管道会给出不同的结果。我相信它是排序功能,因为我希望它可以按日期顺序对列表进行排序,但是它在管道中返回单个日期,而脚本控制台返回已排序的列表。
预先感谢, 杰伊。
答案 0 :(得分:2)
这会发生在您身上,因为您是在Groovy CPS模式下运行列表转换的,这种模式在以连续传递样式运行时有很多限制。解决方案非常简单-将Groovy代码提取到带有@NonCPS
注释的函数中,以便Jenkins在CPS模式之外执行它。看一个简单的例子:
Jenkinsfile :
node {
stage("Test") {
def items = [[1, "09-Aug-2018 11:13" ],
[2, "11-Jul-2018 13:27" ],
[3, "02-Oct-2018 15:48" ],
[4, "03-Sep-2018 14:27" ],
[5, "08-Sep-2018 10:07" ],
[6, "11-Jul-2018 14:06" ],
[7, "08-Sep-2018 10:16" ],
[8, "09-Aug-2018 09:53" ],
[9, "16-Jul-2018 12:59" ],
[10, "08-Aug-2018 15:14" ],
[11, "12-Jul-2018 09:04" ],
[12, "08-Aug-2018 14:32" ],
[13, "08-Sep-2018 10:59" ],
[14, "08-Sep-2018 09:50" ],
[15, "16-Aug-2018 09:12" ],
[16, "19-Sep-2018 09:59" ],
[17, "18-Jul-2018 10:25" ],
[18, "11-Sep-2018 14:45" ],
[19, "12-Jul-2018 09:36" ],
[20, "02-Oct-2018 09:18" ]]
sortExample(items)
}
}
@NonCPS
def sortExample(items) {
def itemsDateNotString = items.collect { [it[0], new Date().parse("dd-MMM-yyyy H:m", it[1])] }
println(itemsDateNotString)
def itemsSorted = itemsDateNotString.sort{ it[1] }
println(itemsSorted)
def itemsSortedReversed = itemsSorted.reverse()
println(itemsSortedReversed)
}
输出:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
[[1, Thu Aug 09 11:13:00 UTC 2018], [2, Wed Jul 11 13:27:00 UTC 2018], [3, Tue Oct 02 15:48:00 UTC 2018], [4, Mon Sep 03 14:27:00 UTC 2018], [5, Sat Sep 08 10:07:00 UTC 2018], [6, Wed Jul 11 14:06:00 UTC 2018], [7, Sat Sep 08 10:16:00 UTC 2018], [8, Thu Aug 09 09:53:00 UTC 2018], [9, Mon Jul 16 12:59:00 UTC 2018], [10, Wed Aug 08 15:14:00 UTC 2018], [11, Thu Jul 12 09:04:00 UTC 2018], [12, Wed Aug 08 14:32:00 UTC 2018], [13, Sat Sep 08 10:59:00 UTC 2018], [14, Sat Sep 08 09:50:00 UTC 2018], [15, Thu Aug 16 09:12:00 UTC 2018], [16, Wed Sep 19 09:59:00 UTC 2018], [17, Wed Jul 18 10:25:00 UTC 2018], [18, Tue Sep 11 14:45:00 UTC 2018], [19, Thu Jul 12 09:36:00 UTC 2018], [20, Tue Oct 02 09:18:00 UTC 2018]]
[Pipeline] echo
[[2, Wed Jul 11 13:27:00 UTC 2018], [6, Wed Jul 11 14:06:00 UTC 2018], [11, Thu Jul 12 09:04:00 UTC 2018], [19, Thu Jul 12 09:36:00 UTC 2018], [9, Mon Jul 16 12:59:00 UTC 2018], [17, Wed Jul 18 10:25:00 UTC 2018], [12, Wed Aug 08 14:32:00 UTC 2018], [10, Wed Aug 08 15:14:00 UTC 2018], [8, Thu Aug 09 09:53:00 UTC 2018], [1, Thu Aug 09 11:13:00 UTC 2018], [15, Thu Aug 16 09:12:00 UTC 2018], [4, Mon Sep 03 14:27:00 UTC 2018], [14, Sat Sep 08 09:50:00 UTC 2018], [5, Sat Sep 08 10:07:00 UTC 2018], [7, Sat Sep 08 10:16:00 UTC 2018], [13, Sat Sep 08 10:59:00 UTC 2018], [18, Tue Sep 11 14:45:00 UTC 2018], [16, Wed Sep 19 09:59:00 UTC 2018], [20, Tue Oct 02 09:18:00 UTC 2018], [3, Tue Oct 02 15:48:00 UTC 2018]]
[Pipeline] echo
[[3, Tue Oct 02 15:48:00 UTC 2018], [20, Tue Oct 02 09:18:00 UTC 2018], [16, Wed Sep 19 09:59:00 UTC 2018], [18, Tue Sep 11 14:45:00 UTC 2018], [13, Sat Sep 08 10:59:00 UTC 2018], [7, Sat Sep 08 10:16:00 UTC 2018], [5, Sat Sep 08 10:07:00 UTC 2018], [14, Sat Sep 08 09:50:00 UTC 2018], [4, Mon Sep 03 14:27:00 UTC 2018], [15, Thu Aug 16 09:12:00 UTC 2018], [1, Thu Aug 09 11:13:00 UTC 2018], [8, Thu Aug 09 09:53:00 UTC 2018], [10, Wed Aug 08 15:14:00 UTC 2018], [12, Wed Aug 08 14:32:00 UTC 2018], [17, Wed Jul 18 10:25:00 UTC 2018], [9, Mon Jul 16 12:59:00 UTC 2018], [19, Thu Jul 12 09:36:00 UTC 2018], [11, Thu Jul 12 09:04:00 UTC 2018], [6, Wed Jul 11 14:06:00 UTC 2018], [2, Wed Jul 11 13:27:00 UTC 2018]]
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
“ 使用带有
@NonCPS
注释的功能来完成稍微复杂的工作。这意味着需要更多的处理,逻辑和转换。这使您可以利用其他Groovy和功能来获得更强大的功能,简洁而高效的代码。”来源:https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/