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

I have logged username and password as credentials in jenkins. Now I would like to use them in my Jenkins file.

I am using withCredentials

DSL, however I am not sure how to get the user's password as separate variables so that I can use them in my command.

This is what I am doing:

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

      

How can I provide a username and password? I've tried doing ${MYCREDS.split(":")[0]}

it but it doesn't seem to work.

+4


source to share


2 answers


You can use UsernamePasswordMultiBinding

to get the credentials data in separate values:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:'mycreds',
  usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']
])

      



So the following should work in your case:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId:'mycreds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
  sh 'cf login some.awesome.url -u $USERNAME -p $PASSWORD'
}

      

+5


source


Here is a slightly simpler version of Stephen King's answer



withCredentials([usernamePassword(credentialsId: 'mycreds', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  sh 'cf login some.awesome.url -u $USERNAME -p $PASSWORD'

}

      

+1


source







All Articles