HNygard
HNygard

Reputation: 4796

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

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?

Upvotes: 36

Views: 82193

Answers (4)

Wulf
Wulf

Reputation: 489

Here's the query for passing jobs only:

http://jenkinsURL/job/ProjectFolderName/api/xml?tree=jobs[name,color=blue]

Here's the query for failing jobs only:

http://jenkinsURL/job/ProjectFolderName/api/xml?tree=jobs[name,color=yellow]

Upvotes: 0

Craig Wayne
Craig Wayne

Reputation: 5060

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

Upvotes: 13

Vitalii Elenhaupt
Vitalii Elenhaupt

Reputation: 7326

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

Upvotes: 9

Larry Cai
Larry Cai

Reputation: 59963

It looks it isn't supported in JSON API, however if you can use XML API, it is possible to query via XPATH, see sample below

http://jenkins.example.com/api/xml?tree=jobs[name,builds[number,actions[parameters[name,value]]]]&exclude=hudson/job/build/action/parameter[value!=%275447e2f43ea44eb4168d6b32e1a7487a3fdf237f%27]

You may tune the better query string to fit for your needs.

credit to http://blog.dahanne.net/2014/04/02/using-jenkins-hudson-remote-api-to-check-jobs-status/

Upvotes: 3

Related Questions