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

前端 未结 2 647
抹茶落季
抹茶落季 2021-02-12 17:28

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

相关标签:
2条回答
  • 2021-02-12 17:59

    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'
    }
    
    0 讨论(0)
  • 2021-02-12 18:21

    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'
    
    }
    
    0 讨论(0)
提交回复
热议问题