Jenkins Pipeline if statement

Jenkins Pipeline if statement | Complete tutorial with examples [2023]

This is the fifth part of the Jenkins pipeline example. In this blog, we will understand Jenkins pipeline if statement.

Jenkins pipeline if statement

In Jenkins, there are two ways to define the Pipeline. One is scripted syntax, and the other uses declarative syntax. Each syntax has its advantages and disadvantages. You can check the below blogs to have a basic understanding of the Jenkins scripted and declarative Pipeline.

Jenkins pipeline syntax

This session will teach us to use the if-else statement in Jenkins’s declarative and scripted Pipeline.

Jenkins scripted pipeline if else statement

We will create a Jenkins declarative and scripted pipeline with an if-else statement in this session. Click on the link to get more information about Jenkins scripted pipeline.

To create a new pipeline in Jenkins Goto, the Jenkins UI and click on New item. Give the pipeline name as Jenkins pipeline-if statement, select Pipeline, andclick the ok button.

Now go to the pipeline session and paste the below code.

node {
    stage('Step1') {
        if (env.BRANCH_NAME == 'main') {
            echo 'Hello from main branch'
        } else {
            sh "echo 'Hello from ${env.BRANCH_NAME} branch!'"
        }
    }
}
Jenkins scripted pipeline if else statement

Click on the save button to save the Pipeline.

Pipeline explanation

  • The scripted Pipeline always starts with node.
  • In the stage block, we are mentioning the stage name as step1
  • After the stage block, we write the code which will perform the actual work.
  • We are using the if statement to check BRANCH_NAME where Jenkins is running. If the branch name is main, then print “Hello from main branch” else print “Hello from ${env.BRANCH_NAME} branch!”

Click on the save button to save the Pipeline. Click on the Build now button to build the Pipeline. Once the Pipeline runs successfully, you will get the below output in logs.

Jenkins scripted pipeline if-else statement logs

Jenkins declarative pipeline if else statement

In the same way, you can define the if-else statement in Jenkins’sdeclarative Pipeline. Below is the code snippet to run the if-else statement in Jenkins declarative pipeline

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'main') {
                        echo 'Hello from main branch'
                    }  else {
                        sh "echo 'Hello from ${env.BRANCH_NAME} branch!'"
                    }
                    }
            }
        }
    }
}

Pipeline 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 an agent as any
  3. Stages/stage
    • stages block consists of different executable stage blocks. 
    • At least one stage block is mandatory inside the stages block.
    • In this example, we have defined a single stage as “Hello”
  4. Steps
    • Steps blocks consist of the actual operation which needs to be performed inside Jenkins.
  5. Script
    • In the script session, we can define scripted pipeline codes.
    • In many use cases, the script step is unnecessary in Declarative Pipelines, but it can provide a useful “escape hatch.”
    • In the script session, we are using the if statement to check BRANCH_NAME where Jenkins is running. If the branch name is main, then print “Hello from main branch” else print “Hello from ${env.BRANCH_NAME} branch!”

Paste the above code in the pipeline session, save the Pipeline and click on the Build now button.

Jenkins declarative pipeline if else statement

Once the Pipeline runs successfully, you will get the below output in logs.

Jenkins declarative pipeline if else statement

Jenkins pipeline multiple if statement

In Jenkins declarative/scripted pipeline, we can define multiple if-else blocks and use them in the Pipeline as per the use case. Below is the code snipped, which a user can use to define the multiple if-else blocks in Jenkins.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'main') 
                        {
                        echo 'Hello from main branch'
                        }
                    if (env.BRANCH_NAME == 'null') 
                        {
                        echo 'Hello from null branch'
                        }
                    else {
                        sh "echo 'Hello from ${env.BRANCH_NAME} branch!'"
                        }
                    }
            }
        }
    }
}

Copy-paste the above code into the Jenkins pipeline script and click on Build now. On a successful run, you will get the below output.

jenkins multiple if statement

Nested if else in Jenkins pipeline

In Jenkins Pipeline, a user can use nested if statements to create more complex logic. you can have a nested if statement within the block.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'main') {
                        echo 'Hello from main branch'
                        if (env.BUILD_USER == 'admin') {
                        	echo 'Job run by admin user'
                        } else {
                        	echo 'Job run by other user'
                        }
                    }  else {
                        sh "echo 'Hello from ${env.BRANCH_NAME} branch!'"
                    	}
                    }
            }
        }
    }
}

Pipeline Explanation:

  • The above Jenkins pipeline checks the value of the BRANCH_NAME environment variable to determine which branch the pipeline is currently running on.
  • If the branch is “main”, it will print “Hello from main branch” and then check the value of the BUILD_USER environment variable to determine if the user is “admin” or not.
  • If the user is “admin”, it will print “Job run by admin user”, otherwise it will print “Job run by other user”.
  • If the branch is not “main”, the pipeline will execute a shell command that prints “Hello from [branch name] branch!”

You can also use else if statement to chain multiple conditions together, like below:

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'main') {
                        echo 'Hello from main branch'
                    } else if (env.BUILD_USER == 'admin') {
                        echo 'Job run by admin user'
                    } else {
                        sh "echo 'Hello from ${env.BRANCH_NAME} branch!'"
                    }
                }
            }
        }
    }
}

Jenkins pipeline if a string contains

This session will understand how to use the Jenkins pipeline to search for a particular string using an if statement.

Suppose we wish to write a Jenkins pipeline to check if Jenkins is running on port 8080. we can use the below Pipeline to achieve that.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    if (env.HUDSON_URL.contains('8080')) {
                        echo 'Jenkins is running on port 8080'
                    }  
                    else {
                        sh "Jenkins is not running on port 8080"
                    }
                    }
            }
        }
    }
}

Code Explanation:

  • Initially, we followed the standard Jenkins declarative pipeline syntax to define the pipeline.
  • After the steps block, we need to define a script block, under which we will be defining the if-else statement.
  • In the if block we are checking the Jenkins URL which is present in the HUDSON_URL environment variable. You can get the list of all environments in Jenkins by going to the URL: http://<jenkins_url>/env-vars.html/.
  • If the HUDSON_URL string variable contains port 8080 then print Jenkins is running on port 8080 else print Jenkins is not running on port 8080.

Copy-paste the above code into the pipeline session and click on build now.

Jenkins pipeline if string contains

You will get the below output on a successful run.

Jenkins if string contains logs

Jenkins Pipeline when statement

Similar to the if-else statement, we can also use the when statement in the Jenkins pipeline. This blog will learn how to use Jenkins when statement in the declarative and scripted pipelines.

You can use the below code snippet to create a declarative pipeline with a when statement.

pipeline {
    agent any
    stages{
        stage('Step1') {
            when {
                expression { env.BRANCH_NAME != 'master'}
                }
            steps {
                echo "Run this stage - ony if the branch is not main"
                }
}
}
}

Pipeline 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 an agent as any
  3. Stages/stage
    • stages block consists of different executable stage blocks. 
    • At least one stage block is mandatory inside the stages block.
    • In this example, we have defined a single stage as “Step1”
  4. when
    • when statement is used to check the branch name environment variable. All the Jenkins environments can be accessed by using env.<varaible_name>.
  5. In the final step using echo command, we are printing output like “Run this stage-Only if the branch is not main” 

Once the job runs successfully, you will get the below output

Jenkins Pipeline when statement

Jenkins scripted Pipeline when statement

when statement does not work with scripted Pipeline syntax. You can use the declarative pipeline system to define the Pipeline; otherwise, you can use the if-else statement instead of the when statement.

Conclusion

We have learned how to use the if-else and multiple if statements in the Jenkins pipeline in this blog. We have also learned how to write if-else statements in Jenkins declarative and scripted pipeline. Please do let me know if you are facing any issues while following along.

More to Read

Jenkins workflow

Jenkins pipeline example hello-world

Jenkins pipeline example multiple stages

Jenkins pipeline define a variable

Leave a Comment

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

Scroll to Top