Hi,

We already know how to create a build on Jenkins using freestyle job, it’s a quick way to make a job, but what happened when we want to have complex tasks and workflow to trigger?

Freestyle job limitation

Indeed ,it’s still possible to chain these builds to get a sophisticated workflow,but freestyle jobs are limited by many ways :
-the builds will not survive a restart of Jenkins
-freestyle job is only based on GUI configuration and not as code
-it is also working on sequential steps and can not be resumed at a particular step of the build.
We have an easier way to perform that workflow and to manage it from the beginning to the end :Using Pipelines

What is a pipeline?

A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.
Also, a pipeline block is a key part of Declarative Pipeline syntax.

Some vocabulary related to pipeline

Jenkins file
It’s a text file written in Groovy language containing the code of all your workflow,it is stored into a SCM like GitHub for example and can be updated by many ways as using Jenkins master UI or directly from the SCM with a text editor. As it is centralized it allows developers an easy to access it to edit update and check it.
SCM
Source code management,tracks changes of your code stored in a source code repository ,example git or mercurial,it allows you to store a Jenkinsfile and your code on it and to update it from one single point.

Pipeline structure components

  • Pipeline is structured in sections, called stages
  • Each stage includes steps
  • steps include the actual tests/commands to run
  • An agent defines where the programs and scripts execute

Node
A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.
Also, a node block is a key part of Scripted Pipeline syntax.
Stage
A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. “Build”, “Test” and “Deploy” stages), which is used by many plugins to visualize or present Jenkins Pipeline status/progress.
Step
A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or “step” in the process). For example, to execute the shell command make use the sh step: sh ‘make’. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step

Two types of pipelines

  • Scripted pipeline: sequential execution, using Groovy expressions for flow control
  • Declarative pipeline: uses a framework to control execution ( pre-defined commands to ease pipeline creation )

Scripted pipeline

The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, a Scripted pipeline is written in the Jenkins file on the web UI of Jenkins. Unlike the Declarative pipeline, the scripted pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge control over the script and can manipulate the flow of the script extensively. This helps developers to develop advance and complex pipelines as code.

  • Executed serially, from top down
  • Relies on Groovy expressions for flow control
    • Requires extensive knowledge of Groovy syntax
  • Very flexible and extensible
    • Limitations are mostly Groovy limitations
    • Power users with complex requirements may need it
  • Novice users can easily mess up the syntax

pipeline as code syntax /scripted Pipeline
Note that the syntax of scripted pipeline is beginning by a “node” block

node {  
    stage('Build') { 
        // 
    }
    stage('Test') { 
        // 
    }
    stage('Deploy') { 
        // 
    }
}

quick example using scripted pipeline skeleton

how to check pipeline log execution

scripted pipeline output

you can check each steps of the pipeline by clicking on the log tab

Declarative pipeline

The Declarative pipeline is a new feature that is added to create the pipeline. The workflow is written in a Jenkins file which can be stored into a source code management system such as Git. Declarative pipelines role is to help developer to  create simple and quick  continuous delivery pipeline using a set of  pre-defined commands

  • Stricter, pre-defined structure
  • Execution can always be resumed after an interruption,
    no matter what caused the interruption
  • Requires only limited knowledge of Groovy syntax
    • Using Blue Ocean simplifies the Pipeline creation process even more( will be a specific topic in next blogs )
  • Encourages a declarative programming model
  • Reduces the risk of syntax errors
  • Use the script step to include bits of scripted code within a Declarative Pipeline only when you have needs that are beyond the capabilities of Declarative syntax

declarative pipeline syntax
Note that the syntax of declarative pipeline is beginning by a “pipeline” block

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

You can store your Jenkinsfile with all your codes in SCM and call them in Jenkins master jobs (in next blog we will go deeper in this topic)

Note that you can update the Jenkinsfile directly with a text editor in Github (for my case)

Conclusion

Now we know the pipeline concept and the two types of pipeline that can be used , one needing more scripting skills and the other with pre-defined commands to ease the conception.
Next time we will focus on how to create a declarative pipeline using blue ocean
Feel free to check the dbi blogger site for more tips and tricks 😉 and also for Jenkins you have loads of info on Cloudbees site and on https://www.jenkins.io/