By Pradeepto K. Bhattacharya, MSys-Clogeny
Do you have complex pipeline scripts that you run for your continuous integration (CI)? Do you want to access Jenkins from those scripts? What about searching and accessing the artifacts? Or would you like to block a job until something has finished correctly? Interact with slaves? Would you like to have some kind of synchronization between your jobs and pipeline scripts and yet keep the your job configurations as simple as possible? This talk is about you getting more programmatic control of your Jenkins instance from your pipeline scripts using the Python API. We will look at the power of the API by showing working code examples and demoing the results. This talk will be very friendly to Jenkins beginners and intermediates alike. We will walk through the concepts and actual code during the presentation.
5. #jenkinsconf
Why?
l
Sometimes you want more control over how you want run your
jobs.
l
It is true that you have many plugins to so many things like -
copy artifacts, run child jobs etc. No doubt about it.
l
But then there are times you have to develop systems or CI
pipelines where you need more fine grained control.
l
Thanks to the developers of Jenkins, there is an excellent
REST API.
l
Open source being open source, some good dudes have
written a Python library encapsulating the REST API
l
This talk is about this library and what can we do with it.
9. #jenkinsconf
Main modules
l
Jenkins : The Jenkins instance
l
Job(s) : Represents Jenkins jobs.
l
User API : Consists of helpful high-level functions
l
Build : This module encapsulates the single run of a
Jenkins job
l
Artifact : Artifacts are created by Jenkins build. This
module encapsulates that.
l
Node : Encapsulates Jenkins Node
11. #jenkinsconf
Jobs
l
>>> jenkins = get_jenkins_instance()
l
>>> jenkins.keys()
l
>>> ['Beefy Job', 'Simple Job']
l
>>> jobs = jenkins.get_jobs()
l
>>> for job in jobs:
l
... print job
l
...
l
('Beefy Job', <jenkinsapi.job.Job Beefy Job>)
l
('Simple Job', <jenkinsapi.job.Job Simple Job>)
12. #jenkinsconf
Jobs
l
>>> job = jenkins.get_job('Simple Job')
l
>>> job.get_description()
l
'A very simple job'
l
>>> job.is_enabled()
l
True
l
>>> job.is_running()
l
False
l
>>> job.get_last_stable_buildnumber()
l
29
13. #jenkinsconf
Jobs
l
>>> job = jenkins.get_job('Beefy Job')
l
>>> job.get_last_failed_buildnumber()
l
3
l
>>> job.get_last_completed_build()
l
<jenkinsapi.build.Build Beefy Job #53>
l
>>> job.invoke()
l
<jenkinsapi.invocation.Invocation object at 0x7f0f75733050>
l
>>> job.is_running()
l
True
17. #jenkinsconf
User API
l
from jenkinsapi.api import *
l
>>> get_latest_build('http://10.10.20.100:8080','Simple Job')
l
<jenkinsapi.build.Build Simple Job #29>
l
>>> get_latest_complete_build('http://10.10.20.100:8080','Beefy Job')
l
<jenkinsapi.build.Build Beefy Job #53>
18. #jenkinsconf
Build
l
>>> build = job.get_build(54)
l
>>> build.is_running()
l
True
l
>>> build.get_revision()
l
'b0c30d15b4c0ac55d003db7533c00e889f74891e'
l
>>> build.name
l
'Beefy Job #57'
l
>>> build.get_status()
l
'SUCCESS'
19. #jenkinsconf
Build
l
>>> build.get_console()
l
'Started by user anonymousnBuilding on master in workspace /var/lib/jenkins/workspace/Beefy
Jobn > git rev-parse --is-inside-work-tree # timeout=10nFetching changes from the remote Git
repositoryn > git config remote.origin.url http://github.com/jenkinsci/mesos-plugin #
timeout=10nFetching upstream changes from http://github.com/jenkinsci/mesos-pluginn > git
--version # timeout=10n > git -c core.askpass=true fetch --tags --progress
http://github.com/jenkinsci/mesos-plugin +refs/heads/*:refs/remotes/origin/*n > git rev-parse
refs/remotes/origin/master^{commit} # timeout=10n > git rev-parse
refs/remotes/origin/origin/master^{commit} # timeout=10nChecking out Revision
b0c30d15b4c0ac55d003db7533c00e889f74891e (refs/remotes/origin/master)n > git config
core.sparsecheckout # timeout=10n > git checkout -f
b0c30d15b4c0ac55d003db7533c00e889f74891en > git rev-list
b0c30d15b4c0ac55d003db7533c00e889f74891e # timeout=10n[Beefy Job] $ /bin/sh -xe
/tmp/hudson1394675615697321597.shn+ set -xn+ echo This job takes a lot of time and
consumes a lot of resources.nThis job takes a lot of time and consumes a lot of resources.n+
which pythonn/usr/bin/pythonn+ python --versionnPython 2.7.6n+ sleep 180n+ echo Done
building beefy job.nDone building beefy job.nFinished: SUCCESSn'
21. #jenkinsconf
Artifacts
l
>>> job = jenkins.get_job('Simple Job')
l
>>> build = job.get_build(33)
l
>>> build.get_artifact_dict()
l
{'artifact.txt': <jenkinsapi.artifact.Artifact http://10.10.20.100:8080/job/Simple
%20Job/33/artifact/artifact.txt>}
l
>>> build.get_artifact_dict()['artifact.txt'].get_data()
l
'This is artifactn'
22. #jenkinsconf
Nodes
l
>>> for node in jenkins.get_nodes().keys():
l
... print node
l
...
l
master
l
Big
l
Small
l
>>> small.is_online()
l
True
24. #jenkinsconf
Exceptions
l
This library comes with bunch of built-in exceptions. I highly
recommend you use them in your code and do stuff as you
want in case you catch those whilst running your CI pipelines.
l
JenkinsAPIException, UnknownJob, UnknownView,
UnknownNode, UnknownPlugin, NoBuildData, NoResults ...
25. #jenkinsconf
Other things you could ...
l
Create Jobs
l
Create Views
l
Monitor and query builds/jobs/nodes
l
Search and manipulate artifacts
26. #jenkinsconf
Documentation
l
Some (very few) of the modules are documented.
l
There are a few examples in documentation.
l
Great opportunity to contribute to this wonderful
library.
l
https://github.com/salimfadhley/jenkinsapi
28. #jenkinsconf
Agenda
CloudBees and organisers of JUC Europe 2015
Contributors of Jenkins, Mesos, Mesos plugin
My Family
Kalpak, Shikha, Lenin
Girija and Rupali (my partners in CI related crimes)
29. #jenkinsconf
Please Share Your Feedback
• Did you find this session valuable?
• Please share your thoughts in the
• Jenkins User Conference Mobile App.
• Find the session in the app and click
• on the feedback area.