Select Page
Automation Testing

A Thorough Jenkins Pipeline Tutorial for Beginners

Achieving Continuous Delivery is no child's play, but our Jenkins Pipeline Tutorial can make it easier for you to achieve your goal.

A Thorough Jenkins Pipeline Tutorial for Beginners - Blog

Jenkins has risen to become an expert in Continuous Integration, Continuous Testing, & Continuous Delivery, and assumes a critical role in conveying great applications or items. It utilizes a component called the Jenkins pipeline for achieving Continuous Delivery (i.e.) the capacity to deliver applications routinely for a long stretch. This Jenkins pipeline guarantees that the product is consistently prepared for creation. As a leading QA Company, we have been using the Jenkins Pipeline and found it to be very effective. So if you are looking for a Jenkins Pipeline Tutorial that covers everything from the basics of the pipeline to learning how to perform parallel execution, you’ve found it.

What is a Jenkins Pipeline?

A pipeline in the Jenkins CI/CD can be defined as a series of events or even tasks that are interconnected to each other in a specific order. To put it in simpler terms, the Jenkins pipeline can also be characterized as a set of modules or plugins that enable the implementation & integration of the Continuous Delivery pipelines within Jenkins. It has an expandable automation system that can be used to build basic or complicated ‘template’ distribution pipelines via the Domain-specific language (DSL) used in the pipeline. Continuous Delivery in the Jenkins pipeline comprises 4 major states,

  • Build
  • Deploy
  • Test
  • Release

We will be taking a closer look at these states later on in the Jenkins Pipeline Tutorial, but first, let’s take a peek at the many advantages that the Jenkins pipeline CI/CD has to offer.

Advantages of the Pipeline:

  • It can divide the jobs into parts (build /test /deploy) and each part can run in each agent.
  • Parallel execution of stages is easy to configure and so it can be instrumental in saving time.
  • Each stage can execute the different versions of JDK/MVN versions.
  • It can be retriggered even from a failed stage.
  • Visualizing the build flow becomes possible.
  • The build can hold until the user gives the input.
  • Version control and code reviews are made easier.
  • We can pause and restart the build as and when we wish.
  • It will automatically be created as sub-branches in a multi-branch pipeline script.

Pipeline’s Basic Keywords:

Knowing the basic keywords that will be used in the pipeline is the first step that we will be taking in our Jenkins Pipeline Tutorial. So let’s see what these keywords are and how you can use them in the pipeline. We’ll start with ‘Steps’.

Steps
  • Steps have to be written inside the stage directive.
  • Steps contains the command or scripts that we’ve used in the build.
  • One step’s directive should be there in the stage directive.
Stage
  • Stage defines a particular stage (build/test/deploy/..) of our job.
  • There has to be a minimum of at least one stage.
  • The name of the stage will be displayed on the Jenkins dashboard.
Stages

Stage and Stages are two different keywords that you shouldn’t confuse yourself with.

  • It contains a sequence of the stages we saw earlier.
  • There has to be at least one stage.
Agent
  • It defines where we need to run our pipeline script. (Master/Slave/Container)
Stage color

So using color, we will be able to find out the current status of the stage.

  • White – The stage hasn’t yet been executed.
  • Green – The stage is a success.
  • Blue Lines – The stage is being executed.
  • Red Line or Red Lines – The stage has failed.
  • Red (If in case few stages were a success and one failed, it will show red even if the few have been successful)

Types of Pipeline

1. Declarative pipeline

2. Scripted pipeline

So as seen above, there are two types of the pipeline, and the declarative pipeline is the recent addition that has the more simplified and opinionated syntax when compared to the scripted pipeline. Now let’s take a look at the syntax for these types.

Declarative Pipeline syntax:
Pipeline {
      agent any 
      stages {
         stage(‘Build’) {
              Steps {
             }
        }
stage(‘Test’){
          steps {
        }
     }
stage(‘Deploy’){
      steps {
            }
       }
   }
} 
Scripted Pipeline Syntax:
node {
  stage(‘Build’) {
     }
stage(Test’) {
    }
stage(‘Deploy’) {
    }
}

Variables in the pipeline:

What is a variable?

A variable is used to store values. There are two types of variables, and they are predefined and user-defined variables.

<variable name> = <variable value>

Predefined Variable:

pipeline{ 
    agent any 
    stages{ 
      stage('pre'){ 
      steps{ 
      echo " predefined variable $BUILD_NUMBER $WORKSPACE " 
              } 
       }
   }
}

Output:

Pre-defined Variable Output - Jenkins Pipeline Tutorial

User-defined Variable:

User-defined Variable we can define in root level or stage level

pipeline { 
    agent any 
         environment{ 
              MYHOME="Chennai" 
            } 
            stages{ 
            stage('User'){ 
            steps{ 
             echo " userdefined variable $MYHOME " 
                       } 
               } 
         } 
}

Output:

User defined Variable Output - Jenkins Pipeline

Parameters in Pipeline:

Parameters are used to pass the following types of data dynamically.

  • String
  • Text
  • Boolean
  • Choice
  • Password
  • File
Implementation of Parameters in Pipeline:
pipeline{
 agent any
 parameters {
string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') 
text(name: 'DEPLOY_TEXT', defaultValue: 'RnD\nJenkins\nPipeline\n', description: '')
booleanParam(name: 'Are You Have Permission to Deploy', defaultValue: true, description: 'Toggle this value')
 choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
 file(name: 'FILE', description: 'Some file to upload')
 password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'A secret password')
}
 stages{
 stage('string'){
 steps{
 echo " string $DEPLOY_ENV"
 }
 }
stage('text'){
 
 steps{
 echo " text $DEPLOY_TEXT"
 }
 }
stage('booleanParam'){
 
 steps{
 script{
 if(TOGGLE){
 echo " now execute, booleann is true"
}else{
 echo " Dont execute, boolean is true"
}
 }
}
 }
stage('choice'){

 steps{
script{
if(DEPLOY_ENV=='staging'){
echo " choice $CHOICE"
}
 }
}
 }
stage('file'){
 
 steps{
 echo " file $FILE"
         }
 }
stage('password'){
 
 steps{
 echo " password $PASSWORD"
          }
      }
   }
}

Output:

Parameters in Pipelines

Parallel Execution in Jenkins pipeline:

We will be focusing on the parallel build using the Jenkins declarative pipeline in our Jenkins Pipeline Tutorial now. So you can trigger your build system by Jenkins if there are some steps that could possibly run at the same time since they have no dependencies. By following this method, you will speed up your build process and save time for other sequential steps. As one of the best automation testing service providers, we don’t just stop with using the best tools, we also focus on using them in the best way possible.

Parallel Exection in jenkins Pipeline Tutorial

Parallel Execution:
pipeline {
    agent any
    
    stages {
        stage ('My Java Project Test Scenarios'){
            parallel {
        stage('Unite Test'){
            steps {
                echo 'Unit test Completed'
                sleep 5
            }
        }
        stage('Integration Test'){
            steps {
                echo 'Integration Completed'
                sleep 5
            }
        }
        stage('Security Test'){
            steps {
                echo 'Security Completed'
                sleep 5
            }
        }
        stage('Selenium UI Test'){
            steps {
                echo 'Selenium UI Test Completed'
                sleep 5
            }
        }
    }
              
}
 }
}

Output:

Parallel Exection Output

Conclusion

We hope you enjoyed reading our Jenkins Pipeline Tutorial and found it to be informative at the same time. As promised we have covered everything including the basics of the Jenkins pipeline, seen its advantages, explored all the perquisites like variables and parameters that you will need to know to use it effectively. So make sure you don’t just try the Jenkins pipeline, make sure you use it effectively.

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


Jenkins has risen to become an expert in Continuous Integration, Continuous Testing, & Continuous Delivery, and assumes a critical role in conveying great applications or items. It utilizes a component called the Jenkins pipeline for achieving Continuous Delivery (i.e.) the capacity to deliver applications routinely for a long stretch. This Jenkins pipeline guarantees that the product is consistently prepared for creation. As a leading QA Company, we have been using the Jenkins Pipeline and found it to be very effective. So if you are looking for a Jenkins Pipeline Tutorial that covers everything from the basics of the pipeline to learning how to perform parallel execution, you’ve found it.

What is a Jenkins Pipeline?

A pipeline in the Jenkins CI/CD can be defined as a series of events or even tasks that are interconnected to each other in a specific order. To put it in simpler terms, the Jenkins pipeline can also be characterized as a set of modules or plugins that enable the implementation & integration of the Continuous Delivery pipelines within Jenkins. It has an expandable automation system that can be used to build basic or complicated ‘template’ distribution pipelines via the Domain-specific language (DSL) used in the pipeline. Continuous Delivery in the Jenkins pipeline comprises 4 major states,

  • Build
  • Deploy
  • Test
  • Release

We will be taking a closer look at these states later on in the Jenkins Pipeline Tutorial, but first, let’s take a peek at the many advantages that the Jenkins pipeline CI/CD has to offer.

Advantages of the Pipeline:

  • It can divide the jobs into parts (build /test /deploy) and each part can run in each agent.
  • Parallel execution of stages is easy to configure and so it can be instrumental in saving time.
  • Each stage can execute the different versions of JDK/MVN versions.
  • It can be retriggered even from a failed stage.
  • Visualizing the build flow becomes possible.
  • The build can hold until the user gives the input.
  • Version control and code reviews are made easier.
  • We can pause and restart the build as and when we wish.
  • It will automatically be created as sub-branches in a multi-branch pipeline script.

Pipeline’s Basic Keywords:

Knowing the basic keywords that will be used in the pipeline is the first step that we will be taking in our Jenkins Pipeline Tutorial. So let’s see what these keywords are and how you can use them in the pipeline. We’ll start with ‘Steps’.

Steps
  • Steps have to be written inside the stage directive.
  • Steps contains the command or scripts that we’ve used in the build.
  • One step’s directive should be there in the stage directive.
Stage
  • Stage defines a particular stage (build/test/deploy/..) of our job.
  • There has to be a minimum of at least one stage.
  • The name of the stage will be displayed on the Jenkins dashboard.
Stages

Stage and Stages are two different keywords that you shouldn’t confuse yourself with.

  • It contains a sequence of the stages we saw earlier.
  • There has to be at least one stage.
Agent
  • It defines where we need to run our pipeline script. (Master/Slave/Container)
Stage color

So using color, we will be able to find out the current status of the stage.

  • White – The stage hasn’t yet been executed.
  • Green – The stage is a success.
  • Blue Lines – The stage is being executed.
  • Red Line or Red Lines – The stage has failed.
  • Red (If in case few stages were a success and one failed, it will show red even if the few have been successful)

Types of Pipeline

1. Declarative pipeline

2. Scripted pipeline

So as seen above, there are two types of the pipeline, and the declarative pipeline is the recent addition that has the more simplified and opinionated syntax when compared to the scripted pipeline. Now let’s take a look at the syntax for these types.

Declarative Pipeline syntax:
Pipeline {
      agent any 
      stages {
         stage(‘Build’) {
              Steps {
             }
        }
stage(‘Test’){
          steps {
        }
     }
stage(‘Deploy’){
      steps {
            }
       }
   }
} 
Scripted Pipeline Syntax:
node {
  stage(‘Build’) {
     }
stage(Test’) {
    }
stage(‘Deploy’) {
    }
}

Variables in the pipeline:

What is a variable?

A variable is used to store values. There are two types of variables, and they are predefined and user-defined variables.

<variable name> = <variable value>

Predefined Variable:

pipeline{ 
    agent any 
    stages{ 
      stage('pre'){ 
      steps{ 
      echo " predefined variable $BUILD_NUMBER $WORKSPACE " 
              } 
       }
   }
}

Output:

Pre-defined Variable Output - Jenkins Pipeline Tutorial

User-defined Variable:

User-defined Variable we can define in root level or stage level

pipeline { 
    agent any 
         environment{ 
              MYHOME="Chennai" 
            } 
            stages{ 
            stage('User'){ 
            steps{ 
             echo " userdefined variable $MYHOME " 
                       } 
               } 
         } 
}

Output:

User defined Variable Output - Jenkins Pipeline

Parameters in Pipeline:

Parameters are used to pass the following types of data dynamically.

  • String
  • Text
  • Boolean
  • Choice
  • Password
  • File
Implementation of Parameters in Pipeline:
pipeline{
 agent any
 parameters {
string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') 
text(name: 'DEPLOY_TEXT', defaultValue: 'RnD\nJenkins\nPipeline\n', description: '')
booleanParam(name: 'Are You Have Permission to Deploy', defaultValue: true, description: 'Toggle this value')
 choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
 file(name: 'FILE', description: 'Some file to upload')
 password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'A secret password')
}
 stages{
 stage('string'){
 steps{
 echo " string $DEPLOY_ENV"
 }
 }
stage('text'){
 
 steps{
 echo " text $DEPLOY_TEXT"
 }
 }
stage('booleanParam'){
 
 steps{
 script{
 if(TOGGLE){
 echo " now execute, booleann is true"
}else{
 echo " Dont execute, boolean is true"
}
 }
}
 }
stage('choice'){

 steps{
script{
if(DEPLOY_ENV=='staging'){
echo " choice $CHOICE"
}
 }
}
 }
stage('file'){
 
 steps{
 echo " file $FILE"
         }
 }
stage('password'){
 
 steps{
 echo " password $PASSWORD"
          }
      }
   }
}

Output:

Parameters in Pipelines

Parallel Execution in Jenkins pipeline:

We will be focusing on the parallel build using the Jenkins declarative pipeline in our Jenkins Pipeline Tutorial now. So you can trigger your build system by Jenkins if there are some steps that could possibly run at the same time since they have no dependencies. By following this method, you will speed up your build process and save time for other sequential steps. As one of the best automation testing service providers, we don’t just stop with using the best tools, we also focus on using them in the best way possible.

Parallel Exection in jenkins Pipeline Tutorial

Parallel Execution:
pipeline {
    agent any
    
    stages {
        stage ('My Java Project Test Scenarios'){
            parallel {
        stage('Unite Test'){
            steps {
                echo 'Unit test Completed'
                sleep 5
            }
        }
        stage('Integration Test'){
            steps {
                echo 'Integration Completed'
                sleep 5
            }
        }
        stage('Security Test'){
            steps {
                echo 'Security Completed'
                sleep 5
            }
        }
        stage('Selenium UI Test'){
            steps {
                echo 'Selenium UI Test Completed'
                sleep 5
            }
        }
    }
              
}
 }
}

Output:

Parallel Exection Output

Conclusion

We hope you enjoyed reading our Jenkins Pipeline Tutorial and found it to be informative at the same time. As promised we have covered everything including the basics of the Jenkins pipeline, seen its advantages, explored all the perquisites like variables and parameters that you will need to know to use it effectively. So make sure you don’t just try the Jenkins pipeline, make sure you use it effectively.