【发布时间】:2019-11-09 04:14:51
【问题描述】:
我在通过 Jenkins 管道运行的 CI-CD 活动中使用 JFrog Artifactory 的操作系统版本。我是 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"
【问题讨论】:
-
你的代码应该可以工作,除非你拼错了变量名:
resultInfovsresultsInfo -
@daggett 我很抱歉。这是一个错字。即使更改为 resultsInfo 后,我也会收到相同的错误
标签: json api jenkins groovy jfrog-cli