Kishore Mohanavelu
Kishore Mohanavelu

Reputation: 457

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. I have added username, hostname and password as environment variables in jenkins. I need to access the value of these environment variables inside my java program.

I tried using the below options inside my java file, but it did not work.

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

I cannot use EnvironmentInjectorPlugin due to some security issues. Could you please point me in the right direction.

Upvotes: 0

Views: 3853

Answers (1)

Kishore Mohanavelu
Kishore Mohanavelu

Reputation: 457

I got this to work by passing the parameters from the Build Step > Maven > Goals and Options,

Maven: Goals and Options

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

and retrieved the value from java file as

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

The value inside ${' '} is actually environmental variables declared in jenkins config.

Upvotes: 1

Related Questions