Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and query a specific build in Jenkins using the Python Jenkins API

We have a Jenkins job that runs builds using specific parameters. Two of these parameters are important for me: the machine that the build is being deployed on, and the version number of the package that is deployed.

https://jenkinsurl/job/folder_level1/job/folder_level2/job/folder_level3/job_id/

Here is a sample of json output of the url:

https://jenkinsurl/job/folder_level1/job/folder_level2/job/folder_level3/job_id/api/json

{"actions":[{"parameters":[{"name":"lab_name","value":"labA"},{"name":"version_no","value":"1.1"}]}

Using the Jenkins REST API or the Python Jenkins wrapper, how would I search for the job if I know the folder_level1 and would like to match the lab name to a job in folder_level3 to finally get the version from that URL?

like image 944
shanwar Avatar asked Jul 22 '16 19:07

shanwar


1 Answers

Use the /api/xml format:

https://jenkinsurl/job/folder_level1/api/xml

which returns the action XML node which can be queried via XPath:

  • https://jenkinsurl/job/folder_level1/api/xml?xpath=//action&wrapper=root

Take the matching name from there to search for the data in question:

  • builtOn - the machine that the build is being deployed on
  • number - the version number of the package that is deployed

Using an XPath for each, along with a wrapper node for grouping, such as the following for builtOn:

https://jenkinsurl/job/folder_level1/api/xml?depth=3&xpath=//fullDisplayName[contains(text(),'foo')]/following-sibling::builtOn&wrapper=builtOn_results

and another for version:

https://jenkinsurl/job/folder_level1/api/xml?depth=3&xpath=//fullDisplayName[contains(text(),'foo')]/following-sibling::number&wrapper=version_results

References

  • Jenkins Wiki: Remote Access API

  • Taming the Jenkins JSON API with Depth and "Tree" | CloudBees

  • python-jenkins API(pdf)

  • xml.tree.elementtree: Supported XPath Syntax
like image 84
Paul Sweatte Avatar answered Oct 16 '22 22:10

Paul Sweatte