Retrieve parameter from a Jenkins REST query

后端 未结 3 742
日久生厌
日久生厌 2021-02-15 00:03


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

3条回答
  •  轮回少年
    2021-02-15 01:10

    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 :)

    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 - Use XPath to control the fragment you want.For example, ../api/xml?xpath=//[0]

    2. JSON API - Use tree

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

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

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

    Syntax : tree=keyname[field1,field2,subkeyname[subfield1]]

    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 :)

    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

    Hope it helps :)

提交回复
热议问题