简体   繁体   English

詹金斯注入环境变量

[英]Jenkins inject environment variable

In a Jenkins job I'm doing a couple of actions that reside in the pre-step build, such as executing a shell script. 在Jenkins的工作中,我正在执行一些驻留在前置步骤构建中的操作,例如执行shell脚本。 With the use of the Jenkins plugin "EnvInject" I want to inject environment variables into my maven build (Unit tests) so that those can be used inside my Java unit tests. 通过使用Jenkins插件“EnvInject”,我想将环境变量注入我的maven构建(单元测试),以便可以在我的Java单元测试中使用它们。 Inside the shell script im doing something similar as: 在shell脚本里面我做了类似的事情:

echo "ip=$IP" >> unit-test.properties

While building Jenkins outputs the following: 构建Jenkins时输出以下内容:

[EnvInject] - Injecting environment variables from a build step.
[EnvInject] - Injecting as environment variables the properties file path 'unit-test.properties'
[EnvInject] - Variables injected successfully.

But the "ip" variable is not available inside my Java code (unit test). 但是我的Java代码(单元测试)中没有“ip”变量。 When I do a full print of both System.getProperties() and System.getenv() I do not see the "ip" enlisted. 当我对System.getProperties() and System.getenv()进行完整打印时,我没有看到“ip”已登记。

Do I need to perform any special actions for maven to pass the variable to my Java code? 我是否需要对maven执行任何特殊操作才能将变量传递给我的Java代码? Is something else wrong? 还有别的错吗?

I'm pretty much stuck from this point onward, I do want to inject a key=value from a pre-step into my Java code. 从这一点开始,我几乎陷入困境,我确实希望从前一步中将一个key=value注入到我的Java代码中。

My solution: 我的解决方案

Create a "Build a free-style software project". 创建“构建自由式软件项目”。

  1. Jenkins > New Item > Build a free-style software project Jenkins> New Item>构建一个自由风格的软件项目
  2. Add 1st step: Execute shell @ Build, and echo key=value pairs to a .properties file 添加第一步:执行shell @ Build,并将echo key=value对执行到.properties文件
  3. Add 2nd step: Inject environment variables, use the .properties file as defined in step 2 添加第2步:注入环境变量,使用步骤2中定义的.properties文件
  4. Add 3rd step: Invoke top-level Maven targets 添加第3步:调用顶级Maven目标

All custom environment variables are accessible with the key as defined in step #2. 可以使用步骤2中定义的key访问所有自定义环境变量。 This was the only way I found to inject environment variables from shell to java. 这是我发现从shell到java注入环境变量的唯一方法。

如上所述。

I had a similar requirement in my project, except my project was Maven. 我的项目中有类似的要求,除了我的项目是Maven。 To use a variable value from jenkins to my java code, I used -DargLine="-DEnv=$Environment" inside "Build->Advanced->JVM Options". 要使用jenkins中的变量值到我的java代码,我在“Build- > Advanced-> JVM Options”中使用了-DargLine =“ - DEnv = $ Environment ”。 From my java code, I fetched the value of "Env" using System.getProperty(). 从我的java代码中,我使用System.getProperty()获取了“Env”的值。 FYI "Environment" is my Jenkins Variable, and "Env" is variable which is storing the value passed from jenkins into its variable(Environment) and fetched in java code using System.getProperty(). FYI“Environment”是我的Jenkins变量,“Env”是变量,它将从jenkins传递的值存储到其变量(Environment)中,并使用System.getProperty()在java代码中获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM