Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RigaDevDays 2017. Pipeline as Code with Jenkins

RigaDevDays 2017. Pipeline as Code with Jenkins

Almost everybody knows Old Faithful Jenkins… and probably hates its job configuration pages. Although being 10 years old, Jenkins actively adopts modern trends like DevOps and Continuous Delivery. One of the hottest areas is Jenkins Pipeline, which allows describing complex automation flows as code in a Groovy-based DSL.

We will talk about Jenkins Pipeline and its fresh-new features. How does it help to organize automation in your projects? How does it improve the developer experience? What is BlueOcean UI? How to choose between declarative and imperative definitions in DSL? How to scale automation with Pipeline? And finally, does it make sense to migrate and what obstacles to expect?

Oleg Nenashev

May 15, 2017
Tweet

More Decks by Oleg Nenashev

Other Decks in Programming

Transcript

  1. The Butler is still young. Pipeline as Code with Jenkins

    Oleg Nenashev (@oleg_nenashev) CloudBees, Inc.
  2. © 2017 CloudBees, Inc. All Rights Reserved. 2 #RigaDevDays 2017

    About me @oleg_nenashev oleg-nenashev LibreCores project St. Petersburg Polytechnic University Jenkins meetups
  3. © 2017 CloudBees, Inc. All Rights Reserved. 3 #RigaDevDays 2017

    Oleg’s “Hall of Shame”(c) • Plugins • Jenkins Core • Remoting • Windows Service Wrapper
  4. © 2017 CloudBees, Inc. All Rights Reserved. 4 #RigaDevDays 2017

    "Having temperature of 38°C is still better than 38°F” © My motto for this conference: https://www.pinterest.com/browserling/ weekly-programming-comic/
  5. © 2017 CloudBees, Inc. All Rights Reserved. 8 #RigaDevDays 2017

    1. Most popular CI/CD tool in the world 2. It’s open source 3. Big community 4. Commercial support vendors 5. … Who is Mr. Jenkins? https://jenkins.io
  6. © 2017 CloudBees, Inc. All Rights Reserved. 9 #RigaDevDays 2017

    Jenkins – Software CI/CD Server Do you agree?
  7. © 2017 CloudBees, Inc. All Rights Reserved. 10 #RigaDevDays 2017

    Jenkins – Software CI/CD Server – Automation Framework Disclaimer: IMHO
  8. © 2017 CloudBees, Inc. All Rights Reserved. 11 #RigaDevDays 2017

    Система Docs Plugins Configs Infra- structure Samples Demos Automation system Infra- structure Infra- structure • Framework • Orchestrator
  9. Today’s Pipelines… ØWhy Jenkins Pipeline? ØWhat’s HOT in Jenkins Pipeline?

    ØTBD: My Adopter Experience Disclaimer: • This presentation reflects the author’s personal opinion • It may differ from CloudBees official position
  10. © 2017 CloudBees, Inc. All Rights Reserved. 14 #RigaDevDays 2017

    • Jenkins 101 • Configuration As Code101 • Pipeline 201 • Added-value Pipeline features in CloudBees Jenkins Enterprise What is NOT in the talk?
  11. © 2017 CloudBees, Inc. All Rights Reserved. 15 #RigaDevDays 2017

    Frameworks are flexible… • Build chains & deps (aka pipelines) • Resource management • Environment/Tool management • …
  12. © 2017 CloudBees, Inc. All Rights Reserved. 16 #RigaDevDays 2017

    Approach: Using “classic” Job types: • Freestyle, Matrix, JobDSL, … Classic Approaches Keywords: • Script steps to run the logic • Custom Tools Plugin to manage tools • Throttle Concurrent Builds and Lockable Resources to dispatch access to hardware • Naginator Plugin to restart builds on-demand • Parameterized Trigger / Copy artifacts to parallelize tests and to collect results JUC2015/London https://jenkins.io/solutions/embedded/
  13. © 2017 CloudBees, Inc. All Rights Reserved. 17 #RigaDevDays 2017

    Classic Approaches Spaghetti Automation • Many job dependencies • Difficult build flow tracking • Complex configurations • Duplication • Expensive maintenance
  14. © 2017 CloudBees, Inc. All Rights Reserved. 18 #RigaDevDays 2017

    ü Store together with the project ü Modify together with the project ü Test together with the project … as code? Automation
  15. © 2017 CloudBees, Inc. All Rights Reserved. 19 #RigaDevDays 2017

    Система Docs Plugins Configs Infra- structure Samples Demos Automation Framework Vision Infra- structure Infra- structure SCM Infrastructure as Code Configuration as Code Documentation as Code Pipeline Libraries Pipelines
  16. © 2017 CloudBees, Inc. All Rights Reserved. 20 #RigaDevDays 2017

    Configuration as Code in Jenkins Job Configuration System Configuration
  17. © 2017 CloudBees, Inc. All Rights Reserved. 21 #RigaDevDays 2017

    Jenkins Configuration… as Code External Tools Jenkins CLI and REST API python- jenkins jenkins- client (java) Configuration Management systems Puppet, Ansible, Chef Docker Solutions within Jenkins project Groovy Hooks Scriptler Plugin SCM Sync Configurati on Unstable Just examples… Unsafe/ blocked
  18. © 2017 CloudBees, Inc. All Rights Reserved. 23 #RigaDevDays 2017

    Configuration as Code for jobs Job DSL Job Builder Plugin Jenkins Pipeline Pipeline is offered by default in Jenkins 2.0 … Groovy YAML Groovy
  19. © 2017 CloudBees, Inc. All Rights Reserved. 25 #RigaDevDays 2017

    Jenkins Pipeline is a… 1. Groovy DSL for automatic flow description - defined in SCM (Jenkinsfile) - defined in Job configuration // Run on a node having the “my_fpga” label node("linux && ml509") { git url:"http://github.com/myorg/myproject.git" sh "make all" } http://bit.ly/pipeline-tutorial
  20. © 2017 CloudBees, Inc. All Rights Reserved. 26 #RigaDevDays 2017

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

    Jenkins Pipeline is a… 1. Groovy DSL for automatic flow description 2. Item type 3. Pipeline as Code Ecosystem
  22. © 2017 CloudBees, Inc. All Rights Reserved. 28 #RigaDevDays 2017

    Branch- Source, BlueOcean, ... 89 - explicit support 42 - Pipeline- specific Documentation Samples Development Tools Jenkins Pipeline Ecosystem Plugins:
  23. © 2017 CloudBees, Inc. All Rights Reserved. 29 #RigaDevDays 2017

    Main features Independent from node/workspace Parallelization Stages Robustness
  24. © 2017 CloudBees, Inc. All Rights Reserved. 30 #RigaDevDays 2017

    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
  25. © 2017 CloudBees, Inc. All Rights Reserved. 31 #RigaDevDays 2017

    More about Pipeline Features • http://bit.ly/pipeline-docs • http://bit.ly/pipeline-compatibility
  26. Jenkins & Pipeline in 2017 Ø Easy to use •

    Blue Ocean • Integrations
  27. Jenkins & Pipeline in 2017 Ø Easy to use Ø

    Easy to start • Declarative Pipeline • Visual Pipeline Editor • Better docs pipeline { agent label:"linux" tools { maven ”M3" } stages { stage("build") { steps { sh 'mvn clean verify' } } } post { always { junit ”target/…/*.xml" } }
  28. Jenkins & Pipeline in 2017 Ø Easy to use Ø

    Easy to start Ø Easy to reuse • Libraries # Jenkinsfile buildPlugin()
  29. © 2017 CloudBees, Inc. All Rights Reserved. 39 #RigaDevDays 2017

    BlueOcean Pipeline Editor Plugin https://jenkins.io/doc/book/blueocean/pipeline-editor/ Edit & Commit from Web UI!
  30. © 2017 CloudBees, Inc. All Rights Reserved. 40 #RigaDevDays 2017

    More Info • Docs: https://jenkins.io/doc/book/blueocean/ • Video: http://bit.ly/blueocean-kickoff
  31. © 2017 CloudBees, Inc. All Rights Reserved. 42 #RigaDevDays 2017

    Syntax sugar on the top of Pipeline • “pipeline { }” closure Common configuration sections • Easy to set up • More static checks • Less flexibility 42 Declarative Pipeline https://github.com/jenkinsci/pipeline- model-definition-plugin/wiki
  32. © 2017 CloudBees, Inc. All Rights Reserved. 43 #RigaDevDays 2017

    43 Example. Declarative Pipeline pipeline { agent label:"generic-linux" tools { maven "Maven 3.3.9" jdk "Oracle JDK 8u40" } stages { stage("build") { steps { sh 'mvn clean install -Dmaven.test.failure.ignore=true' } } } post { always { junit "path/to/xml" } failure { mail to:"[email protected]", subject:"FAILURE:${currentBuild.name}", …} } }
  33. © 2017 CloudBees, Inc. All Rights Reserved. 44 #RigaDevDays 2017

    When to use Declarative? Just starting with Jenkins • Migrating from declarative services Have good encapsulation in Libraries • Call them from stages, use other stuff as wrappers Simple use-case • E.g. “just run Maven/Make & handle errors”
  34. © 2017 CloudBees, Inc. All Rights Reserved. 45 #RigaDevDays 2017

    Error Handling • IMHO: Pipeline is designed for Success Path scenarios When it “Just works” When it does not
  35. © 2017 CloudBees, Inc. All Rights Reserved. 46 #RigaDevDays 2017

    Error Handling. Top-level Scripted Pipeline Declarative Pipeline Global try/catch() L When it does not…
  36. © 2017 CloudBees, Inc. All Rights Reserved. 47 #RigaDevDays 2017

    Obstacles • Limited functionality compared to Scripted Pipeline • Cannot put Declarative Pipeline into library • Difficult to migrate to Scripted if required # Jenkinsfile buildPlugin()
  37. © 2017 CloudBees, Inc. All Rights Reserved. 49 #RigaDevDays 2017

    Reusing code via libraries 2015 • load() command Early 2016 • Global Pipeline Library • Pipeline Remote Loader Plugin Late 2016 • Pipeline Shared Libraries • Library management • Support of @Grab – Pipeline libs can pull common Groovy libs Library Structure:
  38. © 2017 CloudBees, Inc. All Rights Reserved. 50 #RigaDevDays 2017

    Pipeline Shared Libraries • Libs are located in SCM • Versioning • Scopes ▸System-wide ▸Folder ▸Job
  39. © 2017 CloudBees, Inc. All Rights Reserved. 53 #RigaDevDays 2017

    Libraries in Scripted Pipeline com.mycorp.libs.make Jenkinsfile makeProject() makeProject() implementation - global variable com.mycorp.libs.coverity com.mycorp.libs.cppunit Globally-defined library, automatic update @Library() definition
  40. © 2017 CloudBees, Inc. All Rights Reserved. 58 #RigaDevDays 2017

    buildPlugin(). How does it work? … Pure Pipeline DSL
  41. © 2017 CloudBees, Inc. All Rights Reserved. 60 #RigaDevDays 2017

    Pipeline Early adoption It is working! * Image: http://devopsreactions.tumblr.com/post/151281670953/trying-the-first-prototype-good-enough
  42. Lessons Learnt. Obstacles? • Missing integrations • Pipeline DSL !=

    Groovy • Development Tools • Non-Success Path handling
  43. © 2017 CloudBees, Inc. All Rights Reserved. 62 #RigaDevDays 2017

    Missing integration ØTools/plugins •http://bit.ly/pipeline-compatibility •Trusted pipeline libraries help much ØBuild Promotion (Promoted Builds, Release, …)
  44. © 2017 CloudBees, Inc. All Rights Reserved. 63 #RigaDevDays 2017

    For a reason… Pipeline DSL is not Groovy
  45. © 2017 CloudBees, Inc. All Rights Reserved. 64 #RigaDevDays 2017

    Reason 1. Context retention – CPS/NonCPS • No way to propagate any data • Solution - @NonCPS steps within the library steps
  46. © 2017 CloudBees, Inc. All Rights Reserved. 65 #RigaDevDays 2017

    Reason 2. Script Security Script approval Groovy Sandbox Mode • Mandatory for Jenkinsfile from SCM • Mandatory for untrusted libs Limited access to Java API • Script approvals by admins • Debug fun “Run & Fail & Approve & Rerun”
  47. Lessons Learnt. Obstacles? • Missing integrations • Pipeline DSL !=

    Groovy • Development Tools • Non-Success Path handling
  48. © 2017 CloudBees, Inc. All Rights Reserved. 67 #RigaDevDays 2017

    Pipeline. Dev Tools IDE Integration Unit Test Framework Static Analysis Library manager Debug tools Documen- tation Easy script deployment Freestyle => Pipeline Converter Good Not enough Missing
  49. © 2017 CloudBees, Inc. All Rights Reserved. 68 #RigaDevDays 2017

    How do I work? Intellij IDEA Filesystem SCM Plugin (with patch) Disclaimer: There may be better ways • Syntax file • Built-in documentation Jenkins Test Instance Source Code (local .git repos) • Production System Snapshot • Custom Pipeline Library Settings • Repositories for libraries • Jenkinsfile repos
  50. © 2017 CloudBees, Inc. All Rights Reserved. 70 #RigaDevDays 2017

    Filesystem SCM Plugin • Pending pull request: • https://github.com/jenkinsci/filesystem_scm-plugin/pull/2 Library definition in the test instance !!!
  51. © 2017 CloudBees, Inc. All Rights Reserved. 71 #RigaDevDays 2017

    Pipeline Testing • No test framework offered by the Jenkins project • External project: https://github.com/lesfurets/JenkinsPipelineUnit “mvn verify” has been invoked… And the execution passed… For the source Jenkinsfile…
  52. Lessons Learnt. Obstacles? • Missing integrations • Pipeline DSL !=

    Groovy • Development Tools • Non-Success Path handling
  53. © 2017 CloudBees, Inc. All Rights Reserved. 73 #RigaDevDays 2017

    Error Handling When it “Just works” When it does not The interesting part… Failover, repeat
  54. © 2017 CloudBees, Inc. All Rights Reserved. 74 #RigaDevDays 2017

    Error handling. Hardware failover for (def board : boards) { echo "trying board " + board; try { node(board) { checkout scm sh ‘./bin/run.sh’ // Call passed => DONE break; } } catch (Exception ex) { if (ex.message.contains ("exit code 255")) { // Fatal error fail("Test run failed") } } } ??? • Hard to extract error cause • Try/catch handling for every case • Cannot just use existing parsers • Build Failure Analyzer • Log Parser • Warnings • Try-catch blocks pollute the code • Hard to test
  55. © 2017 CloudBees, Inc. All Rights Reserved. 75 #RigaDevDays 2017

    Error handling. Hardware failover Declarative Pipeline Encapsulation via Libraries Simple use-case Other • Exception try/catch • timeout() wrapper • Proper failover • Logic in @NonCPS
  56. © 2017 CloudBees, Inc. All Rights Reserved. 76 #RigaDevDays 2017

    … simplifies small jobs and complex workflows … saves much time on system maintenance … is a programming language, high barrier to entry … lacks Dev Tools and some integrations Lessons learnt. Pipeline…
  57. © 2017 CloudBees, Inc. All Rights Reserved. 77 #RigaDevDays 2017

    What I need? Dev Tools for Pipeline • IDE, Debugger, Test Tools Community Pipeline Libraries • Support common cases without plugins • Pipeline-only plugins work too Promotion engine for Pipeline • Ideally: Merge & Integrate Promoted Builds and Release plugins
  58. © 2017 CloudBees, Inc. All Rights Reserved. 78 #RigaDevDays 2017

    Takeaways • Jenkins is not “CRON with Web UI” • Pipeline as Code is your friend • If you use Jenkins… •Try Jenkins Pipeline •Try Declarative Pipeline •Try BlueOcean
  59. © 2017 CloudBees, Inc. All Rights Reserved. 79 #RigaDevDays 2017

    Thank you! Contacts: E-mail: [email protected] GitHub: oleg-nenashev Twitter: @oleg_nenashev
  60. © 2017 CloudBees, Inc. All Rights Reserved. 80 #RigaDevDays 2017

    • CloudBees website • Website: https://www.cloudbees.com • Jenkins project • Website: https://jenkins.io • Meetup: https://www.meetup.com/Jenkins-online-meetup/ • Jenkins Pipeline • Tutorial: http://bit.ly/pipeline-tutorial • Compatibility: http://bit.ly/pipeline-compatibility • Examples: https://github.com/jenkinsci/pipeline-examples • Blue Ocean • https://jenkins.io//projects/blueocean/ Useful Links
  61. © 2017 CloudBees, Inc. All Rights Reserved. 81 #RigaDevDays 2017

    Incoming Big Events Day Of Jenkins May 30 – Gothenburg, Jun 01 – Oslo Jenkins Community Day Jul 11 – Paris Jenkins World Aug 28-31 San Francisco
  62. © 2017 CloudBees, Inc. All Rights Reserved. 82 #RigaDevDays 2017

    Thank you! Contacts: E-mail: [email protected] GitHub: oleg-nenashev Twitter: @oleg_nenashev