Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins API: Get a list of jobs filtered by build parameter - What jobs have built this Git commit?

Tags:

jenkins

api

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?

like image 855
HNygard Avatar asked Sep 05 '14 14:09

HNygard


People also ask

How do I get a list of jobs in Jenkins?

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/" }, ... }


2 Answers

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

like image 143
Craig Wayne Avatar answered Sep 20 '22 15:09

Craig Wayne


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

like image 31
Vitalii Elenhaupt Avatar answered Sep 16 '22 15:09

Vitalii Elenhaupt