简体   繁体   English

如何从Java内部获取jenkins环境变量?

[英]How to get jenkins environment variables from inside java?

I have created a java program to do a ssh connection and do a file transfer using scp. 我创建了一个Java程序来进行ssh连接并使用scp进行文件传输。 I have added username, hostname and password as environment variables in jenkins. 我在jenkins中添加了用户名,主机名和密码作为环境变量。 I need to access the value of these environment variables inside my java program. 我需要在我的java程序中访问这些环境变量的值。

I tried using the below options inside my java file, but it did not work. 我尝试在我的java文件中使用以下选项,但是它不起作用。

System.getProperty("HOSTNAME");
System.getenv("HOSTNAME"); 

I cannot use EnvironmentInjectorPlugin due to some security issues. 由于某些安全问题,我无法使用EnvironmentInjectorPlugin。 Could you please point me in the right direction. 您能指出我正确的方向吗?

I got this to work by passing the parameters from the Build Step > Maven > Goals and Options, 我通过传递“构建步骤”>“ Maven”>“目标和选项”中的参数来使其工作,

Maven: Goals and Options Maven:目标和选择

-DHOSTNAME=${HOSTNAME} -DUSERNAME=${USERNAME} -DPASSWORD=${PASSWORD} -q clean install test

and retrieved the value from java file as 并从java文件中检索值为

String strUserName = System.getProperty("USERNAME"); 

The value inside ${' '} is actually environmental variables declared in jenkins config. $ {''}中的值实际上是在jenkins配置中声明的环境变量。

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

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