Pipeline as code Building Continuous Delivery Pipelines with Jenkins 2.0 Bert	Jan	Schrijver @bjschrijverbertjan@jpoint.nl
@bjschrijver Let’s meet Bert Jan Schrijver
@bjschrijver Outline • Definitions • Jenkins 1.x • What’s new in Jenkins 2.0? • Pipeline as code • Pipelines in depth • Jenkins 2.0 in the wild Thanks to @alexsotob and @kohsukekawa!
@bjschrijver Definitions Every	change	goes	through	the build/test	pipeline	and automatically	gets	put	into production. Continuous Deployment An	automated	sequence	of	stages to	deliver	software	from	version control	to	your	users. Pipeline Building	and	testing	software	in such	a	way	that	the	software	can	be released	to	production	at	any	time. Continuous Delivery Team	members	integrate	their	work frequently.	Commits	are	verified	by automated	builds	and	tests. Continuous Integration Who’s	who	in	CI	&	CD
@bjschrijver About Jenkins • De-facto standard tool for automation in software development and beyond • Around for 10+ years, millions of users • Over 100.000 active installations • Jenkins is mission critical for >90% of its users • Version 2.0: first major release in years
@bjschrijver Jenkins: an overview Source: http://www.slideshare.net/asotobu/jenkins-20-65705621
@bjschrijver DEMO
@bjschrijver What’s new in Jenkins 2? • Better out-of-the-box experience • Default set of plugins • Secured by default • Revamped UI • Pipeline as code • In general: more code, less GUI, less state • Drop-in upgrade, backwards compatible w/1.6
@bjschrijver Pipeline as code • Key new feature • Positions Jenkins for: • continuous delivery use cases • other more complex automations of today • Allows to: • describe chain of automation in textual form
 and put it in version control
@bjschrijver Pipeline as code • Grows with you from simple to complex • Handle lots of jobs without repetition • Survives Jenkins restarts • Brings next level of reuse to Jenkins
@bjschrijver My first pipeline node('java8') {
 
 stage('Configure') {
 env.PATH = "${tool 'maven-3.3.9'}/bin:${env.PATH}"
 } 
 
 stage('Checkout') {
 git 'https://github.com/bertjan/spring-boot-sample'
 } 
 
 stage('Build') {
 sh 'mvn -B -V -U -e clean package' 
 } 
 
 stage('Archive') {
 junit allowEmptyResults: true, testResults: '**/target/**/TEST*.xml'
 } 
 
 }
@bjschrijver DEMO
@bjschrijver Pipeline syntax • Built-in syntax and snippet generator • Pipeline reference:
 https://jenkins.io/doc/pipeline/steps • Plugin documentation • If all else fails: dive into the source
@bjschrijver SNIPPETS
@bjschrijver Archive build artifacts archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
@bjschrijver Cleanup old builds properties(
 [buildDiscarder(logRotator(
 artifactDaysToKeepStr: '', 
 artifactNumToKeepStr: '', 
 daysToKeepStr: '', 
 numToKeepStr: '10'))
 ])
@bjschrijver E-mail notification try {
 
 // build steps here
 
 } catch (e) {
 currentBuild.result = "FAILED"
 def subject = 'Build '' + env.JOB_NAME + '' (branch '' + branch + '') ' +
 'failed in Jenkins'
 def body = 'Build log is attached. Details: ' + env.BUILD_URL
 def to = 'email@domain.com'
 mail to: to, subject: subject, body: body, attachLog: true
 throw e
 }
 

@bjschrijver Including files, using constants // File common/Constants.groovy: class Constants {
 static final MAJOR_VERSION_NUMBER = '3.2.1';
 
 static final SONAR_URL = 'https://sonar.my.company.com';
 }
 
 return this;
 // Jenkinsfile: load 'common/Constants.groovy'
 sh "mvn -B -V -U -e sonar:sonar -Dsonar.host.url='${Constants.SONAR_URL}'"
@bjschrijver Re-usable workflow steps // In repo ssh://<username>@<jenkins-url>:2222/workflowLibs.git, // file my.company.package.MyWorkflowSteps package my.company.package
 
 
 def someBuildStep() {
 // Some build step
 }
 
 // In Jenkinsfile: def mySteps = new my.company.package.MyWorkflowSteps()
 mySteps.someBuildStep()

@bjschrijver Parallel run on multiple nodes stage('Checkout') {
 git 'https://github.com/bertjan/spring-boot-sample'
 stash excludes: 'build/', includes: '**', name: 'source'
 } 
 
 stage ('Test') {
 parallel 'unit': {
 node {
 // perform unit test
 unstash 'source'
 sh 'mvn test'
 junit '**/build/test-results/*.xml'
 }
 }, 'integration': {
 node {
 // perform integration test
 unstash 'source'
 sh 'mvn integration-test'
 junit '**/build/test-results/*.xml'
 }
 }
 }
@bjschrijver DEMO
@bjschrijver Jenkins 2.0 in the wild • Upgraded a 1.6 instance with about 50 builds • Replaced all builds with pipelines • Minimal use of Jenkins workflow internal repo • One single Git repo for all builds • Re-usable pipelines and steps • Builds are far more consistent
@bjschrijver Jenkins 2.0: Moving forward • Upcoming changes: focused on ease of use • simplified pipeline model • look less like programming, more declarative • cater both point-and-click and editor people • fails when Jenkins reads it, not when it runs it
@bjschrijver SUMMARY
@bjschrijver Summary • Jenkins 2.0: powerful continuous delivery platform • UI improvements, more curated experience • Pipeline as code: less clicks, more code • Sample code: • https://github.com/bertjan/spring-boot-sample
@bjschrijver@bjschrijver Questions?Questions?
@bjschrijver@bjschrijver Thanks for your time! Got feedback? Tweet it! Please rate this session!

JavaOne 2016 - Pipeline as code