pipeline: continuous delivery as code in jenkins 2.0

36
© 2016 CloudBees, Inc. All Rights Reserved © 2016 CloudBees, Inc. All Rights Reserved

Upload: jules-pierre-louis

Post on 16-Apr-2017

455 views

Category:

Software


0 download

TRANSCRIPT

©2016Clou

dBees,Inc.A

llRightsReserved

©2016CloudBees,Inc.AllRightsReserved

©2016Clou

dBees,Inc.A

llRightsReserved

©2016CloudBees,Inc.AllRightsReserved

Continuous Delivery as Code

Pipeline and Jenkins 2

©2016Clou

dBees,Inc.A

llRightsReserved

Continuous Delivery

Continuous delivery (CD) is a software engineering approach in which teams produce software in short cycles, ensuring that the software can be reliably released at any time. It aims at building, testing, and releasing software faster and more frequently. The approach helps reduce the cost, time, and risk of delivering changes by allowing for more incremental updates to applications in production. A straightforward and repeatable deployment process is important for continuous delivery.

https://en.wikipedia.org/wiki/Continuous_delivery

©2016Clou

dBees,Inc.A

llRightsReserved

Meet Jenkins 2

jenkins.io/2.0

©2016Clou

dBees,Inc.A

llRightsReserved

Jenkins 2 Themes

Pipeline as code: front and center

Improved “out of the box” user experience

User interface improvements

©2016Clou

dBees,Inc.A

llRightsReserved

Jenkins 2 Themes

Pipeline as code: front and center

Improved “out of the box” user experience

User interface improvements

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline as code

• Introduce “Pipeline” as a new type in Jenkins • Codify an implicit series of stages into an explicit

pipeline definition (Jenkinsfile) in source control • Resumability/durability of pipeline state • Extend the DSL to meet organizational needs

©2016Clou

dBees,Inc.A

llRightsReserved

Creating a Pipeline

©2016Clou

dBees,Inc.A

llRightsReserved

Creating a Pipeline

©2016Clou

dBees,Inc.A

llRightsReserved

Creating a Pipeline

©2016Clou

dBees,Inc.A

llRightsReserved

Creating a Pipeline

©2016Clou

dBees,Inc.A

llRightsReserved

Jenkinsfile

node('java8'){

stage('Checkout'){

checkoutscm

}

stage('Build'){

sh'mvncleaninstall'

}

stage('Test'){

sh'mvntest'

}

archiveArtifactsartifacts:'target/**/*.jar',fingerprint:true

}

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Documentation

jenkins.io/doc

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Documentation

©2016Clou

dBees,Inc.A

llRightsReserved

Snippet Generator localhost:8080/job/niagara/pipeline-syntax

©2016Clou

dBees,Inc.A

llRightsReserved

Snippet Generator localhost:8080/job/niagara/pipeline-syntax

©2016Clou

dBees,Inc.A

llRightsReserved

DSL Reference localhost:8080/job/niagara/pipeline-syntax/html

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Plugins

Extending Pipeline

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Stage View plugin

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Multibranch plugin

• Represent pipelines for multiple branches in one "Folder" • Automatically scan a repository for each branch which contains a Jenkinsfile

©2016Clou

dBees,Inc.A

llRightsReserved

GitHub Organization Folder plugin

©2016Clou

dBees,Inc.A

llRightsReserved

Blue Ocean jenkins.io/projects/blueocean

©2016Clou

dBees,Inc.A

llRightsReserved

Docker Pipeline plugindefimageName='jenkinsciinfra/bind'node('docker'){ checkoutscm

//Computeauniqueimagetag defimageTag="build-${env.BUILD_NUMBER}"

//The`docker`variableintroducedbytheplugin stage('Build'){ defwhale=docker.build("${imageName}:${imageTag}")

}

//PublishthisimagetoDockerHub stage('Deploy'){ whale.push()

}}

©2016Clou

dBees,Inc.A

llRightsReserved

Docker Pipeline pluginnode('docker'){

//The`docker`variableintroducedbytheplugin.////InvokingourGradlebuildinsideafreshlyspunup//DockercontainerwithJDK8docker.image('openjdk:8-jdk').inside{ checkoutscm sh'./gradlew--info' archiveArtifactsartifacts:'build/libs/**/*.jar',fingerprint:true}

}

©2016Clou

dBees,Inc.A

llRightsReserved

Integration with existing plugins

Pipeline + ??? = Profit.

©2016Clou

dBees,Inc.A

llRightsReserved

Plugins that support Pipeline

• A few plugins which provide custom steps for Pipeline scripts: – Tooling: Credentials binding, SSH Agent, Parallel Test – Reporters: JUnit, Brakeman, HTML Publisher, Cucumber Test Result – Notifiers: Slack, HipChat, email-ext – Wrappers: Timestamper

• Plugins work within Pipelines – SCM: Git, SVN, Mercurial, P4, CVS – Wrappers: Ansi Color, NodeJS – Build steps: Ant, etc

• More defined in COMPATIBILITY.md

©2016Clou

dBees,Inc.A

llRightsReserved

Steps added by pluginsnode{

/*otherworkhappeneduphere*/

timestamps{

sh'mvncleanpackage'

junit'**/target/surefire-reports/*.xml'

}

if(env.BRANCH_NAME=='master'){

sshagent(credentials:['my-credential-id']){

sh'./run-ssh-deploy-script'

}

}

}

©2016Clou

dBees,Inc.A

llRightsReserved

Build Steps

©2016Clou

dBees,Inc.A

llRightsReserved

Build Wrappers

github.com/jenkinsci/pipeline-examples

©2016Clou

dBees,Inc.A

llRightsReserved

Abstracting Pipeline

Sharing code, best practices and templates for success

©2016Clou

dBees,Inc.A

llRightsReserved

Pipeline Shared Libraries

//Thecall()methodinanyfileinpipeline-library.git/varsisexposedasa //methodwiththesamenameasthefile.defcall(defmavenOptions){

docker.image('maven').inside{ timestamps{

sh"mvncleaninstall-Dmaven.test.failure.ignore=true${mavenOptions}"

}junit'**/target/surefire-reports/**/*.xml'

}}

runMaven.groovy

github.com/jenkinsci/pipeline-examples

©2016Clou

dBees,Inc.A

llRightsReserved

node{ checkoutscm stage('Build'){

//LoadsrunMavenstepfromtheSharedLibraryandinvokesit. runMaven } stage('AcceptanceTests'){ git'git://git.example.com/selenium-tests.git' sh'./run-selenium.sh' } stage('Deploy'){ sh'./deploy.sh' }}

Pipeline Shared LibrariesJenkinsfile

github.com/jenkinsci/pipeline-examples

©2016Clou

dBees,Inc.A

llRightsReserved

load step

//Methodsinthisfilewillendupasobjectmethodsontheobjectthatloadreturns.deflookAtThis(StringwhoAreYou){echo"Lookatthis,${whoAreYou}!Youloadedsomethingfromanotherfile!"}

externalMethod.groovy

github.com/jenkinsci/pipeline-examples

//Ifthere'sacallmethod,youcanjustloadthefile,say,as"foo",andtheninvokethatcallmethodwithfoo(...)defcall(StringwhoAreYou){echo"${whoAreYou},saythankstothecall(...)method."}

externalCall.groovy

node{//Loadthefilefromthecurrentdirectory,//intoavariablecalled"externalMethod"defexternalMethod=load("externalMethod.groovy")//CallthemethodwedefinedexternalMethod.lookAtThis("Steve")//Nowload'externalCall.groovy'.defexternalCall=load("externalCall.groovy")//Wecanjustrunitwith`externalCall() ̀externalCall("Steve")}

Jenkinsfile

©2016Clou

dBees,Inc.A

llRightsReserved

Continuous Delivery as Code

Final Thoughts

©2016Clou

dBees,Inc.A

llRightsReserved

• A new pipeline “type” has been introduced • Previously implicit pipelines, chained jobs, can now be

made explicit and committed to SCM • Pipelines have state associated with them and can be

resumed • It’s code. It can be modified and extended

Final Thoughts

©2016Clou

dBees,Inc.A

llRightsReserved

©2016CloudBees,Inc.AllRightsReserved

Questions/Answers

jenkins.io/2.0 jenkins.io/doc @jenkinsci