繁体   English   中英

jenkins groovy 管道排序

[英]jenkins groovy pipeline sorting

无法正确排序工作表。

脚本:

import groovy.json.JsonSlurper

if (TAGS != 'all') {
    return []
}

def image_id = []

def projectList = new URL("https://gitlab.ru/api/v4/search?scope=projects&search=$PROJECT&private_token=$GITLAB_JENKINS_TOKEN")
def projects = new groovy.json.JsonSlurper().parse(projectList.newReader())
projects.each {
 project_id = it.id
}

def repository_name = "$NAME_REPOSITORY"
def id = repository_name.tokenize('/ ')[-1].tokenize('.')[0]
def repository_id = id

def imageList = new URL("https://gitlab.ru/api/v4/projects/$project_id/registry/repositories/$repository_id/tags?per_page=100&private_token=$GITLAB_JENKINS_TOKEN")
def image = new groovy.json.JsonSlurper().parse(imageList.newReader())
image.each {
image_id.add(it.name)
}


return image_id

结果

118/ 119/ 120/ 121/ 79/ 80/ ...

collestions 没有帮助。 最终表的排序方式有哪些

您似乎正在尝试对数字字符串数组进行排序,但希望它们按数字顺序排列。 作为字符串,它们是有序的。

您想对列表进行常规排序,但对数值(121 之前的 79),而不是字符串(“7,9”之前的“1,2,1”)进行排序

我不是 100% 清楚您所引用的“结果”,但该解决方案在适当的情况下适用。

如果项目是列表,

projects.sort(){ a, b -> a.id.toInteger() <=> b.id.toInteger() }.each {
   // process numerically sorted items
}

如果image_id是列表,

return image_id.sort(){ a, b -> a.toInteger() <=> b.toInteger() }

应该返回一个排序列表。

IE:

def list = ['118', '119', '120', '78', '79']

println "raw sort"
list.sort().each {
  println it
}

println "Integer sort"
list.sort(){ a, b -> a.toInteger() <=> b.toInteger() }.each {
  println it
}
return
raw sort
118
119
120
78
79
Integer sort
78
79
118
119
120

如果您尝试返回已排序的地图,请参阅此帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM