Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable in Jenkins Pipeline

Is there any environment variable available for getting the Jenkins Pipeline Title?

I know we can use $JOB_NAME to get title for a freestyle job, but is there anything that can be used for getting Pipeline name?

like image 411
Akki Avatar asked Jan 12 '17 03:01

Akki


People also ask

What is environment variable in Jenkins pipeline?

What are Environment Variables in Jenkins? Environment variables are global key-value pairs Jenkins can access and inject into a project. Use Jenkins environment variables to avoid having to code the same values for each project. Other benefits of using Jenkins environment variables include improved security.

How do I use a variable in Jenkins pipeline?

Jenkins pipeline environment variables: You can define your environment variables in both — global and per-stage — simultaneously. 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.


2 Answers

You can access the same environment variables from groovy using the same names (e.g. JOB_NAME or env.JOB_NAME).

From the documentation:

Environment variables are accessible from Groovy code as env.VARNAME or simply as VARNAME. You can write to such properties as well (only using the env. prefix):

env.MYTOOL_VERSION = '1.33' node {   sh '/usr/local/mytool-$MYTOOL_VERSION/bin/start' } 

These definitions will also be available via the REST API during the build or after its completion, and from upstream Pipeline builds using the build step.

For the rest of the documentation, click the "Pipeline Syntax" link from any Pipeline job enter image description here

like image 87
badgerr Avatar answered Sep 17 '22 16:09

badgerr


To avoid problems of side effects after changing env, especially using multiple nodes, it is better to set a temporary context.

One safe way to alter the environment is:

 withEnv(['MYTOOL_HOME=/usr/local/mytool']) {     sh '$MYTOOL_HOME/bin/start'  } 

This approach does not poison the env after the command execution.

like image 25
sorin Avatar answered Sep 16 '22 16:09

sorin