You are currently viewing Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In today’s blog post, we will discuss how to use environemnt in a Jenkins pipeline with examples.

What is environment is Jenkins pipeline?

The environment directive specifies a sequence of key-value pairs which will be defined as environment variables for all steps, or stage-specific steps, depending on where the environment directive is located within the Pipeline. The environment directive also supports fetching of Jenkins pre-defined credentials by their identifier using helper method credentials().

Let’s dig down a bit with some examples.

Jenkins pipeline part 4 – environment

Prerequisites

One system with Jenkins installed.

System defined environment variables in Jenkins:

Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Note: You can get the full list of Jenkins environment variables in <JENKINS_URL>/pipeline-syntax/globals#env path.

Jenkins pipeline part 4 – environment

User defined environment variables in Jenkins:

Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 4 – environment

Note: The scope of the stage level environment variable is within the stage itself and is not accessible outside the stage. Hence in “environment2” the NAME3 is null as it was set in stage “environment1”.

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Apart from standard way to define the environment variable using environment directive, you can also set or override an user defined environment using withEnv() method or directly using env.<VARIABLE_NAME>.

Update the pipeline definition using below replacing the label as per your Jenkins setup.

Trigger the pipeline depending on you pipeline setup.

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Observe, both the environment variable changed in stage “environment2” as we override the pipeline level environment variable “name1” and set the new environment variable “name2”. We have also created a new environment variable “name3” dynamically.

Next, we will explore how to access and set Jenkins credentials in pipeline using environment variable.

How to access and set Jenkins credentials in pipeline?

In your Jenkins dashboard, create a new global credential.

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Remember the “ID” of the credential which will be used later in the pipeline to fetch the credentials values. In this demo, I have set the ID to “mycredentials”.

Next, update the pipeline definition using below replacing the label as per your Jenkins setup.

Trigger the pipeline depending on you pipeline setup.

Jenkins pipeline part 4 – environment

Jenkins pipeline part 4 – environment

Observe, Jenkins is smart enough to not print the sensitive data like username and password in the console output.

For additional reference on other types of credentials, please refer this documentation.

Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.

https://www.jenkins.io/doc/

Leave a Reply