Large-Scale Automation with Jenkins
Kohsuke Kawaguchi / kk@kohsuke.org / @kohsukekawa
Architect, CloudBees, Inc.




                 ©2010 CloudBees, Inc. All Rights
                           Reserved
Have you met Jenkins?
http://jenkins-ci.org/


• #1 OSS CI server
      – Written in Java
      – 47K+ installations
• About 7 years old
• Two key emphasis
      – Easy to install/use
      – Extensible via 600+ plugins



                         ©2010 CloudBees, Inc. All Rights   2
                                   Reserved
Context
• First steps in Jenkins usage
  – Build your project
  – Run tests on your project
  – Maybe run code analysis tools




                Where to go from here?
                ©2010 CloudBees, Inc. All Rights   3
                          Reserved
Workflow!
• By which I mean interconnected jobs

• Why?
  – Lots of manual process
  – Dividing by the responsibility boundary
  – Single job isn’t meant for complex things
  – Reusing pieces



                 ©2010 CloudBees, Inc. All Rights   4
                           Reserved
Parameterized Builds
• Plain jobs can be thought of like a
  procedure without any input
    void buildAcmeLibrary() { … }


• Ability to pass parameters make it more
  useful
    void buildAcmeLibrary(targetPlatform) { … }



                    ©2010 CloudBees, Inc. All Rights   5
                              Reserved
Three Things You Need To Do
• #1: Define Parameters




 Lots of different parameter types




                           ©2010 CloudBees, Inc. All Rights   6
                                     Reserved
Three Things You Need To Do
• #2: Refer to parameter values
  – As variable expansions: ${Browser}
  – As environment variables from your builds
  – Some parameter types expose data in
    different ways
    • File parameter




                  ©2010 CloudBees, Inc. All Rights   7
                            Reserved
Three Things You Need To Do
• #3: Specify actual values when you run




               ©2010 CloudBees, Inc. All Rights   8
                         Reserved
Parameterized Trigger Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin


• Call other jobs (with parameters)
     – Wait for their completions (optional)
                                                          Foo


                      Sub1                              Sub2                  Sub3


                                                         Foo


                                                          Sub1

                                                          Sub2


                                           ©2010 CloudBees, Inc. All Rights          9
                                                     Reserved
Multi-Configuration Project
• You often do the same thing with slight
  variations
  – Compile C++ code for different platforms
  – Test with different browsers
  – Or more generally, think of it as

        for (x in [a,b,c]) {
          for (y in [d,e,f]) {
            doSomethingWith(x,y,...);
          }
        }


                     ©2010 CloudBees, Inc. All Rights   10
                               Reserved
Model
• Define axes
  – One axis ≈ one for loop

• Choose from pre-defined types of Axis
  – Generic axis: arbitrary values exposed as
    environment variables
  – Slave axis: pick slaves by their names or
    their labels
    • e.g., linux, solaris, and windows
  – JDK axis

                   ©2010 CloudBees, Inc. All Rights   11
                             Reserved
Multi-Configuration Project Gimmicks
• Filtering
   – Otherwise combinations increase exponentially
   – Not all combinations make sense
   – Use boolean expression to trim down the size
      (label==“windows”).implies(browser==“iexplore”) &&
      (label==“mac”).implies(browser==“safari”)

   – Or tell Jenkins to cut the workload to N%
      • Jenkins will thin out the combinations by itself




                        ©2010 CloudBees, Inc. All Rights   12
                                  Reserved
Demo




       ©2010 CloudBees, Inc. All Rights
                 Reserved
Other Simple Choreography Tools
https://wiki.jenkins-ci.org/display/JENKINS/Join+Plugin


• Join Plugin




                                            ©2010 CloudBees, Inc. All Rights   14
                                                      Reserved
When Jobs Start Working Together…
https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin


• Copy Artifact Plugin
     – Copy artifacts into a workspace
     – By using various criteria


                     acme-build #15                                               acme-test #24



                                     foo.jar                                               foo.jar




                                               ©2010 CloudBees, Inc. All Rights                      15
                                                         Reserved
Copy Artifact vs External Repository
• Almost as if artifacts are versioned
     acme-build #10
                                             Last saved build
        acme-build #11
           acme-build #12
                                                      Last stable build
              acme-build #13
                 acme-build #14
                    acme-build #15



                                 foo.jar




                       ©2010 CloudBees, Inc. All Rights                   16
                                 Reserved
Labeling Builds Is Useful
• Especially when labels have semantics
                                         Deployed to production

      acme-build #10
                                                Signed off to QA
         acme-build #11
            acme-build #12
                                                        Last stable build
               acme-build #13
                  acme-build #14
                     acme-build #15



                                   foo.jar




                     ©2010 CloudBees, Inc. All Rights                       17
                               Reserved
Labeling Builds Is Useful
• More so when you automate them
• Take “Signed off to QA” label for example

                                                   acme-devtest #15
     acme-build #12

               foo.jar
                                                  acme-findbugs #22


             Signed off to QA




                         ©2010 CloudBees, Inc. All Rights             18
                                   Reserved
Introducing Promoted Builds Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin


• Promotion = act of giving a build a label
• You specify:
     – Promotion criteria
     – what happens after promotion


• Label is a nice hand-off between teams
     – It’s like sausage making process



                                           ©2010 CloudBees, Inc. All Rights   19
                                                     Reserved
Demo




       ©2010 CloudBees, Inc. All Rights
                 Reserved
Maven Repository Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Maven+Repository+Server


• Virtual Maven repository
     – Expose artifacts from specific build
     – And its upstream builds




                                          ©2010 CloudBees, Inc. All Rights    21
                                                    Reserved
http://www.flickr.com/photos/andresmusta/7990193487




©2010 CloudBees, Inc. All Rights                                22
          Reserved
Challenge: Visualization
• Edge traversal
  breaks down on large
  workflow




              ©2010 CloudBees, Inc. All Rights   23
                        Reserved
Dependency Graph
https://wiki.jenkins-ci.org/display/JENKINS/Dependency+Graph+View+Plugin


• Uses GraphViz to draw a
  graph of relations

• Supports copy-artifact
  plugin




                                         ©2010 CloudBees, Inc. All Rights   24
                                                   Reserved
Build Pipeline Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Build+Pipeline+Plugin


• It shows how far each change has gone




                                            ©2010 CloudBees, Inc. All Rights   25
                                                      Reserved
Fingerprint
• Yet another angle to look at data
• Think of it as a lifelog for a blob

Air travel                                           Jenkins

Airline check-in/TSA/immigration Jobs and workflow

My going through immigration                         Build #13

My travel experience                                 Fingerprint view

                  ©2010 CloudBees, Inc. All Rights                      26
                            Reserved
Fingerprint: Mechanism
• MD5 checksum of a file
  – Recorded against builds that it appeared
  – (And actions that were taken)

      acme-build #15                                         acme-test #13



                 MD5=                                                   MD5=
               604e7e07                                               604e7e07




                          ©2010 CloudBees, Inc. All Rights                       27
                                    Reserved
Fingerprint: Why?
• Track down where it came from
  – My component integrates to product
    XYZ, and a bug was reported against XYZ
    3.0.5. Which build of the component did it
    contain?
• Cross-correlate jobs that aren’t directly
  related



                 ©2010 CloudBees, Inc. All Rights   28
                           Reserved
Next Step in Workflow


     ✓    Aggregation of results



      ?   Aggregation of definitions




              ©2010 CloudBees, Inc. All Rights   29
                        Reserved
Build Flow Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+Plugin


• Groovy DSL for kicking builds
     – High-level primitives
     – Ability to define abstractions

                 b = build(“acme-build”)
                 guard {
                       parallel (
                            { build(“acme-test1”, param1:b.number) },
                            { build(“acme-test2”, param1:b.number) }
                       )
                 } rescue {
                       build(“acme-teardown”)
                 }


                                           ©2010 CloudBees, Inc. All Rights   30
                                                     Reserved
Demo




       ©2010 CloudBees, Inc. All Rights
                 Reserved
Jenkow Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Jenkow+Plugin


• Embed BPMN workflow
  engine in Jenkins
     – Timeout, fork, join, …


• Workflow is version
  controlled in Git
     – Push to Jenkins to load
       them up


                                           ©2010 CloudBees, Inc. All Rights   32
                                                     Reserved
Next Step in Workflow


  ✓   Choreography defined in one place



  ?   Everything defined in one place




              ©2010 CloudBees, Inc. All Rights   33
                        Reserved
Job DSL Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Job+DSL+Plugin


• Groovy DSL for defining jobs
      def project = "jenkinsci/jenkins"
      def branchApi = new URL("https://api.github.com/repos/${project}/branches")
      def branches = new JsonSlurper().parse(branchApi.newReader())
      branches.each { b ->
        job {
           name "${project}-${b.name}".replaceAll('/','-')
           scm {
             git("git://github.com/${project}.git", b.name)
           }
           steps {
             maven("install")
           }
        }
      }




                                           ©2010 CloudBees, Inc. All Rights         34
                                                     Reserved
Or More Likely…
• Take Existing Job, Make Adjustments
  def project = "jenkinsci/jenkins"
  def branchApi = new URL("https://api.github.com/repos/${project}/branches")
  def branches = new JsonSlurper().parse(branchApi.newReader())
  branches.each { b ->
    job {
       using "jenkins-build"
       name "${project}-${b.name}".replaceAll('/','-')
       scm {
         git("git://github.com/${project}.git", b.name)
       }
    }
  }




                             ©2010 CloudBees, Inc. All Rights                   35
                                       Reserved
Job DSL Plugin
• You can go down to XML definitions

• The program itself executes as Jenkins
  job
  – Control over when it executes
  – Store definitions in VCS




                ©2010 CloudBees, Inc. All Rights   36
                          Reserved
Or just a bit of Perl/Python/Ruby scripts
• Programmatically CRUD jobs

  $ ssh jenkins get-job foo 
  | sed -e 's/old.gitserver.com/new.gitserver.com/g’ 
  | ssh jenkins update-job foo




                              ©2010 CloudBees, Inc. All Rights   37
                                        Reserved
Templates (in Jenkins Enterprise by
CloudBees)
http://www.cloudbees.com/jenkins-enterprise-by-cloudbees-overview.cb


• Share some traits with Job DSL
     – Define job once, generate many variations
     – Update definition, and reflect it everywhere
• But different
     – Templates are defined in GUI, not in a
       program
     – Individual variations are manually updated
       by users


                                         ©2010 CloudBees, Inc. All Rights   38
                                                   Reserved
Conclusion
• Lots of useful building blocks for
  automating even more
  – That means many people are doing this


• Take your automation to the next level




                ©2010 CloudBees, Inc. All Rights   39
                          Reserved

Large scale automation with jenkins

  • 1.
    Large-Scale Automation withJenkins Kohsuke Kawaguchi / kk@kohsuke.org / @kohsukekawa Architect, CloudBees, Inc. ©2010 CloudBees, Inc. All Rights Reserved
  • 2.
    Have you metJenkins? http://jenkins-ci.org/ • #1 OSS CI server – Written in Java – 47K+ installations • About 7 years old • Two key emphasis – Easy to install/use – Extensible via 600+ plugins ©2010 CloudBees, Inc. All Rights 2 Reserved
  • 3.
    Context • First stepsin Jenkins usage – Build your project – Run tests on your project – Maybe run code analysis tools Where to go from here? ©2010 CloudBees, Inc. All Rights 3 Reserved
  • 4.
    Workflow! • By whichI mean interconnected jobs • Why? – Lots of manual process – Dividing by the responsibility boundary – Single job isn’t meant for complex things – Reusing pieces ©2010 CloudBees, Inc. All Rights 4 Reserved
  • 5.
    Parameterized Builds • Plainjobs can be thought of like a procedure without any input void buildAcmeLibrary() { … } • Ability to pass parameters make it more useful void buildAcmeLibrary(targetPlatform) { … } ©2010 CloudBees, Inc. All Rights 5 Reserved
  • 6.
    Three Things YouNeed To Do • #1: Define Parameters Lots of different parameter types ©2010 CloudBees, Inc. All Rights 6 Reserved
  • 7.
    Three Things YouNeed To Do • #2: Refer to parameter values – As variable expansions: ${Browser} – As environment variables from your builds – Some parameter types expose data in different ways • File parameter ©2010 CloudBees, Inc. All Rights 7 Reserved
  • 8.
    Three Things YouNeed To Do • #3: Specify actual values when you run ©2010 CloudBees, Inc. All Rights 8 Reserved
  • 9.
    Parameterized Trigger Plugin https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin •Call other jobs (with parameters) – Wait for their completions (optional) Foo Sub1 Sub2 Sub3 Foo Sub1 Sub2 ©2010 CloudBees, Inc. All Rights 9 Reserved
  • 10.
    Multi-Configuration Project • Youoften do the same thing with slight variations – Compile C++ code for different platforms – Test with different browsers – Or more generally, think of it as for (x in [a,b,c]) { for (y in [d,e,f]) { doSomethingWith(x,y,...); } } ©2010 CloudBees, Inc. All Rights 10 Reserved
  • 11.
    Model • Define axes – One axis ≈ one for loop • Choose from pre-defined types of Axis – Generic axis: arbitrary values exposed as environment variables – Slave axis: pick slaves by their names or their labels • e.g., linux, solaris, and windows – JDK axis ©2010 CloudBees, Inc. All Rights 11 Reserved
  • 12.
    Multi-Configuration Project Gimmicks •Filtering – Otherwise combinations increase exponentially – Not all combinations make sense – Use boolean expression to trim down the size (label==“windows”).implies(browser==“iexplore”) && (label==“mac”).implies(browser==“safari”) – Or tell Jenkins to cut the workload to N% • Jenkins will thin out the combinations by itself ©2010 CloudBees, Inc. All Rights 12 Reserved
  • 13.
    Demo ©2010 CloudBees, Inc. All Rights Reserved
  • 14.
    Other Simple ChoreographyTools https://wiki.jenkins-ci.org/display/JENKINS/Join+Plugin • Join Plugin ©2010 CloudBees, Inc. All Rights 14 Reserved
  • 15.
    When Jobs StartWorking Together… https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin • Copy Artifact Plugin – Copy artifacts into a workspace – By using various criteria acme-build #15 acme-test #24 foo.jar foo.jar ©2010 CloudBees, Inc. All Rights 15 Reserved
  • 16.
    Copy Artifact vsExternal Repository • Almost as if artifacts are versioned acme-build #10 Last saved build acme-build #11 acme-build #12 Last stable build acme-build #13 acme-build #14 acme-build #15 foo.jar ©2010 CloudBees, Inc. All Rights 16 Reserved
  • 17.
    Labeling Builds IsUseful • Especially when labels have semantics Deployed to production acme-build #10 Signed off to QA acme-build #11 acme-build #12 Last stable build acme-build #13 acme-build #14 acme-build #15 foo.jar ©2010 CloudBees, Inc. All Rights 17 Reserved
  • 18.
    Labeling Builds IsUseful • More so when you automate them • Take “Signed off to QA” label for example acme-devtest #15 acme-build #12 foo.jar acme-findbugs #22 Signed off to QA ©2010 CloudBees, Inc. All Rights 18 Reserved
  • 19.
    Introducing Promoted BuildsPlugin https://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin • Promotion = act of giving a build a label • You specify: – Promotion criteria – what happens after promotion • Label is a nice hand-off between teams – It’s like sausage making process ©2010 CloudBees, Inc. All Rights 19 Reserved
  • 20.
    Demo ©2010 CloudBees, Inc. All Rights Reserved
  • 21.
    Maven Repository Plugin https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Maven+Repository+Server •Virtual Maven repository – Expose artifacts from specific build – And its upstream builds ©2010 CloudBees, Inc. All Rights 21 Reserved
  • 22.
  • 23.
    Challenge: Visualization • Edgetraversal breaks down on large workflow ©2010 CloudBees, Inc. All Rights 23 Reserved
  • 24.
    Dependency Graph https://wiki.jenkins-ci.org/display/JENKINS/Dependency+Graph+View+Plugin • UsesGraphViz to draw a graph of relations • Supports copy-artifact plugin ©2010 CloudBees, Inc. All Rights 24 Reserved
  • 25.
    Build Pipeline Plugin https://wiki.jenkins-ci.org/display/JENKINS/Build+Pipeline+Plugin •It shows how far each change has gone ©2010 CloudBees, Inc. All Rights 25 Reserved
  • 26.
    Fingerprint • Yet anotherangle to look at data • Think of it as a lifelog for a blob Air travel Jenkins Airline check-in/TSA/immigration Jobs and workflow My going through immigration Build #13 My travel experience Fingerprint view ©2010 CloudBees, Inc. All Rights 26 Reserved
  • 27.
    Fingerprint: Mechanism • MD5checksum of a file – Recorded against builds that it appeared – (And actions that were taken) acme-build #15 acme-test #13 MD5= MD5= 604e7e07 604e7e07 ©2010 CloudBees, Inc. All Rights 27 Reserved
  • 28.
    Fingerprint: Why? • Trackdown where it came from – My component integrates to product XYZ, and a bug was reported against XYZ 3.0.5. Which build of the component did it contain? • Cross-correlate jobs that aren’t directly related ©2010 CloudBees, Inc. All Rights 28 Reserved
  • 29.
    Next Step inWorkflow ✓ Aggregation of results ? Aggregation of definitions ©2010 CloudBees, Inc. All Rights 29 Reserved
  • 30.
    Build Flow Plugin https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+Plugin •Groovy DSL for kicking builds – High-level primitives – Ability to define abstractions b = build(“acme-build”) guard { parallel ( { build(“acme-test1”, param1:b.number) }, { build(“acme-test2”, param1:b.number) } ) } rescue { build(“acme-teardown”) } ©2010 CloudBees, Inc. All Rights 30 Reserved
  • 31.
    Demo ©2010 CloudBees, Inc. All Rights Reserved
  • 32.
    Jenkow Plugin https://wiki.jenkins-ci.org/display/JENKINS/Jenkow+Plugin • EmbedBPMN workflow engine in Jenkins – Timeout, fork, join, … • Workflow is version controlled in Git – Push to Jenkins to load them up ©2010 CloudBees, Inc. All Rights 32 Reserved
  • 33.
    Next Step inWorkflow ✓ Choreography defined in one place ? Everything defined in one place ©2010 CloudBees, Inc. All Rights 33 Reserved
  • 34.
    Job DSL Plugin https://wiki.jenkins-ci.org/display/JENKINS/Job+DSL+Plugin •Groovy DSL for defining jobs def project = "jenkinsci/jenkins" def branchApi = new URL("https://api.github.com/repos/${project}/branches") def branches = new JsonSlurper().parse(branchApi.newReader()) branches.each { b -> job { name "${project}-${b.name}".replaceAll('/','-') scm { git("git://github.com/${project}.git", b.name) } steps { maven("install") } } } ©2010 CloudBees, Inc. All Rights 34 Reserved
  • 35.
    Or More Likely… •Take Existing Job, Make Adjustments def project = "jenkinsci/jenkins" def branchApi = new URL("https://api.github.com/repos/${project}/branches") def branches = new JsonSlurper().parse(branchApi.newReader()) branches.each { b -> job { using "jenkins-build" name "${project}-${b.name}".replaceAll('/','-') scm { git("git://github.com/${project}.git", b.name) } } } ©2010 CloudBees, Inc. All Rights 35 Reserved
  • 36.
    Job DSL Plugin •You can go down to XML definitions • The program itself executes as Jenkins job – Control over when it executes – Store definitions in VCS ©2010 CloudBees, Inc. All Rights 36 Reserved
  • 37.
    Or just abit of Perl/Python/Ruby scripts • Programmatically CRUD jobs $ ssh jenkins get-job foo | sed -e 's/old.gitserver.com/new.gitserver.com/g’ | ssh jenkins update-job foo ©2010 CloudBees, Inc. All Rights 37 Reserved
  • 38.
    Templates (in JenkinsEnterprise by CloudBees) http://www.cloudbees.com/jenkins-enterprise-by-cloudbees-overview.cb • Share some traits with Job DSL – Define job once, generate many variations – Update definition, and reflect it everywhere • But different – Templates are defined in GUI, not in a program – Individual variations are manually updated by users ©2010 CloudBees, Inc. All Rights 38 Reserved
  • 39.
    Conclusion • Lots ofuseful building blocks for automating even more – That means many people are doing this • Take your automation to the next level ©2010 CloudBees, Inc. All Rights 39 Reserved

Editor's Notes

  • #14 Create JDK axis and browser axisShow java –fullversion and sleep 5Do a couple of buildsIndex%2==0Touch stone build
  • #18 Compare how far production is behindSingle build number to rule them all
  • #27 Analogy with airplane travel:Define the procedure that the immigration officers gothrough. Each airport is like a job.People goes through airline counter, security check, border control, is like a workflowWith fingerprint, what we are talking is looking at individual traveler and how he went through the whole process
  • #32 Say Hello + BuildFlow