Hi All, this document shows how to use the choice parameter variable in the Jenkins pipeline.
Jenkins is a widely used CICD tool that supports a lot of integrations. There are certain use cases to have choice based parameter variables for Jenkins Job. Some cases will be to have true or false variables in other cases you will have options like 1, 2, 3. This allows the user who runs Jenkins’s job will have predefined options as a parameter rather than entering the inputs.

One simple use case if you are using a single Jenkins job for deploying multiple environments, where you will have a conditional statement to pick which environment the build needs to be moved. In this case, We can choose the choice parameter in which the user selects the environment rather than entering as a variable. There a lot of chances the job to get failed when the parameters come as case sensitive. Either job should be written in a way that handles the case sensitivity.

Another parameter that can be used in a boolean parameter which accepts true or false. Let’s say do you need a backup before deploying the build. This is one use case to choose the boolean parameter.

Based on the above-mentioned use cases I have created a sample pipeline that takes choice based parameters.

Below are the screenshots of Build with parameters.

Normal Jenkins
Here you can see a drop-down option to select the environment.


With Blue ocean
In the Blue ocean theme, you can exactly see the option.

For your Jenkins job you can use the below-mentioned parameter, which can be changed as per your requirement.

    parameters{
        
           choice(name: 'Environment',choices: 'dev1\ndev2\ndev3',description: 'which environment?')
            booleanParam(name: 'backup_required',defaultValue: true, description: 'do you need backup ?')
        
    }

Below is the sample Jenkins job.

pipeline{
    parameters{
        
           choice(name: 'Environment',choices: 'dev1\ndev2\ndev3',description: 'which year?')
            booleanParam(name: 'backup_required',defaultValue: true, description: 'do you need backup ?')
        
    }
    agent{
        label "node"
    }
    stages{
        stage("A"){
            steps{
                echo "========executing A========"
            }
            post{
                always{
                    echo "========always========"
                }
                success{
                    echo "========A executed successfully========"
                }
                failure{
                    echo "========A execution failed========"
                }
            }
        }
    }
    post{
        always{
            echo "========always========"
        }
        success{
            echo "========pipeline executed successfully ========"
        }
        failure{
            echo "========pipeline execution failed========"
        }
    }
}

© 2020, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points
Categories: Linux

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

0 Comments

Leave a Reply

Keep ahead of COVID-19

Follow the below safety measures as precautionary steps to avoid COVID-19 1. Use face mask in public Places 2. Use Hand sanitizers 3. Maintain Social Distancing 4. Avoid going to crowded places 5. Avoid touching eyes, nose and mouth