本文介绍了如何获取在jenkinsfile中单独存储在Jenkins凭据中的用户名密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将用户名和密码作为凭证存储在jenkins中.现在,我想在我的Jenkinsfile中使用它们.

I've stored username and password as credentials in jenkins. Now I would like to use them in my Jenkinsfile.

我正在使用withCredentials DSL,但是,我不确定如何获取用户名密码作为单独的变量,因此可以在命令中使用它们.

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.

这就是我在做什么:

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

如何分别输入用户名和密码?我尝试做${MYCREDS.split(":")[0]},但这似乎不起作用.

How can I the username and passwork separately? I tried doing ${MYCREDS.split(":")[0]} but that doesn't seem to work.

推荐答案

您可以使用UsernamePasswordMultiBinding获取单独值中的凭据数据:

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'
}

这篇关于如何获取在jenkinsfile中单独存储在Jenkins凭据中的用户名密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 13:45