Slide 1

Slide 1 text

© 2018 CloudBees, Inc. All Rights Reserved. Developing Jenkins Pipelines Oleg Nenashev, CloudBees Copenhagen, Oct 18, 2018

Slide 2

Slide 2 text

© 2018 CloudBees, Inc. All Rights Reserved. > whoami @oleg_nenashev oleg-nenashev • Based in Neuchatel, Switzerland • Engineer @ CloudBees • CloudBees Core & CloudBees Jenkins Support • User since 2008, contributor since 2012 • Jenkins Core Maintainer • Jenkins Ambassador: meetups, GSoC, etc. 2

Slide 3

Slide 3 text

© 2018 CloudBees, Inc. All Rights Reserved. Configuration as Code in Jenkins Job Configuration System Configuration 3

Slide 4

Slide 4 text

© 2018 CloudBees, Inc. All Rights Reserved. What is Jenkins Pipeline? Configuration as Code for jobs “Pipeline as Code” 4

Slide 5

Slide 5 text

© 2018 CloudBees, Inc. All Rights Reserved. Jenkins Pipeline is a… 1. Groovy DSL for automatic flow description - Defined in SCM or in Job configuration - 2 syntaxes: Scripted and Declarative // Jenkinsfile node("linux") { git url:"http://github.com/myorg/myproject.git" sh "make all" } http://bit.ly/pipeline-tutorial 5

Slide 6

Slide 6 text

© 2018 CloudBees, Inc. All Rights Reserved. Jenkins Pipeline is a… 1. Groovy DSL for automatic flow description 2. Item type Pipeline Job Multi-Branch Pipeline Organization Folder • GitHub • BitBucket* • … 6

Slide 7

Slide 7 text

© 2018 CloudBees, Inc. All Rights Reserved. Jenkins Pipeline is a… 1. Groovy DSL for automatic flow description 2. Item type 3. Pipeline as Code Ecosystem 7

Slide 8

Slide 8 text

© 2018 CloudBees, Inc. All Rights Reserved. Branch- Source, BlueOce an, ... 89 - explicit support 42 - Pipeline - specific Documentation Samples Development Tools Jenkins Pipeline Ecosystem Plugins: 8

Slide 9

Slide 9 text

© 2018 CloudBees, Inc. All Rights Reserved. Example. CD for Jenkins IRC Bot def imageName = 'jenkinsciinfra/ircbot’ node('docker') { checkout scm def imageTag = findTag() stage('Build ircbot’) { withMavenEnv (["BUILD_NUMBER=${env.BUILD_NUMBER}:${commit}"]) { sh 'make bot' // Make invokes Maven }} stage ('Build & Deploy container’) { def img = docker.build("${imageName}:${imageTag}”) img.push() } } https://github.com/jenkins-infra/ircbot Docker Registry Server Jenkins SCM Puppet 9

Slide 10

Slide 10 text

© 2018 CloudBees, Inc. All Rights Reserved. More about Jenkins Pipeline http://bit.ly/pipeline-docs http://bit.ly/pipeline-compatibility 10

Slide 11

Slide 11 text

© 2018 CloudBees, Inc. All Rights Reserved. My Jenkins Pipelines 11 Scripted Pipeline - Minimal Jenkinsfile Pipeline Libraries

Slide 12

Slide 12 text

© 2018 CloudBees, Inc. All Rights Reserved. 12 Pipeline Shared Libraries Libs are located in SCM Versioning Encapsulation Visibility scopes

Slide 13

Slide 13 text

© 2018 CloudBees, Inc. All Rights Reserved. Library usage 1. Take library components 13

Slide 14

Slide 14 text

© 2018 CloudBees, Inc. All Rights Reserved. Library usage 1. Take library components 2. Build your own bicycle Pipeline Add some glue! 14

Slide 15

Slide 15 text

© 2018 CloudBees, Inc. All Rights Reserved. Pipeline Development Tools 15

Slide 16

Slide 16 text

© 2018 CloudBees, Inc. All Rights Reserved. IDE Integration Unit Test Framework Static Analysis Library manager Debug tools Documen- tation Pipeline deployment Freestyle to Pipeline Converter Good Missing 16 Partial

Slide 17

Slide 17 text

© 2018 CloudBees, Inc. All Rights Reserved. IDE Integration https://st-g.de/2016/08/jenkins- pipeline-autocompletion-in-intellij 17

Slide 18

Slide 18 text

© 2018 CloudBees, Inc. All Rights Reserved. Documentation (root) +- README.md +- LICENSE.txt +- … +- src +- org +- oleg +- MailExtSender.groovy +- vars +- cobertura.groovy +- covertura.txt +- runTests.groovy +- runTests.txt Documentation as Code? Asciidoc / Markdown 18

Slide 19

Slide 19 text

© 2018 CloudBees, Inc. All Rights Reserved. Documentation (root) +- README.md +- LICENSE.txt +- … +- src +- org +- oleg +- MailExtSender.groovy +- vars +- cobertura.groovy +- covertura.txt +- runTests.groovy +- runTests.txt Asciidoc / Markdown Javadoc/Groovydoc Standard tools work for Classes … but not for Global Variables https://github.com/jenkinsci/gmaven/pull/1 https://github.com/jenkins-infra/pipeline- library/pull/22 19

Slide 20

Slide 20 text

© 2018 CloudBees, Inc. All Rights Reserved. Pipeline Testing https://github.com/jenkinsci/JenkinsPipelineUnit “mvn verify” has been invoked… And the execution passed… For the source Jenkinsfile… 20

Slide 21

Slide 21 text

© 2018 CloudBees, Inc. All Rights Reserved. Pipeline Testing. Libraries String clonePath = 'path/to/clone' def library = library() .name('commons') .retriever(gitSource('git@orgithub/devteam/jenkins-shared.git')) .targetPath(clonePath) .defaultVersion("master") .allowOverride(true) .build() helper.registerSharedLibrary(library) // Same as before… loadScript("job/library/exampleJob.jenkins") printCallStack() … 21

Slide 22

Slide 22 text

© 2018 CloudBees, Inc. All Rights Reserved. NEW: Pipeline Spock https://github.com/homeaway/jenkins-spock Checks: • Steps are callable • Global variables are accessible • Shared Lib Global vars are accessible + Mocking of Pipeline objects for unit tests 22

Slide 23

Slide 23 text

© 2018 CloudBees, Inc. All Rights Reserved. So, how to develop Pipeline? … without common tools? 23

Slide 24

Slide 24 text

© 2018 CloudBees, Inc. All Rights Reserved. Patch => Commit => Launch 24

Slide 25

Slide 25 text

© 2018 CloudBees, Inc. All Rights Reserved. Patch => Commit => Launch => Patch => Commit => Launch => Patch => Commit => Launch => ….? 25

Slide 26

Slide 26 text

© 2018 CloudBees, Inc. All Rights Reserved. Patch => Commit => Launch => Patch => Commit => Launch => Patch => Commit => Launch => ….? 26

Slide 27

Slide 27 text

© 2018 CloudBees, Inc. All Rights Reserved. 27

Slide 28

Slide 28 text

© 2018 CloudBees, Inc. All Rights Reserved. How do I work? Intellij IDEA • Syntax file • Built-in documentation Jenkins Test Instance Source Code local .git repo(s) • Production System Snapshot • Custom Pipeline Library Settings • Filesystem SCM plugin • Repositories for libraries • Jenkinsfile repos volume 28

Slide 29

Slide 29 text

© 2018 CloudBees, Inc. All Rights Reserved. Jenkins Master Docker + Configuration as Code (Groovy hooks and JCasC) + Init script tweaks 29

Slide 30

Slide 30 text

© 2018 CloudBees, Inc. All Rights Reserved. Mapping Pipeline Filesystem SCM Plugin 30

Slide 31

Slide 31 text

© 2018 CloudBees, Inc. All Rights Reserved. Test Jobs • Jobs can generated with Configuration-as-Code • Demo scripts come from the Pipeline library • Groovy scripts for dynamic loading 31

Slide 32

Slide 32 text

© 2018 CloudBees, Inc. All Rights Reserved. Live demo https://github.com/oleg-nenashev/demo-jenkins- config-as-code 32

Slide 33

Slide 33 text

© 2018 CloudBees, Inc. All Rights Reserved. How could I make the demo better? • Jenkins Configuration as Code Plugin • Custom WAR Packager • // to be presented tomorrow 33

Slide 34

Slide 34 text

© 2018 CloudBees, Inc. All Rights Reserved. What do I need? Dev Tools for Pipeline • IDE, Debugger, Test Tools Community Pipeline Libraries • Support common use-cases without plugins • Pipeline-only plugins work too 34

Slide 35

Slide 35 text

© 2018 CloudBees, Inc. All Rights Reserved. Takeaways There are development tools Pipeline development is possible Hacks are required 35

Slide 36

Slide 36 text

© 2018 CloudBees, Inc. All Rights Reserved. Useful links These slides: • https://speakerdeck.com/onenashev (to be published) Demo: • https://github.com/oleg-nenashev/demo-jenkins- config-as-code 36

Slide 37

Slide 37 text

© 2018 CloudBees, Inc. All Rights Reserved. Thank You! www.cloudbees.com Company Confidential