Saturday, June 23, 2018

Retrieve parameter from a Jenkins REST query

Leave a Comment


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" 

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

3 Answers

Answers 1

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.

Answers 2

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

Answers 3

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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment