39
Continuous Delivery Pipeline as code

Continuous Delivery - Pipeline as-code

Embed Size (px)

Citation preview

Page 1: Continuous Delivery - Pipeline as-code

Continuous Delivery Pipeline as code

Page 2: Continuous Delivery - Pipeline as-code

Mike van VendelooSoftware Craftsman at JPoint

TU Delft - Computer Science

Scrum master

Assignments at customers like KLM, Rabobank, Government, de Persgroep.

Focus on improvement of software development process and quality

Hobb: Korfball, domotica

@mikevanvendeloo

Page 3: Continuous Delivery - Pipeline as-code

Agenda• Definitions CI/CD/Pipeline

• Jenkins 2: Pipeline as code

• Pipeline snippets

• Reuse with pipeline libraries

• Declarative pipelines

• Blue Ocean

• Demo

Page 4: Continuous Delivery - Pipeline as-code

Definitions

Page 5: Continuous Delivery - Pipeline as-code

Definitions - Continuous Integration“Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible.“

Martin Fowler

Page 6: Continuous Delivery - Pipeline as-code

Continuous Delivery: Automate everything“If it hurts, do it more frequently, and bring the pain forward.” ― Jez Humble, Continuous Delivery

Page 7: Continuous Delivery - Pipeline as-code

Definitions - Continuous Delivery“Continuous Delivery is the ability to get changes of all types—including new features, configuration changes, bug fixes and experiments—into production, or into the hands of users, safely and quickly in a sustainable way.”

Page 8: Continuous Delivery - Pipeline as-code

Definitions - Continuous Deployment

Page 9: Continuous Delivery - Pipeline as-code

Definitions - PipelineA pipeline is a set of stages to bring functionality from developer to the end user.

Page 10: Continuous Delivery - Pipeline as-code

Jenkins pipeline as code

Page 11: Continuous Delivery - Pipeline as-code

Jenkins 1

Page 12: Continuous Delivery - Pipeline as-code

Jenkins 2 - Pipeline as code

Page 13: Continuous Delivery - Pipeline as-code

Sample basic pipeline stages➔ Checkout

Source code checkout from repository

➔ BuildCompile & Unit test

➔ QACode style check & integration testing

➔ DeployDeploy to a server

Page 14: Continuous Delivery - Pipeline as-code

Basic pipeline definition#!/usr/bin/groovy

node('linux') { stage('Checkout') { checkout scm }

stage('Build') { sh "mvn clean deploy" junit allowEmptyResults: true, testResults: 'target/surefire-reports/*.xml' }

stage('Deploy') { build "Deployer" }}

Page 15: Continuous Delivery - Pipeline as-code
Page 16: Continuous Delivery - Pipeline as-code

Pipeline snippets

Page 17: Continuous Delivery - Pipeline as-code

Error handlingnode { stage('Doing my thing') { try { sh 'exit 1' } catch (someException) { echo 'Something failed, somebody should be notified!' throw someException } finally {

hipchatSend color: 'BLUE', credentialId: 'hipChat', message: “Build result ${env.BUILD_RESULT}”, room: 'BuildResults' } }}

Page 18: Continuous Delivery - Pipeline as-code

Pipeline properties - cleanupproperties( [buildDiscarder( logRotator( artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '7', numToKeepStr: '25') ), pipelineTriggers([]) ])

Page 19: Continuous Delivery - Pipeline as-code

Pipeline properties - Parametersproperties( [parameters( [choice( choices: ['dev', 'test', 'acc', 'prod'], description: 'Kies de omgeving', name: 'omgeving')] ), pipelineTriggers([]) ])

Page 20: Continuous Delivery - Pipeline as-code

Parallel tasksparallel ‘test’: { sh ‘mvn clean test’ }, ‘mutation-test’: { Sh ‘mvn org.pitest:pitest-maven:mutationCoverage’ }, failFast: true

Page 21: Continuous Delivery - Pipeline as-code

Share information between nodes/executorsnode { stage(‘Build’) sh(“mvn -B clean package”) stash excludes:’target/’, includes: ‘**’, name: ‘source’}

stage (‘test’) { unstash ‘source’ }}

Page 22: Continuous Delivery - Pipeline as-code

Notificationsdef notify(String colorCode, String color, String summary) slackSend (color: colorCode, message: summary)

hipchatSend (color: color, notify: true, message: summary)

emailext (

to: [email protected]',

subject: “Build ${env.JOB_NAME} [${env.BUILD_NUMBER}] result ${currentBuild.result}”

body: “<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>”,

recipientProviders: [[$class: 'DevelopersRecipientProvider']]

)

}

Page 23: Continuous Delivery - Pipeline as-code

Pipeline libraries

Page 24: Continuous Delivery - Pipeline as-code

Reuse between pipelines: libraries

Page 25: Continuous Delivery - Pipeline as-code

Library implementationpackage vanvendeloo.jenkins

def checkout(String repositoryName) { git “[email protected]:vanvendeloo/${repositoryName}”}def updateVersion() {

sh “mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion} versions:commit”}// Return the contents of this script as object so it can be re-used in Jenkinsfiles.return this

Page 26: Continuous Delivery - Pipeline as-code

Use the library: @Library@Library(‘pipeline-library’)

node { pipelineSteps = new PipelineSteps() stage(‘Preparation’) { pipelineSteps.checkout(‘my-service’) pipelineSteps.updateVersion() }}

Page 27: Continuous Delivery - Pipeline as-code

Catch: Groovy Sandbox

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: ↵ Scripts not permitted to use method java.lang.String replaceAll java.lang.String java.lang.String

1. Go to Manage Jenkins > In-process Script Approval

2. Review the pending signatures and click Approve to add them to the whitelist

3. Re-run your job; it should no longer fail (for this particular method call)

Page 28: Continuous Delivery - Pipeline as-code

Declarative pipelines

Page 29: Continuous Delivery - Pipeline as-code

Declarative Pipelinesnode { stage(‘Checkout’) { checkout scm } stage(‘Build’) { withEnv(["PATH+MAVEN=${tool 'M3'}/bin"]) { try { sh 'mvn clean install' } catch (e) { currentBuild.result = 'FAILURE' }

} }

pipeline { agent any tools { maven 'M3' } stages { stage('Checkout') { steps { checkout scm } } stage(Build) { steps { sh ‘mvn clean install’ } post { success { junit '**/surefire-reports/**/*.xml' } } } }

Page 30: Continuous Delivery - Pipeline as-code

Blue Ocean

Page 31: Continuous Delivery - Pipeline as-code

Blue Ocean

Page 32: Continuous Delivery - Pipeline as-code

Pipeline result

Page 33: Continuous Delivery - Pipeline as-code
Page 34: Continuous Delivery - Pipeline as-code
Page 35: Continuous Delivery - Pipeline as-code
Page 36: Continuous Delivery - Pipeline as-code

ConclusionsPipeline evaluates with your code

Reuse in libraries keeps your Jenkinsfile clean and simple

BlueOcean definately an improvement, but need to get used to it

Pipeline editor very cool feature!

Page 37: Continuous Delivery - Pipeline as-code
Page 38: Continuous Delivery - Pipeline as-code

http://github.com/mikevanvendeloo

https://jenkins.io/doc/book/pipeline/syntax/

https://jenkins.io/blog/2017/02/07/declarative-maven-project/

Resources

Page 39: Continuous Delivery - Pipeline as-code

@mikevanvendeloo