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.
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.
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.
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.
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.
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With