Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get username password stored in Jenkins credentials separately in jenkinsfile

Tags:

I've stored username and password as credentials in jenkins. Now I would like to use them in my Jenkinsfile.

I am using withCredentials DSL, however, I'm not sure how to get the username password as separate variables so I can use them in my command.

This is what I'm doing:

withCredentials([usernameColonPassword(credentialsId: 'mycreds', variable: 'MYCREDS')]) {   sh 'cf login some.awesome.url -u <user> -p password' } 

How can I the username and passwork separately? I tried doing ${MYCREDS.split(":")[0]} but that doesn't seem to work.

like image 706
Anthony Avatar asked Mar 26 '17 08:03

Anthony


People also ask

How do I get Jenkins username and password?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.

How does Jenkinsfile use username and password?

To use, first go to the Credentials link and add items of type Secret file and/or Secret text. Now in a freestyle job, check the box Use secret text(s) or file(s) and add some variable bindings which will use your credentials. The resulting environment variables can be accessed from shell script build steps and so on.

Where are Jenkins user passwords stored?

Jenkins uses AES to encrypt and protect secrets, credentials, and their respective encryption keys. These encryption keys are stored in $JENKINS_HOME/secrets/ along with the master key used to protect said keys.

How do I get hidden text from Jenkins credentials?

Open localhost:8080 , where you should see a Jenkins with a couple of jobs. To browse and add secrets, click on Credentials . My Jenkins instance already has some pre-made credentials created by me. To add secrets hover over (global) to show a ▼ sign and click on it.


1 Answers

Here is a tiny bit simpler version of StephenKing's answer

withCredentials([usernamePassword(credentialsId: 'mycreds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {   sh 'cf login some.awesome.url -u $USERNAME -p $PASSWORD'  } 
like image 96
Greg Bala Avatar answered Oct 10 '22 04:10

Greg Bala