pipeline as code - doag.org · • what’s new in jenkins 2? • pipeline as code ... • groovy...

36
Pipeline as code Building Continuous Delivery Pipelines with Jenkins 2 Bert Jan Schrijver @bjschrijver [email protected]

Upload: duongtram

Post on 01-Sep-2018

268 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

Pipeline as codeBuilding Continuous Delivery Pipelines with Jenkins 2

BertJanSchrijver@[email protected]

Page 2: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Let’s meet

Bert Jan Schrijver

Page 3: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Outline• Definitions • Jenkins 1.x • What’s new in Jenkins 2? • Pipeline as code • Pipelines in depth • Jenkins 2 in the wild

Thanks to @alexsotob and @kohsukekawa!

Page 4: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Definitions

Everychangegoesthroughthebuild/testpipelineandautomaticallygetsputintoproduction.

Continuous Deployment

Anautomatedsequenceofstagestodeliversoftwarefromversioncontroltoyourusers.

PipelineBuildingandtestingsoftwarein

suchawaythatthesoftwarecanbereleasedtoproductionatanytime.

Continuous Delivery

Teammembersintegratetheirworkfrequently.Commitsareverifiedby

automatedbuildsandtests.

Continuous Integration

Who’swhoinCI&CD

Page 5: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

About Jenkins• De-facto standard tool for automation in software

development and beyond • Around for 10+ years, millions of users • Over 120.000 active installations • Jenkins is mission critical for >90% of its users • Version 2: first major release in years

Page 6: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Jenkins: an overview• TODO: Jenkins overview slide uit talk Alex Soto

Source: http://www.slideshare.net/asotobu/jenkins-20-65705621

Page 7: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

DEMO

Page 8: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@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

Page 9: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@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

Page 10: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@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

Page 11: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Scripted vs. declarative pipelines• Scripted

• more flexible, better reuse, compact • Declarative

• No Groovy experience necessary • Syntax checking • Linting via API and CLI • Visual editor (beta)

Page 12: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

My first scripted pipelinenode('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' } }

Page 13: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

My first declarative pipelinepipeline { agent { node { label 'java8' } } tools { maven 'maven-3.3.9' } stages { stage('Checkout') { steps { git 'https://github.com/bertjan/spring-boot-sample' } } stage('Build') { steps { sh 'mvn -B -V -U -e clean package' } } stage('Archive') { steps { junit(testResults: '**/target/**/TEST*.xml', allowEmptyResults: true) } } } }

Page 14: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

DEMO

Page 15: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Page 16: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Page 17: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Page 18: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Page 19: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

DEMO

Page 20: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Pipeline syntax• Built-in syntax and snippet generator • Groovy DSL definition (GDSL file) for IDE • Pipeline reference:

https://jenkins.io/doc/pipeline/steps • Plugin documentation • If all else fails: dive into the source

Page 21: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

SNIPPETS

Page 22: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Archive build artifacts

archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true

Page 23: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Cleanup old builds

properties( [buildDiscarder(logRotator( artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10')) ])

Page 24: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

E-mail notificationtry { // 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 protected]' mail to: to, subject: subject, body: body, attachLog: true throw e }

Page 25: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@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}'"

Page 26: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Re-usable workflow steps// In file <some git repo>/src/my/company/package/SomeLibrary.groovypackage my.company.packagedef someBuildStep() { // Some build step}

// In Jenkinsfile: @Library('pipeline-library') import my.company.package.*

def myLibrary = new SomeLibrary()myLibrary.someBuildStep()

Page 27: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Re-usable workflow steps

Page 28: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Parallel run on multiple nodesstage('Checkout') { git 'https://github.com/bertjan/spring-boot-sample' stash excludes: 'build/', includes: '**', name: 'source'} stage ('Test') { parallel 'unit': { node { // run unit tests unstash 'source' sh 'mvn test' junit '**/build/test-results/*.xml' } }, 'integration': { node { // run integration tests unstash 'source' sh 'mvn integration-test' junit '**/build/test-results/*.xml' } }}

Page 29: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

DEMO

Page 30: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Jenkins 2 in the wild• Upgraded a 1.6 instance with about 50 builds • Replaced all builds with pipelines • One single Git repo for all builds • Re-usable pipeline library • Builds are far more consistent

Page 31: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Jenkins 2: 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

Page 32: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver@bjschrijver

SUMMARY

Page 33: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Summary• Jenkins 2: powerful continuous delivery platform • UI improvements, more curated experience • Scripted pipelines: less clicks, more code • Declarative pipelines: syntax checking, visual editor

Page 34: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

20

JAVA MAGAZINE

JAVA MAGAZINE | 06 2016

PIPELINE AS CODE

Op JavaOne vertelde Kohsuke Kawaguchi, de maker van Jenkins, dat er meer dan 120.000 actieve installaties zijn. Voor meer dan 90% van de gebruikers is Jenkins “mission critical”. Jenkins is dus niet zomaar een hobbypro-jectje. Een belangrijke factor in het succes van Jenkins is de enorme verzameling plug-ins, die beschikbaar zijn. Er is een heel ecosys-teem ontstaan, waarmee Jenkins in vrijwel elke omgeving kan worden ingezet. Nieuw in Jenkins 2Jenkins 2 is een drop-in upgrade, volledig backward compatible met versie 1.6, met drie grote wijzigingen.

Betere out-of-the-box ervaringVroeger was Jenkins standaard vrij beperkt. Je moest veel plug-ins installeren, voordat je aan de gang kon. Dat is nu beter geregeld. Je kunt tijdens de installatie kiezen voor een aanbevolen set plug-ins, waardoor je direct aan de slag kunt.

Security is nu standaard. Bij de eerste keer opstarten wordt er een initieel admin wacht-

woord gezet, dat je uit de logs moet vissen. Dat is iets meer werk vergeleken met vroeger waar alles open stond, maar het is wel een stuk veiliger. Zeker als je te weten komt, dat er bots zijn die het internet af scannen naar onbeveiligde Jenkins-instanties.

Opgefriste GUIDe GUI van Jenkins is licht verbeterd ten opzichte van Jenkins 1.x. Bij installatie en upgrade krijg je nu een wizard voorgeschoteld, die je helpt bij de configuratie. Eenmaal ge-installeerd zijn de wijzigingen marginaal. Het aanmaken van een nieuwe job ziet er iets an-ders uit en bij het configureren van jobs heb je nu tabjes, waarmee je snel naar een bepaalde sectie kunt springen. De echt grote GUI-wijzigingen zitten in project “Blue Ocean”, waarover je verderop meer kunt lezen.

Pipeline as codeVeruit de grootste wijziging in Jenkins 2 is het pipeline as code concept. Een pipeline is een geautomatiseerde reeks van stappen, die software vanuit versiebeheer naar de handen van je gebruikers brengt. Met pipeline as code

Pipeline as codeContinuous Delivery pipelines met Jenkins 2In de afgelopen tien jaar heeft Jenkins zich ontpopt tot de standaardtool voor automatisering

in software development. Dit jaar is de eerste grote Jenkins release sinds tijden uitgekomen:

Jenkins 2.0. In dit artikel lees je wat er allemaal nieuw is.

Bert Jan Schrijver is software craftsman bij JPoint en is momenteel werkzaam bij de Nationale Politie

Listing 1: pipeline script voor een Maven project

Afbeelding 2: visualisatie van een Jenkins pipeline

Afbeelding 1: Het Jenkins ecosysteem. Bron: [1]

kun je pipelines in tekstuele vorm beschrijven en in versiebeheer bewa-ren. Deze pipelines schrijf je met een flexibele Groovy DSL.

In Jenkins 1 werkte het maken van pipelines niet echt optimaal, zeker niet in een microservice-omgeving als je veel losse jobs hebt voor build, test en deploy. Een project met 20 services leverde dan al gauw een stuk of 100 Jenkins jobs op.

Met Jenkins 2 pipelines werkt dat een stuk praktischer om twee redenen:1. Je kunt jobs precies zo samenstel-len als je zelf wilt. Een enkele job kan een complete pipeline van commit tot en met productie-deployment afhandelen.2. Hergebruik van onderdelen van jobs is veel eenvoudiger. Hierover verderop meer.

Jenkins wordt hiermee gepositio-neerd voor continuous delivery use cases en voor andere meer complexe automatiserings-scenario’s.

PipelinesJe kunt een Jenkins 2 pipeline op twee manieren definiëren: door de pipeline in de Jenkins GUI in te tik-ken of door een bestand met daarin de pipeline-definitie in versiebeheer te plaatsen. Voor de laatste optie is het een conventie om een “Jenkinsfile” in de root van het pro-ject te zetten waar de job betrekking op heeft. Dit is vergelijkbaar met de Dockerfile of een Maven

pom.xml: een standaard configuratie-file op een standaard plek.

Als je in je IDE aan Jenkinsfiles werkt, dan kun je een GDSL file importeren [2]. Dit bestand regelt de highlighting en code completion in je IDE.

Een voorbeeldHet volgende pipeline script bouwt een eenvoudig Maven project (zie Listing 1).

De eerste stap is het kiezen van het type Jenkins node waar de build op moet gaan draaien. In het eenvoudig-ste scenario heb je alleen een Jenkins “master” node. In wat complexere omgevingen zie je vaak een master node en een aantal tot zelfs tientallen slave nodes om veel builds tegelijker-tijd te kunnen draaien.

In dit voorbeeld geven we aan dat de job op een node met label ‘java8’ moet draaien. Jenkins kiest dan de

eerstvolgende beschikbare node waar dit label aan toegekend is. De hele pipeline draait nu op deze ‘java8’ node. Je kunt er ook voor kiezen om verschillende onderdelen op ver-schillende nodes en zelfs parallel te draaien.

De pipeline in dit voorbeeld kent vier “stages”. Een stage is een stap in een pipeline, die als zodanig herkenbaar is in de Jenkins GUI. In de eerste stage zoeken we geïnstalleerde tool ‘maven-3.3.9’ op en maken deze beschikbaar door hem in de path-variabele van het OS te zetten. De tweede stap is een Git checkout, de derde een Maven build en de vierde ar-chiveert de resultaten van de unit tests.Als we de pipeline uitvoeren, zien we de volgende resultaten terug in de Jenkins GUI (zie Afbeelding 2).

De praktijk leert dat pipelines meegroeien van eenvoudig naar com-plex. Met gecodeerde pipelines kun je meerdere jobs definiëren zonder jezelf te herhalen. Dit is een groot voordeel ten opzichte van traditionele, point-and-click pipelines.

Pipelines overleven een herstart van Jenkins. Dat is handig als je af en toe een upgrade van Jenkins wilt uitvoe-ren, waarvoor een herstart nodig is. Jenkins bevat daarnaast de mogelijk-heid om een pipeline te “replayen”, waarbij je via de GUI kleine wijzigingen kunt doen en de pipeline vervolgens opnieuw kan uit-voeren. Dat is erg handig bij het ont-wikkelen van gecodeerde pipelines.

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' }

}

• https://github.com/bertjan/spring-boot-sample • https://github.com/politie/pipeline-library • https://www.voxxed.com/blog/2017/01/pipeline-as-

code-with-jenkins-2/ • https://github.com/bertjan/javamagazine (Dutch)

Resources

Page 35: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

@bjschrijver

Questions?Questions?

Page 36: Pipeline as code - doag.org · • What’s new in Jenkins 2? • Pipeline as code ... • Groovy DSL definition ... //jenkins.io/doc/pipeline/steps • Plugin documentation

Thanks for your time.Gotfeedback?Tweetit! @bjschrijver

Allpicturesbelongtotheirrespective

authors