In this article, I will introduce ways of defining and using pipeline variables. Read the previous article if you didn’t yet. Variables are the most important part of the pipelines that able us to make repeatable pipelines and separate codes from configurations. That is why you should learn how to define and use variables. So, let’s get started.

Follow our social media:

Before continuing reading:

If you need a complete Jenkins stack with a set of tools and plugins, you can deploy the one I use in our company, training courses, and this article series.

This stack is available for both Kubernetes and Docker.

Jenkins pipeline environment variables:

Pipeline environment variables can be defined in the environment section. This section can be defined globally on the entire pipeline or in the specific pipeline stage. You can simultaneously define your environment variables in both global and per-stage. Globally defined variables can be used in all stages, but stage-defined variables can only be used within that stage. Environment variables can be defined using NAME = VALUE syntax. To access the variable value, you can use these three methods $env.NAME, $NAME or ${NAME} There are no differences between these methods. Here is a complete pipeline sample with environment variables example.

Jenkins Credentials and Environment variables:

A helper method can be used as a value of environment variables to access pre-defined Jenkins credentials. All well-known credentials – Secret Text, Secret File, Username/Password and SSH with Private Key and other credential types installed by other plugins – can be accessed using credentials(id) helper.

Important tips:

When we use one of the “Username/Password” or “SSH with Private Key” credentials, in addition to our defined variable name, two other environment variables are defined automatically by Jenkins. These two automatically defined variables are named VARNAME_USR and VARNAME_PSW These extra variables are defined because these types of credentials are not single-value credentials. The value of these additional automatically defined variables is a subset of the initially defined credential.

In the case of the “Username/Password” credential, suppose you define a variable called MYUSERPASS the value of MYUSERPASS is username:password the value of MYUSERPASS_USR is username and the value of MYUSERPASS_PSW is password respectively. Here is an example.

In the case of the “SSH with Private Key” credential, suppose you define an environment variable MYSSHINFO the value of MYSSHINFO is the location of the SSH key file. If that credential has an SSH username and passphrase, MYSSHINFO_USR and MYSSHINFO_PSW are defined automatically, and those values are assigned to these variables respectively.

Where environment variables can be used and which Jenkins methods/functions can accept environment variables, as well as other methods to define/access variables, will be described in future articles.

You can find all tutorial materials in the following GitHub repository:

If you like this series of articles, please share them and write your thoughts as comments here. Your feedback encourages me to complete this massively planned program.

Follow my LinkedIn

Follow Kubedemy LinkedIn

Follow Kubedemy Telegram

Leave a Reply

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