Jenkins automation

22
Jenkins automation Using Python to automate Jenkins

description

Presentation for Singapore Devops meetup

Transcript of Jenkins automation

Page 1: Jenkins automation

Jenkins automationUsing Python to automate Jenkins

Page 2: Jenkins automation

About me● Work with builds for 8 years● Was Java architect and developer for 7

years before that● Originally from Russia, but lived and work in

US, UK, Denmark and now in Singapore● Follow me on Github: @lechat

Page 3: Jenkins automation

Questions● How many jobs your Jenkins has?

● Do you run parallel build pipelines?

● Do you automate Jenkins?

Page 4: Jenkins automation

Agenda● Save Jenkins jobs in a safe place

● Making config.xml easier to handle

● Making branch builds

● Making and running build pipelines

Page 5: Jenkins automation

I want to save my (Jenkins) job!

Jenkins backup

● Jobs

of course, they only exist in Jenkins and handcrafted to perfection

● Builds

a history of (not so) useful information

● Keys

usually forgotten essential bit of information

Page 6: Jenkins automation

I want to save my (Jenkins) job!

Jenkins backup

● Jobs

Trivial approach - make shell script that will put files somewhere else

Another trivial approach - use Jenkins job to save Jenkins as build artefact

● Useful only as a disaster recovery

Restoring just one job requires Jenkins restart

(only in latest version there is an option to reload job definition)

Page 7: Jenkins automation

I want to save my (Jenkins) job!

But why not keep job definition with your source code?

Use jenkinsapi library to save/restore jobs!

What jenkinsapi can do?

● Query Jenkins about jobs, builds, views, nodes● Create/update jobs, views, nodes (only JNLP nodes for

now)● Execute jobs

Page 8: Jenkins automation

I want to save my (Jenkins) job!

Example 1:

Page 9: Jenkins automation

Making easier to handle config.xml

Editing job’s config.xml is not easy

● You need to know plugin/component internal name

● XML is too verbose

● Editing does not scale

Page 10: Jenkins automation

Making easier to handle config.xml

Page 11: Jenkins automation

Making easier to handle config.xml

Use OpenStack jenkins-job-builder

● Job definition is in YAML

● Good defaults - you only need to specify overrides

● Less stuff to edit

Page 12: Jenkins automation

Making easier to handle config.xml

Page 13: Jenkins automation

Making easier to handle config.xml

Jenkins-job-builder features

● Create templates for jobs

● Create variables and expand them when job is generated

● Create default values used across multiple jobs

● Define macros for repeated sections

Page 14: Jenkins automation

Making branch builds

Example 3: Create jobs for each git branch

Page 15: Jenkins automation

Making job pipelines

Making job pipelines is not trivial in Jenkins

Page 16: Jenkins automation

Making job pipelines

Making good job pipelines is not trivial in Jenkins

● No visualization when making jobs dependencies

● Have to use “Join” plugin

● Hard to run part of the pipeline

Page 17: Jenkins automation

Making job pipelines

Use jenkinsflow library to make and run pipelines

Page 18: Jenkins automation

Making job pipelines

Page 19: Jenkins automation

Making job pipelines

With jenkinsflow

No dependencies between jobs in Jenkins

You can create dynamic pipelines

You can execute pipelines outside Jenkins

good for building automated Git flows

Page 20: Jenkins automation

Summary

All in your source code repository:

● Job definitions

● Job pipelines

● Build automation

No worries about losing Jenkins instance - it can be re-

created in minutes

Page 21: Jenkins automation

Reference

jenkinsapi

https://github.com/salimfadhley/jenkinsapi

OpenStack jenkins-job-builder

https://github.com/openstack-infra/jenkins-job-builder

jenkinsflow

https://github.com/lhupfeldt/jenkinsflow

Page 22: Jenkins automation

Reference

Job creators + editors (summary)

https://gist.github.com/mika/5237127

XML templates - Hudson job creator

https://github.com/hudson2-plugins/jobcreator-tool

Source code for this presentation

https://github.com/lechat/devops-python-jenkins