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

58,361

Solution 1

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

Solution 2

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

Solution 3

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/

Share:
58,361
HNygard
Author by

HNygard

Mostly working on PHP projects.

Updated on May 08, 2020

Comments

  • HNygard
    HNygard almost 4 years

    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?