gpt4 book ai didi

sorting - Jenkins Pipelines 中的列表就地排序

转载 作者:行者123 更新时间:2023-12-02 00:41:36 25 4
gpt4 key购买 nike

我正在尝试对 Jenkins 管道中的对象列表进行排序。我在本地或 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()
}
}
}
}
}

当我使用 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

在 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

Jenkins 工作流是否使列表无论如何都不可变,或者重写 sort 方法,如果是,如何在 Jenkins 管道代码中进行就地列表排序?

最佳答案

您将 nonCpsTest 声明为变量并引用闭包的问题,​​因此 @NonCPS 在这种情况下不起作用

以下变体工作正常:

@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()
}

关于sorting - Jenkins Pipelines 中的列表就地排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46335062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com