如何使用 groovy 对 JFrog JSON 响应进行排序和提取数据以用于 Jenkins 流水线

How to sort and extract data from JFrog JSON response using groovy for Jenkins pipelining

我正在使用 OS 版本的 JFrog Artifactory 进行我的 CI-CD 活动,这些活动 运行 通过 Jenkins 管道。我是新手 groovy/java OS JFrog Artifactory 的 REST API 不支持从存储库中提取最新版本。随着 Jenkins 管道的运行,我想知道我是否可以使用 Jenkins 本机 groovy 支持从 Artifactory 提供的 JSON 响应中提取数据(只是为了避免外部服务 运行 通过python/Java/Shell)。

我希望将提取的 JSON 响应放入 Map,按降序对 Map 进行排序并提取包含最新构建信息的第一个键值对。 当我尝试提取数据时,我最终得到“-1”作为响应。

import groovy.json.JsonSlurper

def response = httpRequest authentication: 'ArtifactoryAPIKey', consoleLogResponseBody: false, contentType: 'TEXT_PLAIN', httpMode: 'POST', requestBody: '''
    items.find({
    "$and": [
        {"repo": {"$match": "libs-snapshot-local"}},
        {"name": {"$match": "simple-integration*.jar"}}
            ]
     })''', url: 'http://<my-ip-and-port-info>/artifactory/api/search/aql'

def jsonParser = new JsonSlurper()
Map jsonOutput = jsonParser.parseText(response.content)
List resultsInfo = jsonOutput['results']
print(resultInfo[0].created)

def sortedResult = resultInfo.sort( {a, b -> b["created"] <=> a["created"] } )
sortedResult.each { 
 println it
}

待解析样本JSON:

    {
"results" : [ {
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.150.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-23T19:51:30.367+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-23T19:51:30.364+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-23T19:51:30.368+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.140",
  "name" : "simple-integration-2.5.140.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-21T19:52:40.670+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-21T19:52:40.659+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-21T19:52:40.671+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.160.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-28T19:58:04.973+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-28T19:58:04.970+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-28T19:58:04.973+05:30"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 3,
  "total" : 3
}
}






  //The output i am looking for: Latest build info with fields "created" and "name"
def jsonOutput = new groovy.json.JsonSlurper().parseText('''
{
"results" : [ {
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.150.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-23T19:51:30.367+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-23T19:51:30.364+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-23T19:51:30.368+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.140",
  "name" : "simple-integration-2.5.140.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-21T19:52:40.670+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-21T19:52:40.659+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-21T19:52:40.671+05:30"
},{
  "repo" : "libs-snapshot-local",
  "path" : "simple-integration/2.5.150",
  "name" : "simple-integration-2.5.160.jar",
  "type" : "file",
  "size" : 1175,
  "created" : "2019-06-28T19:58:04.973+05:30",
  "created_by" : "admin",
  "modified" : "2019-06-28T19:58:04.970+05:30",
  "modified_by" : "admin",
  "updated" : "2019-06-28T19:58:04.973+05:30"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 3,
  "total" : 3
}
}
''')

def last = jsonOutput.results.sort{a, b -> b.created <=> a.created }[0]
println last.created
println last.name

这里的问题不在于 Groovy 代码,而在于 Jenkins 管道。 此代码作为问题的一部分,@daggett 提供的解决方案对任何 Groovy IDE 都有效,但是,当 运行 通过 jenkins 管道时失败。

问题URL:https://issues.jenkins-ci.org/browse/JENKINS-44924

我希望他们尽快修复它。 谢谢你们的帮助。

这里是 link 我正在研究的完整解决方案。仅供参考,它可以帮助某人 https://github.com/AravinthR/Py-JFrog-Fetch-Build