Environment variables and credentials usage in Jenkins pipelines

Devops article 35:​​ Environment variables​​ and credentials​​ usage in pipelines

What you will Learn:

  • Setup environment variables​​ in pipeline

  • Define global environment variable

  • Manage credentials​​ (secret text)​​ in pipeline

  • Manage credentials (username with password) in pipeline

Setup environment variables​​ in​​ pipeline

To understand setting up of​​ environment variables​​ in pipeline, let us create a new pipeline job

Select ‘Hello World’ from the dropdown

Change the stage name from ‘Hello’ to something else

Insert the environment block as shown below

Create an environment variable and assign a value to it as shown below

Now,​​ inside​​ the​​ step, we​​ can​​ access​​ this​​ environment variable using a $ sign.

Important note: The echo statement should be within double quotes as shown below

Apply/Save/Build.

Notice below that the correct value is printed

Define global environment variable

Let us now define the environment​​ variable​​ globally.​​ Configure the same job.

Cut the environment block

Paste it inside pipeline (at line number 3)

Apply/Save/Build.

The result is same. Notice below that the correct value is printed

That​​ is how​​ we use environment​​ variables.

Manage credentials​​ (secret text)​​ in pipeline

Let us see how to manage credentials in jenkins pipeline.​​ At the top right hand side, there is ‘admin’ dropdown menu. When you click it, we can see ‘Credentials’ as shown below

Click ‘Credentials’

Click (global) that is highlighted above.

We can now see ‘Add Credentials’ as seen below

Click ‘Add Credentials’​​ and select ‘Secret text’ from the dropdown

Do not change the scope. Enter any value in ‘Secret’ field​​ (assume that this is your git token). Enter ID and description

Make a note of ID

Click Ok. The credential gets created as shown below

Create new pipeline job, select ‘Hello World’ from dropdown.

Next, insert below environment block in the stage section:​​ 

environment

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ My_Git_token = credentials('<paste your credential id>')

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

Let us write the ID as shown below

To access this credential​​ inside step,​​ let us add:

echo "My​​ GithubToken is $My_Git_token"

Apply/Save/Build.

Notice below that​​ token​​ gets printed as​​ secret text, it is not visible

Manage credentials (username with password) in pipeline

Similarly create ‘Username with password’ credential as shown below. Make a note of ID

Click OK

Configure the same job (or create new one). Change the lines as below. Note that ‘Test’ is ID that we had given above.

Also, to access the username, we have to use _USR suffix at the end. Similarly, to access the password, we have to use _PSW suffix at the end

Save/Build.

Notice below that username and password are hidden.

The complete code is:

pipeline {

 ​​ ​​ ​​​​ agent any

 ​​ ​​ ​​​​ stages {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ stage('Hello') {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ environment

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ My_User_Credentials = credentials('Test')

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ steps {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "My username is $My_User_Credentials_USR"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "My password is $My_User_Credentials_PSW"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

}

So, this is how we use environment variables and credentials in pipelines.

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024