40

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Akki
  • 2,179
  • 8
  • 21
  • 37
  • You can have a look at this answer if you want to see the variables that are available in your pipeline. https://stackoverflow.com/a/42138466/1238300 – Nicolas Seiller Feb 01 '18 at 13:45

2 Answers2

50

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

badgerr
  • 7,802
  • 2
  • 28
  • 43
  • Thanks, I used env.MyVariable and it works in my usecase now I can define environment variables from pipeline. Thanks for saving some time for me. – Akki Jan 13 '17 at 07:56
43

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.

AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
sorin
  • 161,544
  • 178
  • 535
  • 806