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
Tweet

More Decks by Arnaud Heritier

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. Better error reporting • Eliminates many potentially confusing stack traces

    (though not all!) • Validates things like syntax, required configuration, step parameter types, and more
  7. 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
  8. 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.
  9. Why everyone should use Pipeline • Durability • Pipeline as

    Code • More modern backend implementation • More powerful and flexible than traditional Jenkins jobs
  10. 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
  11. 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
  12. © 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:'[email protected]', subject:'FAILURE:' } finally { deleteDir() }
 } pipeline {
 agent { docker 'ubuntu' } stages {
 stage('Build') {
 steps {
 sh 'mvn clean install'
 }
 }
 }
 post {
 always {
 deleteDir()
 }
 failure {
 mail to:'[email protected]', subject:'FAILURE:' }
 }
 } Scripted Pipeline Declarative Pipeline
  13. 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
  14. 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
  15. 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
  16. 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