我使用Jenkins凭证创建了MY_USERNAME
类型的条目,选择了User Name and Password
类型:
现在我可以在Groovy脚本中访问此变量:
withCredentials([usernamePassword(
credentialsId: 'MY_USERNAME_ID',
passwordVariable: 'pwd',
usernameVariable: 'user')
]) {
sh 'echo $user'
sh "echo $pwd"
sh "echo ${user}"
echo('$pwd')
echo("$user")
echo("${pwd}")
echo user
}
以上所有命令都可以获取变量值。并且所有这些字符都掩盖了用星号替换实际字符的值,例如***********
。
现在,我需要将真实的用户名和密码值保存到文本文件中。如何将它们保存到文件中?
答案 0 :(得分:2)
凭据仅在控制台输出中被屏蔽。这有效:
withCredentials([usernamePassword(
credentialsId: 'MY_USERNAME_ID',
passwordVariable: 'pwd',
usernameVariable: 'user')
]) {
writeFile file: 'pwdfile', text: "$user:$pwd"
}