Jenkins Pipeline Tutorial: How to Create Jenkins Pipeline

The Jenkins Pipeline is a collection of procedures required for consistently creating and evaluating applications. It is intended to move the product from version control to a user-facing release by expanding continuous delivery practice capabilities.

Types of Jenkins Pipeline

Jenkins supports 2 types of pipelines.

1)Declarative: Declarative pipeline provided limited pre-defined syntax and its relatively new feature. It is easier to start as its is more suitable for users who are not very familiar with Groovy syntax.

2)Scripted: Scripted pipeline requires Groovy language knowledge to write the Jenkins pipeline. it is the traditional way of writing pipelines which provides control over the script and helps in the manipulation of the script workflow.

Jenkins Pipeline Syntax

1)Declarative pipeline syntax

1)Pipeline: This is the first block for declarative pipeline syntax. Inside this block, all processing steps like build, test, compile etc can be defined. This block is not available for the scripted pipeline and this must be the top-level section in declarative pipeline syntax.

pipeline
{

}

2)Agent: This tells Jenkins to allocate an executor or node and create a workspace for the pipeline.

pipeline
{
​ agent any   // Run the pipeline on any available agent/executor
 agent none  //No global agent for the entire pipeline and each stage must specify its    own agent.
}

3)Stages: This section contains a series of actions in the pipeline like build, test, deploy etc. There can be multiple stages in the pipeline. The stage is the section where work actually happens.

pipeline{
       agent any
       stages{
              stage('Build'){
                 ........
                }
              stage('Test'){
                 ........
                }
              stage('Deploy'){
                 ........
                }
              stage('Monitor'){
                 ........
                }
       }
}

4)Step: A step is a single task that defines what needs to be done in a particular stage.

pipeline{
     agent any
     stages{
          stage('Stage 1'){
              steps{
                //Perform steps related to this stage
              }
          }
          stage('Stage 2'){
              steps{
                //Perform steps related to this stage
              }
          }
     }

}

2)Scripted pipeline syntax

Let’s see the syntax of the scripted Jenkins pipeline.

1)Node: This block behaves similarly to the agent block but it allows for some additional configurations.

node {
}

2)Stage: This block can be a standalone or a group of stages like declarative pipeline syntax.

To learn more about the pipeline syntax please refer to this repo.

What is Jenkins File?

A Jenkins file is a text file that consists of the entire workflow as code. This file is written in Groovy DSL. With the help of this file, we can write the workflow steps that are required for functioning a Jenkins pipeline. The Jenkins file should be present in the SCM.

How to Create a Jenkins Pipeline?

Jenkins pipeline can be created using multiple ways. Let’s see common ways.

1)By Using Jenkins UI

Login Jenkins –> Go to Dashboard –> Select New item –> Type name –> Select Pipeline option –> Click Ok

Type pipeline code in the text area. For example, I have written a sample code of a declarative pipeline for echoing(Writing string to console) hello world.

pipeline {
    agent any 
    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello world!' 
            }
        }
    }
}

Apply and save. Click on build now.

Jenkins Pipeline Syntax

2)Pipeline script from SCM(How to create Jenkins File)

Create a text file and name it Jenkinsfile.txt. Place your pipeline code in this text file. Add this text file to the root folder of your project in the code repository.

pipeline{
	agent any
	stages{
		
		stage("Start Grid"){
			steps{
				sh "docker-compose up -d hub chrome firefox"
			}
		}
		stage("Run Test"){
			steps{
			
                            echo 'Run integration tests here...'
			}
		}
    
	}
	post{
		always{
			archiveArtifacts artifacts: 'output/**'
			sh "docker-compose down"
			sh "sudo rm -rf output/"
		}
	}
}

Go to the pipeline tab and select pipeline script from the SCM option. Select Git from the SCM dropdown. Enter your git repo URL and credentials. Next, set the branch and the Jenkins file path. Click on Apply and save.

Jenkins pipeline from SCM

Now the pipeline is configured. Click on build now. Once the pipeline starts execution we can see the results displayed on the stage view.

Post Build Actions in Jenkinsfile

We can add post-build actions in the Jenkins file. These actions will be executed every time after work is finished. There are several steps that can be used for post-build actions. Some of them are as follows:-

1)Always: The logic that is written inside always will execute always no matter if the build fails or succeeds. For example, we want to notify our team whether the build failed or passed, so we can configure email actions inside the always block of post-build actions.

pipeline {
  agent any
  stages {
    
  }
  post {
    always {
      echo ‘I will always execute this!’
    }
  }
}

2)Success: Executes when the current build is executed successfully.

3)Failure: Executes when the current build is failed.

 post {
    failure {
      mail to: [email protected], subject: ‘The Pipeline success :(‘
    }
  }

4)CleanUp: Clean up post-build action is used for clean-up actions like deleting artifacts, temp files etc after the build execution.

Pipelines offer continuous delivery starting from little modifications to significant changes that are originating from the source code. The pipeline is very useful while integrating several environments and releasing software.

To learn more about scheduling automated jobs in Jenkins refer to this article.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading