Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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'
                What is the "credentialsId" used for? In this example when and how could you use the "mycreds" variable?
– Virtual Penman
                Oct 20, 2022 at 21:49
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'
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.