[英]List in place sorting in Jenkins Pipelines
I am trying to sort a list of objects in Jenkins pipelines. 我正在尝试对Jenkins管道中的对象列表进行排序。 I'm getting different results running code below locally or within Jenkins: 我在本地下面或在詹金斯内部运行代码得到了不同的结果:
pipeline {
agent any
stages {
stage('default'){
steps {
script {
@NonCPS
def nonCpsTest = {
def list = [
['CreationDate': '200'],
['CreationDate': '300'],
['CreationDate': '100'],
]
def rval = list.sort { it['CreationDate'] }
echo "Rval=$rval"
echo "List=$list"
}
nonCpsTest()
}
}
}
}
}
When I execute this script locally using groovy shell ( groovysh
) result is 当我使用groovy shell( groovysh
)在本地执行此脚本时,结果是
groovy:000> list = [[ 'CreationDate':200 ], [ 'CreationDate':300 ], [ 'CreationDate':100 ]]
===> [[CreationDate:200], [CreationDate:300], [CreationDate:100]]
groovy:000> rval = list.sort { it['CreationDate'] }
===> [[CreationDate:100], [CreationDate:200], [CreationDate:300]]
groovy:000> list
===> [[CreationDate:100], [CreationDate:200], [CreationDate:300]]
groovy:000> list == rval
===> true
While on the Jenkins server I'm getting following 在Jenkins服务器上时,我正在关注
[Pipeline] {
[Pipeline] stage
[Pipeline] { (default)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Rval=300
[Pipeline] echo
List=[[CreationDate:200], [CreationDate:300], [CreationDate:100]]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Is Jenkins workflow making list immutable in anyway, or overriding sort
method, and if so, how to do in place list sorting within the Jenkins pipeline code? Jenkins工作流是否使列表无论如何都是不变的,或者是覆盖sort
方法?如果是,那么如何在Jenkins管道代码中进行位置列表排序?
the problem that you declared nonCpsTest
as a variable and it references to closure, so @NonCPS
does not work in this case 您将nonCpsTest
声明为变量并且引用了闭包的问题,因此@NonCPS
在这种情况下不起作用
the following variant works fine: 以下变体可以正常工作:
@NonCPS
def nonCpsTest() {
def list = [
['CreationDate': '200'],
['CreationDate': '300'],
['CreationDate': '100'],
]
def rval = list.sort{ it['CreationDate'] }
echo "Rval=$rval"
echo "List=$list"
}
node{
nonCpsTest()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.