Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable from shell script to jenkins

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 !!

like image 352
user3232823 Avatar asked May 07 '15 20:05

user3232823


People also ask

How do you declare 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.


1 Answers

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.

like image 125
Christopher Orr Avatar answered Sep 30 '22 10:09

Christopher Orr