SlideShare a Scribd company logo
1 of 29
Download to read offline
#jenkinsconf
Orchestrating Your Jenkins Pipelines With
Python and Jenkins API
London, 23 & 24 June 2015
#jenkinsconf
whoami
Pradeepto Bhattacharya / @pradeepto
#jenkinsconf
Agenda
l
Why do we need this library?
l
Installation
l
Library Modules
l
Code Examples
l
Documentations
l
Q & A
#jenkinsconf
#jenkinsconf
The Motivation
#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.
#jenkinsconf
#jenkinsconf
Installation
#jenkinsconf
Installation
l
pip install jenkinsapi
l
easy_install jenkinsapi
l
sudo apt-get install python-jenkinsapi
l
Note : There are other similar libraries. I found this one best for all my use cases. It definitely was
the most exhaustive.
#jenkinsconf
#jenkinsconf
Modules and Code
#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
#jenkinsconf
Connect to Jenkins
l
from jenkinsapi.jenkins import Jenkins
l
def get_jenkins_instance():
l
return Jenkins('http://10.10.20.100:8080')
#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>)
#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
#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
#jenkinsconf
Jobs
l
>>> job.get_scm_type()
l
'git'
l
>>> job.get_scm_branch()
l
['*/master']
l
>>> job.get_scm_url()
l
['http://github.com/jenkinsci/mesos-plugin']
#jenkinsconf
Jobs
l
>>> job.disable()
l
>>> job.is_enabled()
l
False
l
>>> job.enable()
l
>>> job.is_enabled()
l
True
#jenkinsconf
Jobs
l
'get_first_build', 'get_first_buildnumber', 'get_jenkins_obj', 'get_last_build',
'get_last_buildnumber', 'get_last_completed_build', 'get_last_completed_buildnumber',
'get_last_failed_buildnumber', 'get_last_good_build', 'get_last_good_buildnumber',
'get_last_stable_build', 'get_last_stable_buildnumber', 'get_next_build_number',
'get_params', 'get_params_list', 'get_revision_dict', 'get_scm_branch', 'get_scm_type',
'get_scm_url', 'get_upstream_job_names', 'get_upstream_jobs', 'has_queued_build',
'invoke', 'is_enabled', 'is_queued', 'is_queued_or_running', 'is_running'
#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>
#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'
#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'
#jenkinsconf
Build
l
'block', 'block_until_complete', 'buildno', 'get_actions', 'get_artifact_dict', 'get_artifacts',
'get_console', 'get_data', 'get_downstream_builds', 'get_downstream_job_names',
'get_downstream_jobs', 'get_duration', 'get_jenkins_obj', 'get_master_build',
'get_master_build_number', 'get_master_job', 'get_master_job_name',
'get_matrix_runs', 'get_number', 'get_result_url', 'get_resultset', 'get_revision',
'get_revision_branch', 'get_status', 'get_timestamp', 'get_upstream_build',
'get_upstream_build_number', 'get_upstream_job', 'get_upstream_job_name',
'has_resultset', 'is_good', 'is_running'
#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'
#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
#jenkinsconf
Plugins
l
>>> jenkins.get_plugins().keys()
l
['git-client', 'matrix-auth', 'mesos', 'maven-plugin', 'javadoc', 'external-monitor-job', 'ant',
'ssh-slaves', 'pam-auth', 'windows-slaves', 'git', 'scm-api', 'subversion', 'antisamy-
markup-formatter', 'ldap', 'junit', 'mailer', 'credentials', 'translation', 'ssh-credentials',
'matrix-project', 'cvs', 'script-security']
#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 ...
#jenkinsconf
Other things you could ...
l
Create Jobs
l
Create Views
l
Monitor and query builds/jobs/nodes
l
Search and manipulate artifacts
#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
#jenkinsconf
Questions?
pradeeptob@gmail.com
#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)
#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.

More Related Content

What's hot

What's hot (20)

JUC Europe 2015: Plugin Development with Gradle and Groovy
JUC Europe 2015: Plugin Development with Gradle and GroovyJUC Europe 2015: Plugin Development with Gradle and Groovy
JUC Europe 2015: Plugin Development with Gradle and Groovy
 
JUC Europe 2015: Continuous Integration and Distribution in the Cloud with DE...
JUC Europe 2015: Continuous Integration and Distribution in the Cloud with DE...JUC Europe 2015: Continuous Integration and Distribution in the Cloud with DE...
JUC Europe 2015: Continuous Integration and Distribution in the Cloud with DE...
 
JUC Europe 2015: Scaling Your Jenkins Master with Docker
JUC Europe 2015: Scaling Your Jenkins Master with DockerJUC Europe 2015: Scaling Your Jenkins Master with Docker
JUC Europe 2015: Scaling Your Jenkins Master with Docker
 
JUC Europe 2015: Jenkins Pipeline for Continuous Delivery of Big Data Projects
JUC Europe 2015: Jenkins Pipeline for Continuous Delivery of Big Data ProjectsJUC Europe 2015: Jenkins Pipeline for Continuous Delivery of Big Data Projects
JUC Europe 2015: Jenkins Pipeline for Continuous Delivery of Big Data Projects
 
JUC Europe 2015: Bringing CD at Cloud-Scale with Jenkins, Docker and "Tiger"
JUC Europe 2015: Bringing CD at Cloud-Scale with Jenkins, Docker and "Tiger"JUC Europe 2015: Bringing CD at Cloud-Scale with Jenkins, Docker and "Tiger"
JUC Europe 2015: Bringing CD at Cloud-Scale with Jenkins, Docker and "Tiger"
 
Building Jenkins Pipelines at Scale
Building Jenkins Pipelines at ScaleBuilding Jenkins Pipelines at Scale
Building Jenkins Pipelines at Scale
 
At Your Service: Using Jenkins in Operations
At Your Service: Using Jenkins in OperationsAt Your Service: Using Jenkins in Operations
At Your Service: Using Jenkins in Operations
 
Managing Jenkins with Jenkins (Jenkins User Conference Palo Alto, 2013)
Managing Jenkins with Jenkins (Jenkins User Conference Palo Alto, 2013)Managing Jenkins with Jenkins (Jenkins User Conference Palo Alto, 2013)
Managing Jenkins with Jenkins (Jenkins User Conference Palo Alto, 2013)
 
Seven Habits of Highly Effective Jenkins Users (2014 edition!)
Seven Habits of Highly Effective Jenkins Users (2014 edition!)Seven Habits of Highly Effective Jenkins Users (2014 edition!)
Seven Habits of Highly Effective Jenkins Users (2014 edition!)
 
Scaling your jenkins master with docker
Scaling your jenkins master with dockerScaling your jenkins master with docker
Scaling your jenkins master with docker
 
Brujug Jenkins pipeline scalability
Brujug Jenkins pipeline scalabilityBrujug Jenkins pipeline scalability
Brujug Jenkins pipeline scalability
 
Drupal Continuous Integration with Jenkins - The Basics
Drupal Continuous Integration with Jenkins - The BasicsDrupal Continuous Integration with Jenkins - The Basics
Drupal Continuous Integration with Jenkins - The Basics
 
Grooving with Jenkins
Grooving with JenkinsGrooving with Jenkins
Grooving with Jenkins
 
Jenkins vs. AWS CodePipeline (AWS User Group Berlin)
Jenkins vs. AWS CodePipeline (AWS User Group Berlin)Jenkins vs. AWS CodePipeline (AWS User Group Berlin)
Jenkins vs. AWS CodePipeline (AWS User Group Berlin)
 
An Open-Source Chef Cookbook CI/CD Implementation Using Jenkins Pipelines
An Open-Source Chef Cookbook CI/CD Implementation Using Jenkins PipelinesAn Open-Source Chef Cookbook CI/CD Implementation Using Jenkins Pipelines
An Open-Source Chef Cookbook CI/CD Implementation Using Jenkins Pipelines
 
Let’s start Continuous Integration with jenkins
Let’s start Continuous Integration with jenkinsLet’s start Continuous Integration with jenkins
Let’s start Continuous Integration with jenkins
 
Jenkins Scriptler in 90mins
Jenkins Scriptler in 90minsJenkins Scriptler in 90mins
Jenkins Scriptler in 90mins
 
Jenkins days workshop pipelines - Eric Long
Jenkins days workshop  pipelines - Eric LongJenkins days workshop  pipelines - Eric Long
Jenkins days workshop pipelines - Eric Long
 
Jenkins Best Practices
Jenkins Best PracticesJenkins Best Practices
Jenkins Best Practices
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
 

Similar to JUC Europe 2015: Orchestrating Your Pipelines with Jenkins, Python and the Jenkins API

Tips and tricks for building api heavy ruby on rails applications
Tips and tricks for building api heavy ruby on rails applicationsTips and tricks for building api heavy ruby on rails applications
Tips and tricks for building api heavy ruby on rails applications
Tim Cull
 

Similar to JUC Europe 2015: Orchestrating Your Pipelines with Jenkins, Python and the Jenkins API (20)

Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL PluginConfiguration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
 
JUC Europe 2015: Configuration as Code: The Job DSL Plugin
JUC Europe 2015: Configuration as Code: The Job DSL PluginJUC Europe 2015: Configuration as Code: The Job DSL Plugin
JUC Europe 2015: Configuration as Code: The Job DSL Plugin
 
Using Prometheus to monitor your build pipelines
Using Prometheus to monitor your build pipelinesUsing Prometheus to monitor your build pipelines
Using Prometheus to monitor your build pipelines
 
Django for IoT: From hackathon to production (DjangoCon US)
Django for IoT: From hackathon to production (DjangoCon US)Django for IoT: From hackathon to production (DjangoCon US)
Django for IoT: From hackathon to production (DjangoCon US)
 
Graduating to Jenkins CI for Ruby(-on-Rails) Teams
Graduating to Jenkins CI for Ruby(-on-Rails) TeamsGraduating to Jenkins CI for Ruby(-on-Rails) Teams
Graduating to Jenkins CI for Ruby(-on-Rails) Teams
 
SharePoint PowerShell for the Admin and Developer - A Venn Diagram Experience
SharePoint PowerShell for the Admin and Developer - A Venn Diagram ExperienceSharePoint PowerShell for the Admin and Developer - A Venn Diagram Experience
SharePoint PowerShell for the Admin and Developer - A Venn Diagram Experience
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Tips and tricks for building api heavy ruby on rails applications
Tips and tricks for building api heavy ruby on rails applicationsTips and tricks for building api heavy ruby on rails applications
Tips and tricks for building api heavy ruby on rails applications
 
The Job DSL Plugin: Introduction & What’s New
The Job DSL Plugin: Introduction & What’s NewThe Job DSL Plugin: Introduction & What’s New
The Job DSL Plugin: Introduction & What’s New
 
Service Delivery Assembly Line with Vagrant, Packer, and Ansible
Service Delivery Assembly Line with Vagrant, Packer, and AnsibleService Delivery Assembly Line with Vagrant, Packer, and Ansible
Service Delivery Assembly Line with Vagrant, Packer, and Ansible
 
Super Charged Configuration As Code
Super Charged Configuration As CodeSuper Charged Configuration As Code
Super Charged Configuration As Code
 
Minimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team ProductivityMinimizing Decision Fatigue to Improve Team Productivity
Minimizing Decision Fatigue to Improve Team Productivity
 
Tips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows with ChefTips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows with Chef
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
JavaScript Async for Effortless UX
JavaScript Async for Effortless UXJavaScript Async for Effortless UX
JavaScript Async for Effortless UX
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
Managing Jenkins with Python
Managing Jenkins with PythonManaging Jenkins with Python
Managing Jenkins with Python
 
The Snake and the Butler
The Snake and the ButlerThe Snake and the Butler
The Snake and the Butler
 
Configuration As Code: The Job DSL Plugin
Configuration As Code: The Job DSL PluginConfiguration As Code: The Job DSL Plugin
Configuration As Code: The Job DSL Plugin
 
iOS Unit Testing
iOS Unit TestingiOS Unit Testing
iOS Unit Testing
 

More from CloudBees

Jenkins Workflow Webinar - Dec 10, 2014
Jenkins Workflow Webinar - Dec 10, 2014Jenkins Workflow Webinar - Dec 10, 2014
Jenkins Workflow Webinar - Dec 10, 2014
CloudBees
 

More from CloudBees (17)

JUC Europe 2015: Multi-Node Environment as a Jenkins Slave (Compound-Slave)
JUC Europe 2015: Multi-Node Environment as a Jenkins Slave (Compound-Slave)JUC Europe 2015: Multi-Node Environment as a Jenkins Slave (Compound-Slave)
JUC Europe 2015: Multi-Node Environment as a Jenkins Slave (Compound-Slave)
 
JUC Europe 2015: From Virtual Machines to Containers: Achieving Continuous In...
JUC Europe 2015: From Virtual Machines to Containers: Achieving Continuous In...JUC Europe 2015: From Virtual Machines to Containers: Achieving Continuous In...
JUC Europe 2015: From Virtual Machines to Containers: Achieving Continuous In...
 
JUC Europe 2015: Making Strides towards Enterprise-Scale DevOps...with Jenkin...
JUC Europe 2015: Making Strides towards Enterprise-Scale DevOps...with Jenkin...JUC Europe 2015: Making Strides towards Enterprise-Scale DevOps...with Jenkin...
JUC Europe 2015: Making Strides towards Enterprise-Scale DevOps...with Jenkin...
 
JUC Europe 2015: Evolving the Jenkins UI
JUC Europe 2015: Evolving the Jenkins UIJUC Europe 2015: Evolving the Jenkins UI
JUC Europe 2015: Evolving the Jenkins UI
 
JUC Europe 2015: Using Infrastructure Nodes Wisely With Jenkins and Apache Mesos
JUC Europe 2015: Using Infrastructure Nodes Wisely With Jenkins and Apache MesosJUC Europe 2015: Using Infrastructure Nodes Wisely With Jenkins and Apache Mesos
JUC Europe 2015: Using Infrastructure Nodes Wisely With Jenkins and Apache Mesos
 
JUC Europe 2015: How to Optimize Automated Testing with Everyone's Favorite B...
JUC Europe 2015: How to Optimize Automated Testing with Everyone's Favorite B...JUC Europe 2015: How to Optimize Automated Testing with Everyone's Favorite B...
JUC Europe 2015: How to Optimize Automated Testing with Everyone's Favorite B...
 
JUC 2015 - Keynote Address and Opening Remarks by Kohsuke Kawaguchi, Founder,...
JUC 2015 - Keynote Address and Opening Remarks by Kohsuke Kawaguchi, Founder,...JUC 2015 - Keynote Address and Opening Remarks by Kohsuke Kawaguchi, Founder,...
JUC 2015 - Keynote Address and Opening Remarks by Kohsuke Kawaguchi, Founder,...
 
JUC Europe 2015: A Reproducible Build Environment with Jenkins
JUC Europe 2015: A Reproducible Build Environment with JenkinsJUC Europe 2015: A Reproducible Build Environment with Jenkins
JUC Europe 2015: A Reproducible Build Environment with Jenkins
 
Pimp your jenkins platform with docker - Devops.com 2015/11
Pimp your jenkins platform with docker - Devops.com 2015/11Pimp your jenkins platform with docker - Devops.com 2015/11
Pimp your jenkins platform with docker - Devops.com 2015/11
 
Analyze This! CloudBees Jenkins Cluster Operations and Analytics
Analyze This! CloudBees Jenkins Cluster Operations and AnalyticsAnalyze This! CloudBees Jenkins Cluster Operations and Analytics
Analyze This! CloudBees Jenkins Cluster Operations and Analytics
 
Jenkins Workflow Webinar - Dec 10, 2014
Jenkins Workflow Webinar - Dec 10, 2014Jenkins Workflow Webinar - Dec 10, 2014
Jenkins Workflow Webinar - Dec 10, 2014
 
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
 
Pimp your Continuous Delivery Pipeline with Jenkins workflow (W-JAX 14)
Pimp your Continuous Delivery Pipeline with Jenkins workflow (W-JAX 14)Pimp your Continuous Delivery Pipeline with Jenkins workflow (W-JAX 14)
Pimp your Continuous Delivery Pipeline with Jenkins workflow (W-JAX 14)
 
From Continuous Integration to Continuous Delivery with Jenkins - javaland.de...
From Continuous Integration to Continuous Delivery with Jenkins - javaland.de...From Continuous Integration to Continuous Delivery with Jenkins - javaland.de...
From Continuous Integration to Continuous Delivery with Jenkins - javaland.de...
 
Rethinking Application Design for the Cloud
Rethinking Application Design for the CloudRethinking Application Design for the Cloud
Rethinking Application Design for the Cloud
 
The Fall of Giants
The Fall of GiantsThe Fall of Giants
The Fall of Giants
 
Getting Started with Platform-as-a-Service
Getting Started with Platform-as-a-ServiceGetting Started with Platform-as-a-Service
Getting Started with Platform-as-a-Service
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

JUC Europe 2015: Orchestrating Your Pipelines with Jenkins, Python and the Jenkins API

  • 1. #jenkinsconf Orchestrating Your Jenkins Pipelines With Python and Jenkins API London, 23 & 24 June 2015
  • 3. #jenkinsconf Agenda l Why do we need this library? l Installation l Library Modules l Code Examples l Documentations l Q & A
  • 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.
  • 7. #jenkinsconf Installation l pip install jenkinsapi l easy_install jenkinsapi l sudo apt-get install python-jenkinsapi l Note : There are other similar libraries. I found this one best for all my use cases. It definitely was the most exhaustive.
  • 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
  • 10. #jenkinsconf Connect to Jenkins l from jenkinsapi.jenkins import Jenkins l def get_jenkins_instance(): l return Jenkins('http://10.10.20.100:8080')
  • 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
  • 14. #jenkinsconf Jobs l >>> job.get_scm_type() l 'git' l >>> job.get_scm_branch() l ['*/master'] l >>> job.get_scm_url() l ['http://github.com/jenkinsci/mesos-plugin']
  • 16. #jenkinsconf Jobs l 'get_first_build', 'get_first_buildnumber', 'get_jenkins_obj', 'get_last_build', 'get_last_buildnumber', 'get_last_completed_build', 'get_last_completed_buildnumber', 'get_last_failed_buildnumber', 'get_last_good_build', 'get_last_good_buildnumber', 'get_last_stable_build', 'get_last_stable_buildnumber', 'get_next_build_number', 'get_params', 'get_params_list', 'get_revision_dict', 'get_scm_branch', 'get_scm_type', 'get_scm_url', 'get_upstream_job_names', 'get_upstream_jobs', 'has_queued_build', 'invoke', 'is_enabled', 'is_queued', 'is_queued_or_running', 'is_running'
  • 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'
  • 20. #jenkinsconf Build l 'block', 'block_until_complete', 'buildno', 'get_actions', 'get_artifact_dict', 'get_artifacts', 'get_console', 'get_data', 'get_downstream_builds', 'get_downstream_job_names', 'get_downstream_jobs', 'get_duration', 'get_jenkins_obj', 'get_master_build', 'get_master_build_number', 'get_master_job', 'get_master_job_name', 'get_matrix_runs', 'get_number', 'get_result_url', 'get_resultset', 'get_revision', 'get_revision_branch', 'get_status', 'get_timestamp', 'get_upstream_build', 'get_upstream_build_number', 'get_upstream_job', 'get_upstream_job_name', 'has_resultset', 'is_good', 'is_running'
  • 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
  • 23. #jenkinsconf Plugins l >>> jenkins.get_plugins().keys() l ['git-client', 'matrix-auth', 'mesos', 'maven-plugin', 'javadoc', 'external-monitor-job', 'ant', 'ssh-slaves', 'pam-auth', 'windows-slaves', 'git', 'scm-api', 'subversion', 'antisamy- markup-formatter', 'ldap', 'junit', 'mailer', 'credentials', 'translation', 'ssh-credentials', 'matrix-project', 'cvs', 'script-security']
  • 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.