We are sending different parameters to our Jenkins jobs, among them are the Git commit SHA1. We want to get a list of jobs that used that parameter value (the Git SHA1 - which jobs ran this commit?).
The following URL will give us all builds:
http://jenkins.example.com/api/json?tree=jobs[name,builds[number,actions[parameters[name,value]]]]&pretty=true
It takes some time to render (6 seconds) and contains too many builds (5 MB of builds).
Sample output from that URL:
{ "jobs" : [ { "name" : "Job name - Build", "builds" : [ { "actions" : [ { "parameters" : [ { "name" : "GIT_COMMIT_PARAM", "value" : "5447e2f43ea44eb4168d6b32e1a7487a3fdf237f" } ] }, (...)
How can we use the Jenkins JSON API to list all jobs with a certain build parameter value?
Get a list of jobs. This can be done requesting http://jenkins_url:port/api/json?tree=jobs[name,url] . Response example: { "jobs" : [ { "name" : "JOB_NAME1", "url" : "http://jenkins_url:port/job/JOB_NAME1/" }, { "name" : "JOB_NAME2", "url" : "http://jenkins_url:port/job/JOB_NAME2/" }, ... }
Also been looking for this, and luckily i found an awesome gist
https://gist.github.com/justlaputa/5634984
To answer your question:
jenkins_url + /api/json?tree=jobs[name,color]
Using your example from above
http://jenkins.example.com/api/json?tree=jobs[name,color]
So it seems like all you need to do is remove the builds parameter from your original url, and you should be fine
How can we use the Jenkins JSON API to list all jobs with a certain build parameter value?
Not sure about JSON API, but you can use XML API and combine tree and xpath parameters:
http://jenkins_url/api/xml?tree=jobs[name,builds[actions[parameters[name,value]]]]&xpath=/hudson/job[build/action/parameter[name="GIT_COMMIT_PARAM"][value="5447e2f43ea44eb4168d6b32e1a7487a3fdf237f"]]/name&wrapper=job_names&pretty=true
Result sample:
<job_names> <name>JOB1</name> <name>JOB2</name> <name>JOB3</name> ... </job_names>
Note: job falls into this list if at least one it's build was built with desired parameter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With