SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler 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.

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
 

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
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...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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)
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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.