Reputation: 71
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.
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.
Upvotes: 4
Views: 18459
Reputation: 13712
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
Option 2: Add a Inject environment variable
build step and move it to the top on others build steps
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.
Step 4) obtain ssytem property: username and password in Java code
String userName = System.getProperty("UserName")
// the parameter: "UserName" from -Dxxxx, also case sensitive
Upvotes: 3