Contents

Jenkins Variables and Parameters

Jenkins variables and parameters explained

Website Visitors:

Variables and parameters allow you to define values that can be referenced throughout your pipeline. They let you customize builds by assigning variable values. You’ve probably heard about them before but maybe you don’t know just what they do or how they work. That’s okay – In this tutorial, we’ll talk about what variables and parameters are and why you might need them.

Variables

Variables allows you to use dynamic values in our pipeline scripts. Jenkins supports three types of variables:

  • Environment variables,

  • Current build variables

  • Parameters

Environment Variables:

Capital letters are used for all environment variable names in pipeline scripts as it is easy to identify them. Environment variables can bet set globally or locally. Environment variables which are declared at the beginning of the pipeline (at the top) are applied globally to all the pipeline steps and the ones declared in a stage are only used within that stage. Environment variables can be referred in different ways as shown below:

1
2
3
4
5
6
echo DISABLE_AUTH # using the env variable name directly
echo env.DISABLE_AUTH # using env keyword
echo "$env.DISABLE_AUTH" # using env, $ and quotes
echo "$DISABLE_AUTH" # using quotes and $
echo "${env.DISABLE_AUTH}" # using $, {} and quotes
echo "${DISABLE_AUTH}" # using quotes, $ and {}

If you are using environment variable in a string, it should be prefixed with a $sign. You can also wrap the environment variable name in curly brackets {} which is optional.

In the below example, environment variables are declared globally.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
pipeline {
    agent {
        label '!windows'
    }

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                echo "Database engine is ${DB_ENGINE}"
                echo "DISABLE_AUTH is $env.DISABLE_AUTH"
                sh 'printenv'
            }
        }
    }
}

Example of environment variables declared in a stage.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    pipeline {
    agent any
    stages {
        stage('Build') {
            environment {
                DISABLE_AUTH = 'true'
                DB_ENGINE    = 'sqlite'
            }
            steps {
                echo 'Running build...'
            }
        }
        stage('Test') {
            steps {
                echo "Auth is ${DISABLE_AUTH}"
                echo "Db engine is ${DB_ENGINE}"
            }
        }
    }
}

Here, when it runs Test stage it fails as environment variables are declared in Build stage. They are only available within that stage only. When we referenced them in Test stage it fails.

currentBuild Variables

currentBuild variables allow pipeline steps to refer to state of build while it is running. This variable is case sensitive. To access currentBuild variables properties, values are preceded by currentbuild.Nameofproperty. Properties are also case sensitive. Example:

1
2
currentBuild.number
currentBuild.projectName

Just like environment variables, currentBuild variables also preceded by a $ sign when used in a string. In order to view all currentBuild variables, go to pipeline script in Jenkins and click on pipeline syntax option at the end of the page. This will open pipeline syntax page. Click on Global Variables Reference option to the left and scroll down which says currentBuild variable reference.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
pipeline {
    agent {
        label '!windows'
    }

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                echo "Database engine is ${DB_ENGINE}"
                echo "DISABLE_AUTH is $env.DISABLE_AUTH"
                echo "current build duration is $currentBuild.duration"
            }
        }
    }
}
Tip
To view detailed pipeline steps, open your build and click on pipeline steps option to the left.

Parameters

Parameters are also type of variables that get their values when the job is triggered. They are defined in a parameters block, which is placed at the beginning of the pipeline code and are accessed by their name and a params prefix.

It should have a name, a default value and a description. Just like variables, they are written with capital letters to identify easily. If they are used in a string, they should have a $ sign at the beginning and entered with a curly braces.

There are 5 different types of parameters as explained below:

  • string

A parameter of a string type, for example: parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }

  • text

A text parameter, which can contain multiple lines, for example: parameters { text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '') }

  • booleanParam

A boolean parameter, for example: parameters { booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }

  • choice

A choice parameter, for example: parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') }

  • password

A password parameter, for example: parameters { password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'A secret password') }

When using string and text parameters, users are entered with a text box to enter the parameter details when the pipeline is executed. When using boolean parameter, users are prompted with a true or false prompt. When using choice users are prompted with a list of options. With choice, default value is not needed.

Tip
When you use parameters, and build the script, you will not get option to enter the parameter values for the very first time. You should run the pipeline/job first and then from second run, you will get “build with parameters” option.

Sample parameter script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
pipeline {
    agent any
    parameters {
            string(name:'Enter Father name', defaultValue: 'Enter name', description: 'Enter your father name')
            text(name: 'phrase', defaultValue: "enter phrase", description: "Enter description")
            booleanParam(name: 'True or false',defaultValue: True, description: "Enter true or false" )
            choice(name: 'colors', choices: ['green','red','yellow'], description: "choose color")
            password(name: 'password', defaultValue: 'mypassword', description: 'Enter password')
    }
        stages {
            stage('Build') {
                steps {
                    echo "hello"
                }

            }
        }
}

Suggested Article

Now that you know about Jenkins variables and parameters, continue reading our other article Jenkins pipeline here. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.

Conclusion

In this tutorial, we’ve explained the basic concepts about Jenkins variables and parameters. We’ve also demonstrated examples for all the topics that are discussed in the article. We hope you have learned something new in this article.

Now that you know about variables and parameters in Jenkins, would you use variables or use the values directly in the script? Let us know in the comments section below.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.