How to set an environment variable programmatically in Jenkins/Hudson?

♀尐吖头ヾ 提交于 2019-11-27 02:39:55

问题


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.


回答1:


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

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

  • How to set environment variables in Jenkins?
  • Jenkins : Report results of intermediate [windows batch] build steps in email body

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.




回答2:


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()]


来源:https://stackoverflow.com/questions/10789341/how-to-set-an-environment-variable-programmatically-in-jenkins-hudson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!