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:
|
|
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.
|
|
Example of environment variables declared in a stage.
|
|
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:
|
|
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.
|
|
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.
Sample parameter script:
|
|
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.