Home > front end >  jenkins groovy pipeline sorting
jenkins groovy pipeline sorting

Time:10-14

it is not possible to correctly sort the sheet.

script:

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

result

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

collestions doesn't help. What are the ways to sort the final sheet

CodePudding user response:

You appear to be attempting to sort an array of numeric strings but want them in numeric order. As strings, they are ordered.

You want to groovy sort the list, but on numerical value (79 before 121), not string ("1,2,1" before "7,9")

I'm not 100% clear on what you reference as "the result", but the solution is applicable where appropriate.

If projects is the list,

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

If image_id is the list,

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

should return a sorted list.

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

If you are trying to return a sorted map, see this post.

  • Related