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().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## ----------- ## environment ## ----------- pipeline { agent { ... } environment { } stages { stage(' environment { key = credentials(' } steps { } ... } ... } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## ---------------------------- ## environment | system defined ## ---------------------------- pipeline { agent { label 'test' } stages { stage('environment') { steps { echo "All available environment variables" sh 'printenv' } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
Note: You can get the full list of Jenkins environment variables in <JENKINS_URL>/pipeline-syntax/globals#env path.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
## ------------------------------------- ## environment | user defined | example1 ## ------------------------------------- pipeline { agent { label 'test' } environment { name1 = 'cloudaffaire' name2 = 'chandrima' } stages { stage('environment1') { environment { name3 = 'debjeet' } steps { echo "name1 is ${env.NAME1}" echo "name2 is ${env.NAME2}" echo "name3 is ${env.NAME3}" } } stage('environment2') { steps { echo "name1 is ${env.NAME1}" echo "name2 is ${env.NAME2}" echo "name3 is ${env.NAME3}" } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
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”.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
## ------------------------------------- ## environment | user defined | example2 ## ------------------------------------- pipeline { agent { label 'test' } environment { name1 = 'cloudaffaire' } stages { stage('environment1') { environment { name2 = 'debjeet' //static assignment name3 = """${sh( //dynamic assignment returnStdout: true, script: 'echo "Jara"' )}""" } steps { echo "name1 is ${env.NAME1}" echo "name2 is ${env.NAME2}" } } stage('environment2') { environment { name3 = """${sh( returnStdout: true, script: 'echo "Jara"' )}""" } steps { withEnv(["NAME1=avik"]) { // using withEnv() echo "name1 is ${env.NAME1}" } script { env.NAME2 = 'chandrima' //using env. } echo "name2 is ${env.NAME2}" echo "name3 is ${env.NAME3}" } } } } |
Trigger the pipeline depending on you pipeline setup.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## -------------------------------------------- ## environment | user defined using credentials ## -------------------------------------------- pipeline { agent { label 'test' } environment { CRED = credentials('mycredentials') } stages { stage('environment') { steps { sh 'echo "Username is $CRED_USR"' sh 'echo "Password is $CRED_PSW"' } } } } |
Trigger the pipeline depending on you pipeline setup.
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.