Jenkins multiple stages pipeline

Jenkins multiple stages pipeline | Complete tutorial in [2022]

This is the second part of the Jenkins pipeline example. In this blog, we will understand how to write a Jenkins multiple stages pipeline.

For this demo, I will be using the Jenkins declarative pipeline. Follow this link to get more information about the Jenkins pipeline syntax.

Jenkins pipeline stages

Before working on the actual Pipeline, it is crucial to understand the Jenkins pipeline stages and why we need to define the pipeline in stage and step blocks.

A stage block in the Jenkins is used to segregate the different tasks performed in the Jenkins pipeline. The tasks can be related to Building, Deploying, or testing any application. The step blocks follow the stages block.

Below is the syntax for defining the stages block in Jenkins.

pipeline {
    agent any
    stages {
        stage('build') {

Jenkins pipeline steps

A step block is used to define a single task. A step block instructs Jenkins to perform any task at any particular time. For example, to execute any command in the shell, we have to use “sh” in steps followed by the actual command. i.e.

steps{
  sh "date"
}

Jenkins pipeline example multiple stages

In this session, we will learn how to write a Jenkins pipeline that contains multiple stages using Declarative pipeline syntax.

To create a new pipeline in Jenkins, connect to Jenkins instance and click on New item.

Supply the item name, select the project as Pipeline, and click on the ok button.

Jenkins multiple stages pipeline

Now goto pipeline session and paste the below code

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                echo 'Build stage'
            }
        }
        stage('Deploy') {
            steps {
                sh 'date'
            }
        }
    }
}
Jenkins multiple stages pipeline

Code explanation

  1. Pipeline
    • The Declarative pipeline should start with the mandatory pipeline block.
  2. Agent
    • Agent signifies where the Jenkins build job should run. In this case, we have selected agent as any
  3. Stages/stage
    • stages block consists of different executable stage blocks. 
    • At least one stage block is mandatory inside stages block.
    • In this example we have defined two stages(build and Deploy)
  4. Steps
    • Steps blocks consists of the actual operation which needs to be performed inside jenkins.
    • In the build stage,we are printing “Build Stage” in console
    • In the Deploy stage,we are using shell module(sh) to print the system date.

Click on the save button to save the Pipeline.

Now click on Build now button(1) to build the Pipeline

Jenkins multiple stages pipeline

Once the job runs successfully, you can view the logs as highlighted by number 2

Click on #1 to view logs and then click on the console output

Jenkins multiple stages pipeline logs

From the above result, it is clear that the Pipeline with two stages ran successfully, and the printed Build stage and date

Jenkins pipeline multiple agents in multiple stages

Users can also specify agents in the stage block. This agent can be the same or different and decided based on the use case. Below is the Jenkins pipeline with multiple stages and agents defined.

pipeline {
    agent any
    stages {
        stage('build') {
            agent {
              docker "imagename1"
               }
            steps {
                echo 'Build stage'
            }
        }
        stage('Deploy') {
           agent {
              docker "imagename2"
               }
            steps {
                sh 'date'
            }
        }
    }
}

Jenkins pipeline parallel stages

Sometimes the user needs to run the Pipeline in parallel stages instead of default sequential execution(by default in Jenkins). In Jenkins, this can be achieved using steps in the parallel block.

The complete pipeline block looks like below.

pipeline {
    agent any
    stages {
        stage('build') {
          parallel{
            stage("Stage 1"){
              agent any
              steps{
                echo "stage 1"
                }
              }
            stage("Stage 2") {
              agent any
              steps{
                echo "stage 2"
                }
              }
        }
    }
}
}

Paste the above code in the pipeline script block and build the Pipeline. On a successful build, you will get below output.

Jenkins pipeline parallel stages
  • The user needs to define a parallel block inside the stage block.
  • Inside the parallel block, the user can add the stages he wants to execute in parallel. In the above example, we have defined 2 stages: Stage 1 and Stage 2.
  • If the user needs to run the stages in multiple agents, he can specify the agents under the agent block. In the above example, I am using any as the agent.

Conclusion

In this blog, we have learned about the Jenkins pipelines stages. We have seen Jenkins pipeline multiple stages and Jenkins parallel pipeline stages example. Please let me know if you are facing any issues while following along.

More to Explore

Deploy Jenkins on Kubernetes using Jenkins operator

Jenkins scripted pipeline

Jenkins workflow

Jenkins pipeline example hello-world

Leave a Comment

Your email address will not be published.

Scroll to Top