Retrieve parameter from a Jenkins REST query

后端 未结 3 685
日久生厌
日久生厌 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 00:57

    Here is an example with a public jenkins instance and one of its builds in order to get "candidate_revision" parameter for "lastSuccessfulBuild" build:

    https://jenkins.qa.ubuntu.com/view/All/job/account-plugins-vivid-i386-ci/lastSuccessfulBuild/parameters/

    https://jenkins.qa.ubuntu.com/view/All/job/account-plugins-vivid-i386-ci/lastSuccessfulBuild/api/xml?xpath=/freeStyleBuild/action/parameter[name=%22candidate_revision%22]/value

    0 讨论(0)
  • 2021-02-15 01:07

    Short answer: No.

    Easiest way to programmatically access any attribute exposed via the JSON API is to take the JSON from one of Jenkins supported JSON APIs (in your case: https://localhost/job/<jobname>/lastSuccessfulBuild/api/json)

    1. Copy the resultant JSON into http://json2csharp.com
    2. Generate the corresponding C# code. Don't forget to create a meaningful name for top level class.
    3. Call RestAPI programmatically from C# using RestSharp.
    4. Deserialise the json to the C# class you defined in 2 above.

    Wammo, you have access to the entire object tree and all its values.

    I used this approach to write an MVC5 ASP.NET site I called "BuildDashboard" to provide all the information a development team could want and answered every question they had.

    0 讨论(0)
  • 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 :)

    0 讨论(0)
提交回复
热议问题