Jenkins排序方法行为与预期不符 [英] Jenkins sort method behaviour not as expected

查看:139
本文介绍了Jenkins排序方法行为与预期不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与jenkins脚本控制台相比,在尝试对列表进行排序时,在jenkins管道中得到了不同的结果.

I'm getting different results in jenkins pipeline when trying to sort a list compared to jenkins script console.

詹金斯版本: 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脚本控制台上的结果:

Results on my laptop and on Jenkins script console:

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管道的一部分运行时,它会发出异常提示,结果如下:

However when I run this code as part of the jenkins pipeline it thows and exception and the results are below:

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脚本控制台上运行的情况-我不确定为什么管道会给出不同的结果.我相信它是排序功能,因为我希望它可以按日期顺序对列表进行排序,但是它在管道中返回单个日期,而脚本控制台返回已排序的列表.

Provided that the code works on jenkins script consolei'm not sure why the pipeline is giving different results. I believe it's the sort function because I would expect it to sort the list in date order however it is returning an single date in the pipeline whereas the script console is returning the sorted list.

预先感谢, 杰伊.

推荐答案

您会遇到这种情况,因为您是在Groovy CPS模式下运行列表转换的,这种模式在以连续传递方式运行时有很多限制.解决方案非常简单-将Groovy代码提取到带有@NonCPS注释的函数中,以便Jenkins在CPS模式之外执行该代码.看一下这个简单的例子:

It happens to you, because you run list transformation in Groovy CPS mode, which has plenty of limitations when running in continuation passing style. Solution is fairly simple - extract your Groovy code to a function annotated with @NonCPS so Jenkins executes it outside CPS mode. Take a look at this simple example:

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&功能功能来获得更强大,更简洁的功能. ,以及性能代码."

"Use @NonCPS-annotated functions for slightly more complex work. This means more involved processing, logic, and transformations. This lets you leverage additional Groovy & functional features for more powerful, concise, and performant code."

来源: https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/

这篇关于Jenkins排序方法行为与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆