Search code examples
javajenkinscontinuous-integrationjenkins-pipelinejenkins-cli

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.


Solution

  • 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.