如何在詹金斯的内置步骤中解析 JSON 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37062872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse JSON response in a built step in jenkins
提问by khaldoune
In order to industrialize the deployments of an IBM product, I'm going to use its REST API interfaces. I plan to use jenkins in order to orchestrate the calls to the REST APIs. I'm still wondering if it's a good idea...? If so, is there any way to simply parse the JSON responses in order to be able to make some conditions in the steps? Thanks.
为了工业化 IBM 产品的部署,我将使用其 REST API 接口。我计划使用 jenkins 来编排对 REST API 的调用。我仍然想知道这是否是个好主意......?如果是这样,是否有任何方法可以简单地解析 JSON 响应,以便能够在步骤中设置一些条件?谢谢。
回答by badgerr
You didn't say what you're using the API for, but if you have the Groovy plugin, you could use JsonSlurper
您没有说明您使用 API 的目的,但是如果您有Groovy 插件,则可以使用JsonSlurper
Something like
就像是
import groovy.json.JsonSlurper
URL apiUrl = "https://some.website/api/someFunction".toURL()
List json = new JsonSlurper().parse(apiUrl.newReader())
// do stuff with the json object
I'm not quite sure how you would take this and use it directly for conditional build steps during the execution of the job, though.
不过,我不太确定您将如何使用它并在作业执行期间将其直接用于有条件的构建步骤。
An alternative approach is to generate a set of jobs with the appropriate steps based on the API response, using the Job DSL Plugin. This sort of thing can be used for stuff like reading a list of SCM branches and generating jobs for each of them. That may or may not be what you're trying to do.
另一种方法是使用Job DSL Plugin根据 API 响应通过适当的步骤生成一组作业。这类东西可用于读取 SCM 分支列表并为每个分支生成作业之类的东西。这可能是也可能不是你想要做的。
回答by Somaiah Kumbera
JQ https://stedolan.github.io/jq/is a JSON parser for bash. I have used it in the past and its beautiful.
JQ https://stedolan.github.io/jq/是 bash 的 JSON 解析器。我过去用过它,它很漂亮。
You can download JQ to your Jenkins server, and then call JQ in your build step bash scripts
您可以将 JQ 下载到您的 Jenkins 服务器,然后在您的构建步骤 bash 脚本中调用 JQ
回答by Yevhen
If you are using Pipeline Job, you will be happy with Pipeline Utility Steps Plugin
如果您正在使用 Pipeline Job,您会对Pipeline Utility Steps Plugin感到满意

