53

I trigger a shell script from Jenkins, This scripts get date and export it as a environment(Linux) variable $DATE. I need to use this $DATE inside same Jenkins job. I made job as parameter build. Created a string parameter as DATE value as DATE=$DATE. But it is not working.

Please suggest !!

user3232823
  • 1,867
  • 3
  • 18
  • 27
  • It's unclear what you're saying — are you setting `DATE` as a parameter, or are you exporting `DATE` from an "Execute shell" build step? – Christopher Orr May 07 '15 at 22:56
  • Hi There, I am trying to export it from script which is running inside Jenkins job to same Jenkins job. – user3232823 May 08 '15 at 12:51
  • EnvInject plugin is having some serious security vulnerability. Now Jenkins is having inbuilt support for this https://stackoverflow.com/a/53430757/1753177 – lambodar Nov 22 '18 at 16:50

3 Answers3

92

You mention that you are exporting a DATE environment variable in an shell script, which is presumably being started via an "Execute shell" step.

The problem is, once the shell step has completed, that environment is gone — the variables will not be carried over to subsequent build steps.
So when you later try to use the $DATE value — whether in another build step, or as a parameter to another job — that particular environment variable will no longer exist.

What you can do instead is use the EnvInject plugin to export environment variables during a build. Variables set up using this plugin will be available to all subsequent build steps.

For example, you could write the DATE to a properties field in one build step:

echo DATE=$(date +%Y-%m-%d) > env.properties

Then you can add an "Inject environment variables for your job" build step, and enter env.properties in the "Environment Properties File Path" field.

That way, the DATE variable (and anything else in that properties file) will be exported and will be visible to the rest of the build steps.

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 2
    Thanks. It is unfortunate that Jenkins doesn't support this functionality by default. What I was trying to find is a better way to automatically output all variables set within a shell script to the properties file in a single command. One of my job needs to create about 15 variables based on directories, filenames, timestamps, etc. I would then need 15 echo commands to output to the properties file. And if I were to add additional variables I would also need to ensure they were being outputted. It would be nice to be able to run a command similar to "env" which outputs all variables defined. – leeman24 Nov 04 '16 at 14:01
  • 1
    It is indeed unfortunate that Jenkins does not support it out of the box. Thanks for this answer. – Akshay Maldhure Dec 01 '17 at 10:27
  • It might be better to write `env.properties` to `$WORKSPACE/env.properties` and access `$WORKSPACE/env.properties` while injecting variables. – prayagupa Apr 05 '19 at 21:40
17

You could use an assignment statement and sh's returnStdout to get the value in Jenkins without having to write to a properties file.

foo = sh(
  returnStdout: true, 
  script: 'date'
)

Then later on in the Jenkinsfile you can use $foo like any other variable.

EDIT: This is for a pipeline job, not a freestyle job.

Damon Stamper
  • 378
  • 1
  • 4
  • 16
  • Thanks, tested today and in the current version it works! One relevant thing, needs to call the variable `foo`, in the script with` env.foo`: D – David Apr 28 '21 at 12:31
  • 1
    If you are using the variable in another shell command then I believe you are correct and need to use the env syntax. However if you are using the output as raw text in Jenkins' Groovy then you can just use `foo`. Also this applies to any of the shell/PowerShell executors. – Damon Stamper Aug 11 '21 at 18:43
1

I had the same issue. The solution that worked for me was:

env.ABC=bat(returnStdout: true, 
script: '''  @echo off echo abc ''').trim()

The .trim() and @echo off is important if you want to reuse the variable in another batch script.

Fabian W.
  • 38
  • 5