【问题标题】:How to sort and extract data from JFrog JSON response using groovy for Jenkins pipelining如何使用 groovy for Jenkins 流水线从 JFrog JSON 响应中排序和提取数据
【发布时间】: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"

【问题讨论】:

  • 你的代码应该可以工作,除非你拼错了变量名:resultInfo vs resultsInfo
  • @daggett 我很抱歉。这是一个错字。即使更改为 resultsInfo 后,我也会收到相同的错误

标签: json api jenkins groovy jfrog-cli


【解决方案1】:
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

【讨论】:

  • org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:找不到这样的 getAt 方法:org.jenkinsci.plugins.scriptsecurity.sandbox 中的方法 java.lang.Integer[java.lang.Integer]。 groovy.SandboxInterceptor.onGetArray(SandboxInterceptor.java:479)
  • 你检查jenkins_host/scriptApproval是否有脚本需要批准
  • 上面的代码是可以运行的。自己检查:groovyconsole.appspot.com/script/5070537919299584
  • 您的解决方案有效。我的也是。但是,这里的问题是 Jenkins 的一个错误,它产生的结果为“-1”。在issues.jenkins-ci.org/browse/JENKINS-44924 中跟踪了这个问题
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 这是使用 Python 构建的。不时髦。必须在 Groovy 上覆盖 jenkins 支持的限制
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多