15

I have two scripts in the pre-build step in a Jenkins job, the first one a perl script, the second a system groovy script using the groovy plugin. I need information from the first perl script in my second groovy script. I think the best way would be to set some environment variable, and was wondering how that can be realized.

Or any other better way.

Thanks for your time.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Gogi
  • 1,695
  • 4
  • 23
  • 36
  • This question involves project-specific environment variables, but for global environment variables, please see [this answer](http://stackoverflow.com/a/39004253/2074605). – Parker Aug 17 '16 at 18:56

2 Answers2

14

The way to propagate environment variables among build steps is via EnvInject Plugin.

Here are some previous answers that show how to do it:

In your case, however, it may be simpler just to write to a file in one build step and read that file in another. To make sure you do not accidentally read from a previous version of the file you can incorporate BUILD_ID in the file name.

Community
  • 1
  • 1
malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87
  • Workspace is always separate for each build so there is no need to add a `build_id`. If you will add build_id you will end up with a lot of files eventually. It's better to simply start by removing old file if it exists (or simply overwrite it). – Nux Jul 24 '19 at 13:38
  • It really depends on your use case. If you want to preserve the content of the file for a later usage you need to add the build-id in the file name. If you will only use the latest build info, then you can always replace it. – Luis Elizondo Aug 28 '20 at 21:46
3

Using EnvInject Plugin from job configuration you should use Inject environment variables to the build process / Evaluated Groovy script.

Depending on the setup you may execute Groovy or shell command and save it in map containing environment variables:

Example

By either getting command result with execute method:

return [DATE: 'date'.execute().text]

or with Groovy equivalent if one exists:

return [DATE: new Date()]
luka5z
  • 7,525
  • 6
  • 29
  • 52