简体   繁体   English

Java找不到Jenkins中定义的环境变量

[英]Java not finding environment variables defined in Jenkins

I have a snippet of code that is obtaining an environment variable as follows: System.getenv("MY_VAL") 我有System.getenv("MY_VAL")代码正在获取环境变量,如下所示: System.getenv("MY_VAL")

locally on my windows machine this works fine. 在我的Windows计算机上本地工作正常。

However, on my Jenkins CI Server which is running CentOS I am encountering some issues 但是,在运行CentOS的Jenkins CI Server上,我遇到了一些问题

I have tried setting the value of MY_VAL through both the envinject plugin as well as the global jenkins settings 我已经尝试通过envinject插件以及全局詹金斯设置来设置MY_VAL的值

If i do a pre-build step to echo the value out, it works fine, however inside my java code this is not being resolved. 如果我执行一个预构建步骤来回显值,则可以正常工作,但是在我的Java代码中,此问题尚未解决。

How do I get this to be resolved? 我该如何解决?

You need to inject this variable into property.file and then access this from property file. 您需要将此变量注入property.file,然后从属性文件访问它。 eg in execute shell you can define "echo MY_VAL=default > property.file" .. Later in subsequent jobs you can pass through using "Jenkins Parameterized Trigger plugin" where you have option to access parameters from property file. 例如,在execute shell中,您可以定义“ echo MY_VAL = default> property.file”。稍后,在随后的作业中,您可以使用“ Jenkins Parameterized Trigger插件”进行传递,您可以选择从属性文件访问参数。

You can achieve that by installing EnvInject plugin. 您可以通过安装EnvInject插件来实现。

1) After installing check the Prepare an environment for the job option in the job configuration screen. 1)安装后,在作业配置屏幕中选中为作业准备环境选项。 This option will display several field for you to fill. 此选项将显示几个字段供您填写。

UPDATE UPDATE

2) Fill the Script Content area with a command touch env.properties to create the file. 2)用命令touch env.properties填充“ 脚本内容”区域以创建文件。

3) Fill the Properties Content t field with the variables you want to inject inside your recently created env.properties file by doing so. 3)你想这样做你最近创建env.properties文件中注入的变量填充属性内容 T字段。 Place one variable per line ex: 每行放置一个变量,例如:

     VARIABLE1=value
     VARIABLE2=value

4) Reference env.properties file you've just created in the file path area. 4)参考您刚刚在文件路径区域中创建的env.properties文件。

5) At runtime Jenkins will inject those variables and they will be available to your program. 5)在运行时,Jenkins将注入这些变量,它们将可用于您的程序。

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

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