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

Declarative Pipelines in Jenkins

Declarative Pipelines in Jenkins

A new way to define your pipelines in Jenkins!!

Pipeline is quickly establishing itself as the direction that Jenkins jobs are going, enabling the definition of a complete CD pipeline in a single job; Pipeline as Code via the “Jenkinsfile”; job durability across master restarts; and more. I’ll be talking here about the next evolution for Pipeline: a simple, declarative model to define your Pipelines with no need to write scripts. This configuration syntax for Pipeline allows you to automatically configure all stages of your pipeline, the complete build environment, post-build actions, notifications and more. All while providing syntactic and semantic validation before the build actually gets going. We'll also play with the just-released initial version of the Pipeline Editor, built to work with Declarative!

This session was presented in French at Voxxed Days Luxembourg 2017


Arnaud Heritier

June 22, 2017


  1. Declarative Pipelines Andrew Bayer Arnaud Héritier

  2. Arnaud Héritier • Long time Jenkins user and community member

    • August 2007 : My first discussion on the mailing list and my first ticket about Jenkins born Hudson 1.131 • Contributor on various plugins: • Maven, Xcode, Appaloosa, … • Member of the Jenkins infra team • I love to reboot confluence ☺ • Support Delivery Manager at CloudBees, Inc • I help our customers to get the best from Jenkins @aheritier aheritier
  3. What is Declarative?

  4. What is Declarative? • A syntax for Jenkins Pipelines designed

    to… ◦ Have smarter default behavior for most use cases ◦ Make Pipelines more structured ◦ Provide better error reporting and handling
  5. Smarter default behavior • Default SCM checkout • Built with

    Docker and containers in mind from day one • No more need for try/catch for notifications, etc • More intuitive ways to specify job properties, triggers and parameters • Streamlined syntax for conditional execution of stages
  6. More structure in Pipelines • Separate configuration from actual steps

    to be executed • Everything happens in a stage • Easier to look at any Declarative Pipeline and understand what it’s doing - more consistency! • Enables round-tripping with the visual editor (more on this later!) • Built with and for Blue Ocean visualization
  7. Better error reporting • Syntax and validation errors reported at

    the very beginning of the build • No more waiting til a build has gone an hour to discover that you misspelled “timeout”! • Errors report as compilation errors, pointing to the specific code or text that’s causing problems
  8. Better error reporting • Eliminates many potentially confusing stack traces

    (though not all!) • Validates things like syntax, required configuration, step parameter types, and more
  9. What happens to Scripted Pipeline? • In short? Nothing. •

    We’re now calling “traditional” Pipeline “Scripted Pipeline” to distinguish it from Declarative Pipeline • All one execution engine behind the scenes ◦ Declarative is built on top of Scripted Pipeline, not replacing it ◦ Blue Ocean and Stage View don’t distinguish between a Scripted Pipeline or a Declarative Pipeline
  10. What happens to Scripted Pipeline? • Scripted Pipeline still used

    *inside* Declarative Pipeline in steps blocks, post blocks ◦ You can copy the contents of a Declarative steps block into a Scripted Pipeline (inside a node block, of course!) and it’ll Just Work.
  11. Why Declarative?

  12. Why everyone should use Pipeline • Durability • Pipeline as

    Code • More modern backend implementation • More powerful and flexible than traditional Jenkins jobs
  13. Benefits of Declarative for new users • Lower barrier of

    entry than Scripted Pipelines • Human readable • Does not require Groovy-specific expertise • Create and edit Declarative Pipelines via the UI Editor
  14. Benefits of Declarative for existing users • Uses same engine

    as Scripted, so existing investments still pay off • Expand usage and usability ◦ Less burden on “experts” ◦ Easier empowerment of other users • Easier collaboration and code review • Lintable! • Separation of Jenkins infrastructure-related configuration from steps to execute
  15. Syntax Overview

  16. © 2017 CloudBees, Inc. All Rights Reserved. 16 node {

 try { stage('Build') { checkout scm docker.image('ubuntu').inside {
 sh 'mvn clean install' } } }
 catch(exc) { mail to:'me@ex.com', subject:'FAILURE:' } finally { deleteDir() }
 } pipeline {
 agent { docker 'ubuntu' } stages {
 stage('Build') {
 steps {
 sh 'mvn clean install'
 post {
 always {
 failure {
 mail to:'me@ex.com', subject:'FAILURE:' }
 } Scripted Pipeline Declarative Pipeline
  17. First example https://gist.github.com/abayer/ bd99a722aeee5077a16c4bfc6ed8f9a1

  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. Second example https://gist.github.com/abayer/ 01ca97caae06aeb2d8e3f71b3bc0b9a9

  25. None
  26. None
  27. None
  28. None
  29. Validation

  30. Validation and linting • Always happens at the beginning of

    a build • If any validation errors are found, the build will fail before actually running anything • Errors point to the problem area with line/column number and an error message, often with suggestions for what you may have intended
  31. None
  32. CLI and API support for linting

  33. Blue Ocean

  34. Blue Ocean visualization • Some special smarts on both sides

    for optimized visualization • Blue Ocean keeps built-in stages for things like checkout, Docker image prep, post-build actions out of your way unless there’s a problem • Special marking of stages that have been skipped due to either an unsatisfied when condition or an earlier failed stage
  35. Pipeline Visual Editor!

  36. None
  37. None
  38. None
  39. What’s next?

  40. Declarative - Parallel stages stage('foo') { parallel { stage('first') {

    steps { echo "First branch" } } stage('second') { steps { echo "Second branch" } } } } https://issues.jenkins-ci.org/browse/JENKINS-41334
  41. That’s a wrap!

  42. Resources on Declarative • Documentation on https://jenkins.io/doc/ • Examples coming

    soon at https://github.com/ jenkinsci/pipeline-examples • Plugin source at https://github.com/jenkinsci/pipeline- model-definition-plugin
  43. Questions?

  44. Thanks!