Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve parameter from a Jenkins REST query

Tags:

jenkins


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

like image 667
Carla Avatar asked Jun 13 '18 12:06

Carla


3 Answers

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

like image 97
rohit thomas Avatar answered Oct 01 '22 20:10

rohit thomas


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.

like image 24
Andrew Gray Avatar answered Oct 01 '22 20:10

Andrew Gray


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

like image 37
Stan E Avatar answered Oct 01 '22 19:10

Stan E