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

落爺英雄遲暮 提交于 2019-12-21 09:14:37

问题


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.


回答1:


You can use the UsernamePasswordMultiBinding to get credential 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'
}



回答2:


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'

}


来源:https://stackoverflow.com/questions/43026637/how-to-get-username-password-stored-in-jenkins-credentials-separately-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!