从Jenkins REST查询中检索参数 [英] Retrieve parameter from a Jenkins REST query

查看:183
本文介绍了从Jenkins REST查询中检索参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


以下REST查询将返回上一次成功构建作业的参数: https://localhost/job/test1/lastSuccessfulBuild/api/json
我想检索此构建的参数之一BUILD_VERSION:


The following REST query will return parameters of the last successful build of a job: https://localhost/job/test1/lastSuccessfulBuild/api/json
I'd be interested to retrieve one of the parameters of this build, the BUILD_VERSION:

{

    "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
    "actions": [
        {
            "_class": "hudson.model.CauseAction",
            "causes": [
                {
                    "_class": "hudson.model.Cause$UpstreamCause",
                    "shortDescription": "Started by upstream project \"continuous-testing-pipeline-for-nightly\" build number 114",
                    "upstreamBuild": 114,
                    "upstreamProject": "continuous-testing-pipeline-for-nightly",
                    "upstreamUrl": "job/continuous-testing-pipeline-for-nightly/"
                }
            ]
        },
        { },
        {
            "_class": "hudson.model.ParametersAction",
            "parameters": [

                {
                    "_class": "hudson.model.StringParameterValue",
                    "name": "BUILD_VERSION",
                    "value": "1.1.15"

是否可以使用REST Api直接检索BUILD_VERSION(1.1.15),还是必须手动解析json字符串? 谢谢

Is there a way to retrieve the BUILD_VERSION (1.1.15) directly using the REST Api or do I have to parse manually the json string ? Thanks

推荐答案

是的,您可以获取该值,但是它仅适用于XML API :(
JSON API将使用Tree:)

Yeah you can get the value,But it will only work for XML API :(
The JSON API will return a simplified json object using Tree :)

因此,Jenkins为您提供了api(XML,JSON,PYTHON),您可以从中读取任何项目的Jenkins相关数据. https://localhost/job/test1/lastSuccessfulBuild/api

So Jenkins provides you with api (XML,JSON,PYTHON) from which you can read the Jenkins related data of any project. Documentation in detail is provide in https://localhost/job/test1/lastSuccessfulBuild/api

其中明确指出

In that it clearly states that

  1. XML API -使用XPath控制所需的片段.例如,../api/xml?xpath =// [0]

  1. XML API - Use XPath to control the fragment you want.For example, ../api/xml?xpath=//[0]

JSON API -使用树

Python API -使用st.literal_eval(urllib.urlopen("...").read())

Python API - Use st.literal_eval(urllib.urlopen("...").read())

以上所有内容均可用于从您从API获取的整个凌乱数据中获取特定片段/片段.

All the above can be used to get a specific fragment/piece from the entire messy data that you get from the API.

在您的情况下,出于明显的原因,我们将使用tree:)

In your case, we will use tree for obvious reasons :)

语法: tree=keyname[field1,field2,subkeyname[subfield1]]

为了获取BUILD_VERSION即值

In order to retrieve BUILD_VERSION i.e. value

//jenkins/job/myjob/../api/json?tree=lastSuccessfulBuild[parameters[value]]

上面的内容应该可以为您提供所需的东西,但是需要一点点的麻烦和错误:)

The above should get you what you want, but a bit of trail and error is required :)

您也可以在这里参考,以更好地了解如何在JSON API中使用Tree https://www.cloudbees.com/blog/驯服jenkins-json-api深度和树

You can also refer here for a better understanding of how to use Tree in JSON API https://www.cloudbees.com/blog/taming-jenkins-json-api-depth-and-tree

希望它会有所帮助:)

这篇关于从Jenkins REST查询中检索参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆