Jenkins pipeline as code - scripted and declarative

Jenkins Pipeline as code – High level information

Last Updated on February 18, 2023 by cscontents

Introduction

In this article we will discuss Jenkins pipeline as a code, which means Jenkins pipeline is implemented as a code (in case of Jenkins “pipeline” type of job we have the option to write pipeline code). In case of Jenkins “freestyle” type of job we need to configure all the stages. Instead of this using pipeline code for our Jenkins pipeline has many benefits like

  • More flexibility,
  • Reusability
  • Managing multiple pipelines (using free style job can be very time-consuming if we have multiple pipeline to manage).

Using pipeline as a code we can define whole application lifecycle which might not be possible with freestyle job.

Prerequisite

Before going through this article, I will assume you are aware of below

  • What is Jenkins? Why is it used?
  • How to create a job (Freestyle) job?
  • And, you are having some understanding of Jenkins “pipeline” type of job.

Jenkins Pipeline as a code/script

Generally, in a Jenkins CI/CD pipeline, we have the below stages

  1. Checkout the codebase from source code repository
  2. Perform various test (e.g., unit test)
  3. Code analysis
  4. Build, which will create some artifacts
  5. Pushing artifacts to some repository.

Using Jenkins pipeline as code, in the pipeline code we need to mention all those stages sequentially.

Now, to start with let’s discuss the syntax of this Jenkins pipeline code (also called pipeline script). There is two types of syntax using which we can write Jenkins pipeline.

  • Scripted Pipeline
  • Declarative pipeline

Both the syntax is built on same underlying pipeline sub-system.

Scripted Pipeline

Few points about scripted pipeline

  • It is the traditional way of writing Jenkins pipeline code/script.
  • It came into market many years back when Jenkins pipeline was launched.
  • Scripted pipeline uses a “general purpose DSL” which is groovy.
  • Most of the functionality of scripted pipeline is provided by groovy language.
  • Since scripted pipeline is powered by groovy, so it is very expressive. Developer/ DevOps engineer can write more complex pipeline code using “scripted pipeline syntax”.
  • Scripted pipeline can’t be written in Jenkinsfile, that is writing Jenkins pipeline in Jenkinsfile is not supported by scripted pipeline.
  • Syntax checking at the beginning of pipeline run is not available in scripted pipeline. For example, our scripted pipeline has 3 stage, and we have syntax error in 3rd stage. Now if we run the pipeline it will run stage-1 & 2, but in stage-3 it will fail. That is only at stage-3, the pipeline will fail. It is kind of time waste since we have to wait until 3rd stage to get to know about the error/issue.

Basic structure of scripted pipeline is

node {

}

Within this node block we need to mention various stages.

node ('node 1') {
  stage('stage 1') {
    //code
  }
  stage('stage 2') {
    //code
  }
  //any other stages can be added here
}

Declarative Pipeline

Few points about declarative pipeline

  • Scripted pipeline is not easy for everyone, to solve this issue declarative pipeline has been introduced.
  • It has more simplified syntax on top of pipeline sub-system.
  • It came into market after scripted pipeline.
  • Declarative pipeline uses “Jenkins DSL”.
  • The basic statements, expressions in declarative pipeline follow the same groovy’s syntax but at the top level pipeline must have a pipeline {} block.
  • Declarative pipeline can be written in Jenkinsfile.
  • Syntax checking at the beginning of pipeline run is available in scripted pipeline. For example, our declarative pipeline has 3 stage and in 3rd stage we have a syntax error. Now, if we run the pipeline, it will fail at the beginning only. So no time waste like scripted pipeline where we will get to know the issue/error at the concerned stage only.

Basic structure of declarative pipeline

pipeline {

}

Within the pipeline block we need to mention the agent block and stages block.

pipeline {
  agent { label 'dev node' } //any other agent can be added
  stages {
    stage('Stage 1') {
      steps {
        //code
      }
    }
    stage('Stage 2') {
      steps {
        //code
      }
    }
    //other stage can be added here
  }
}

 

Reference:

https://www.jenkins.io/doc/book/pipeline/

https://www.jenkins.io/doc/book/pipeline/syntax/

Thank You.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.