将变量从shell脚本传递给jenkins [英] Passing variable from shell script to jenkins

查看:427
本文介绍了将变量从shell脚本传递给jenkins的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Jenkins触发了一个shell脚本,该脚本获取日期并将其导出为环境变量(Linux)$ DATE.我需要在同一詹金斯工作中使用此$ 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.

问题是,一旦完成外壳程序步骤,该环境就消失了-变量将不会被带到后续的构建步骤中.
因此,当您以后尝试使用$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.

这篇关于将变量从shell脚本传递给jenkins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆