4

I want to pass username and password in Jenkins by passing through property file and Use this credential in Java program to login to my app.

  1. Inject property file in Jenkins
  2. Use credentials from that file in my Java program to login to my app.

OR, Other way around it, I can pass it as Password passing parameter for my jenkins job but I am not getting a way to fetch this password in my java program.

Any help would be appreciated. Thanks in advance.

Neel Desai
  • 71
  • 2
  • 3
  • 10
  • 1
    The easiest way seems to be to pass it as a system property via the command line (-D option when you start the JVM) – Henry Feb 02 '18 at 05:07

1 Answers1

3

Step 1) prepare a java property file with username and password and put togerther with java code

username = xxx
password = xxx

Step 2) Config jenkins job to inject environment variable from property file

Option 1: check Inject environment variables to the build process under Build Environment section
enter image description here

Option 2: Add a Inject environment variable build step and move it to the top on others build steps
enter image description here

For both options, specify the credential property file path relative to jenkins job workspace

Step 3) specify system property: username and password in java cmd line

for example: java -DUserName="${username}" -DPassWord="${password}"

Note:
1. ${xxx}, the xxx from the key in property file, case sensitive.
2. Please use double quote in case username or password includes space
3. The pattern -Dxxx="${yyy}" can work on both Execute linux shell and Execute windows batch build step.

enter image description here More about Java command

Step 4) obtain ssytem property: username and password in Java code

String userName = System.getProperty("UserName")
// the parameter: "UserName" from -Dxxxx, also case sensitive

enter image description here

More about system property

yong
  • 13,357
  • 1
  • 16
  • 27
  • can you explain step#3 and 4 in more detail? – Neel Desai Feb 02 '18 at 17:12
  • but in this way, I will have to pass default password in java code which could be access or view by others. which is not good in terms of security reason. – Neel Desai Feb 03 '18 at 06:25
  • System.getProperty() has another version only accept one argument: system proterty name, no default value required. – yong Feb 03 '18 at 14:35
  • But this property file will be inside my Java project. Am I right? So In future if my credentials will get changed, I will have to access my java project & will need to change property file over there. Am I right? – Neel Desai Feb 04 '18 at 00:47
  • If the username and password appear in job's console output is acceptable? If yes, i can update answer to generate auth file during job running(not exist source code). If no, you need to add credential file by Jenkins admin, then choose credential in job configuration. – yong Feb 05 '18 at 01:40
  • the username and password appear in command line is acceptable for security requirement? – yong Feb 05 '18 at 13:53
  • I think for security reason, what I am planning is to request admin to add credentials in jenkins and during job execution get that parameter in java class for login. I will have to see how to access the username and password passed as binding credentials in java – Neel Desai Feb 05 '18 at 16:53