jenkinsjenkins-api

Jenkins xml rest API to get list of job names and last build number filtered by name


I want to get list of all job names and their last build number filtered by job name. I tried to use the following -

https://blahblahxxxxx/jenkins/api/xml?tree=jobs[displayName,lastBuild[number]]&xpath=/hudson/job&includes=/hudson/job/displayName[contains(.,%27myjobnamefilter%27)]&wrapper=job_names&pretty=true

But this only gives all jobs and their last build numbers without filtering for job name.

e.g.

<job_names>
  <job>
    <displayName>blahjob</displayName>
    <lastBuild>
         <number>25</number>
    </lastBuild>
  </job>
  <job>
    <displayName>blahblahblahjob</displayName>
    <lastBuild>
         <number>49</number>
    </lastBuild>
  </job>
<job_names>

Solution

  • You are doing it right. But you need to define the multiple values check not using [contains(.,%27myjobnamefilter%27)]. Instead use, [. = 'name1' or . = 'name2']. Here name1 and name2 are the job names that you have got. For more info, Please read this thread.

    https://blahblahxxxxx/jenkins/api/xml?tree=jobs[displayName,lastBuild[number]]&xpath=hudson/job[displayName[. = 'name1' or . = 'name2']]&wrapper=job_names&pretty=true
    

    Hope this helps.