将变量从 shell 脚本传递给 jenkins

问题描述:

我从 Jenkins 触发了一个 shell 脚本,该脚本获取日期并将其导出为环境(Linux)变量 $DATE.我需要在同一个 Jenkins 工作中使用这个 $DATE.我作为参数构建工作.创建一个字符串参数作为 DATE 值作为 DATE=$DATE.但它不起作用.

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.

请推荐!!

您提到您正在 shell 脚本中导出 DATE 环境变量,该脚本大概是通过执行 shell"启动的步骤.

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

问题是,一旦 shell 步骤完成,那个环境就消失了——变量不会被带到后续的构建步骤中.
因此,当您稍后尝试使用 $DATE 值时——无论是在另一个构建步骤中,还是作为另一个作业的参数——该特定环境变量将不再存在.

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.

您可以做的是使用 EnvInject 插件在构建期间导出环境变量.使用此插件设置的变量将可用于所有后续构建步骤.

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.

例如,您可以在一个构建步骤中将 DATE 写入属性字段:

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

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

然后您可以添加为您的作业注入环境变量"构建步骤,并在环境属性文件路径"字段中输入 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.

这样,DATE 变量(以及该属性文件中的任何其他内容)将被导出,并对其余的构建步骤可见.

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.