Continuous Delivery with Jenkins Pipeline.

About Jenkins

Jenkins is an Open source, Java-based automation tool. This tool automates the Software Integration and delivery process called Continuous Integration and Continuous Delivery. Jenkins support various popular Version control system, Software build, and delivery tools. Over the Years, Jenkins become giant in CICD process, especially new features like Jenkins Pipelines (Scripted and Declarative Pipeline) makes the delivery process very easy and help Team to adopt DevOps easily.

Jenkins, From Zero To Hero: Become a DevOps Jenkins Master

Become a DevOps Master learning Jenkins & integrations with powerful tools like Docker, Ansible, AWS, GIT & more!
Limited time 97% offer on Udemy.

Jenkins pipeline

Jenkins Pipeline is a stack of Jenkins plugins and other tools which helps implementing and continuous integration and delivery pipelines. In Jenkins, Pipelines are written in DSL code which implements this continuous integration and delivery pipeline jobs.

Get the pipeline plugin from the Jenkins plugin market place and install into the Jenkins instance. For the same, Go to Manage Jenkins > Manage Plugins. > Available tab, search for “build pipeline”.

This Jenkins pipeline can be built using Web UI or Scripted Jenkinsfile. This Jenkinsfile is written with Groovy DSL and updated in configuration page on the Jenkins job. Overall, Jenkinsfile can also be called pipeline as code.

Types of Jenkins pipeline

There are two type of Jenkins pipeline based on which format the Jenkinsfile is written.

  1. Declarative pipeline
  2. Scripted pipeline

Declarative Pipeline

The Declarative pipeline is a new feature that is added to create the pipeline. This is basically written in a Jenkinsfile which can be stored into a source code management system such as Git. Declarative pipelines is an ideal solution for the simple continuous delivery pipeline as it has very limited and pre-defined structure.

Lets see the structure and syntax of the Declarative pipeline.

The Agent is where the whole pipeline runs. Example, Docker. The Agent has following parameters:

  • any – Which mean the whole pipeline will run on any available agent.
  • none – Which mean all the stages under the block will have to declared with agent separately.
  • label –  this is just a label for the Jenkins environment
  • docker –  this is to run the pipeline in Docker environment.

The Declarative pipeline code will looks like this:

pipeline {
  agent { label 'node-1' }
  stages {
    stage('Source') { 
      steps {
        git 'https://github.com/digitalvarys/jenkins-tutorials.git''
      }
    }
    stage('Compile') { 
      tools {
        gradle 'gradle4'
      }
      steps {
        sh 'gradle clean compileJava test'
      }
    }
  }
}

Scripted Pipeline

The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, Scripted pipeline is written in Jenkins file on web UI of Jenkins. Unlike 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 script extensively. This helps developers to develop advance and complex pipeline as code.

Let us see the structure and syntax of the scripted pipeline

Node Block:

Node is the part of the Jenkins architecture where Node or agent node will run the part of the workload of the jobs and master node will handle the configuration of the job. So this will be defined in the first place as

node {
}
Stage Block:

Stage block can be a single stage or multiple as the task goes. And it may have common stages like

  • Cloning the code from SCM
  • Building the project
  • Running the Unit Test cases
  • Deploying the code
  • Other functional and performance tests.

So the stages can be written as mentioned below:

stage {
}

So, Together, the scripted pipeline can be written as mentioned below.

stage {
}
So, Together, the scripted pipeline can be written as mentioned below.
node ('node-1') {
  stage('Source') {
    git 'https://github.com/digitalvarys/jenkins-tutorials.git''
  }
  stage('Compile') { 
    def gradle_home = tool 'gradle4'
    sh "'${gradle_home}/bin/gradle' clean compileJava test"
  }
}

Run the pipeline

Now lets configure the Jenkins pipeline. Following are the steps to create Jenkins Pipeline.

Step 1:

Select “New Item” from the dashboard of the Jenkins instance

Step 2:

Name the item and select pipeline as the style of the job item.

Step 3:

Scroll down and select the definition of the Pipeline. Here we get two options.

  1. One is the Pipeline script from SCM which is for Declarative Pipeline
  2. Another is the Pipeline script which is for Scripted Pipeline that we will write directly on UI.

If it is the Pipeline Script, which will be written directly on UI, Need to write it on Script place (Scripted pipeline)

If we are selecting the Pipeline script from SCM definition, then we need to select the Git > SCM Repository URL > Credentials. (Declarative Pipeline).

Declarative Pipeline from SCM

In the Declarative pipeline, We need to select the repository of the Jenkinsfile, Credentials, Branch and the path of the Jenkinsfile as shown in the above screenshot.

Now let us see how these jobs are executed.

Scripted Pipeline Execution

For testing, we are going to use the following code for the pipeline.

node {
      for (i=0; i<2; i++) { 
           stage "Stage #"+i
           print 'This is the stage #' +i
           if (i==0)
           {
               echo 'Running on Stage #0'
           }
           else {
               build 'Tutorial-pipeline'
               echo 'Running on Stage #1'
           }
      }
}

Basically,

  • This Script will run two stages as we mentioned in the “For loop”.
  • Next, the Condition will execute the stages mentioned in the block when i’ = 0.
  • Next condition will execute another job from the Jenkins when ‘i’ not equal to 0.

So once we run the job we will get output in the dashboard (As mentioned in the screenshot)

Scripted Pipeline Execution Result

Declarative Pipeline Execution

Let us take the Jenkinsfile from the SCM. In our case, we will take this from the Github. The script inside the Jenkinsfile will look like this

pipeline {
	agent any
		stages {
		stage('One') {
			steps {
				echo 'I am executing stage 1'
			}
		}
		stage('Two') {
			steps {
				input('In pipeline, We take decision. Can we proceed?')
			}
		}
		stage('Three') {
			steps {
				echo "Running Stage Three"
			}
		}
		stage('Four') {       
			steps {
			echo "Running another test job"
			}        
		}
	}
}

So basically, this script is having 4 stages. And each stage is running in any defined agent. For the Demo purpose, I made it so simple. Once we execute this, we can see the result in the dashboard as shown in the screenshot.

Declarative Pipeline Execution Result

Ideally, we can connect any Build job with these scripts to upstream or downstream the pipelines. So this pipelines will provide continuous delivery right from the small changes from the major changes that are happening from the source code. Especially the pipeline is very useful when we connect multiple environments together and deliver the software. Like, Build – Test – Deploy on every environment. say Dev environment to QA or UAT to Stage to Prod.

Stay tuned. We will see the detailed tutorial of the Scripted and Declarative pipelines and tools like Blue oceans in upcoming articles.

1 thought on “Continuous Delivery with Jenkins Pipeline.”

  1. Pingback: scripted sign up login - loginen.com

Leave a Reply