如何在詹金斯中存储密码

时间:2020-04-09 16:14:56

标签: bash jenkins groovy ssh

我使用Jenkins凭证创建了MY_USERNAME类型的条目,选择了User Name and Password类型:

enter image description here

现在我可以在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
  }

以上所有命令都可以获取变量值。并且所有这些字符都掩盖了用星号替换实际字符的值,例如***********

现在,我需要将真实的用户名和密码值保存到文本文件中。如何将它们保存到文件中?

1 个答案:

答案 0 :(得分:2)

凭据仅在控制台输出中被屏蔽。这有效:

withCredentials([usernamePassword(
    credentialsId: 'MY_USERNAME_ID', 
    passwordVariable: 'pwd', 
    usernameVariable: 'user') 
]) {
    writeFile file: 'pwdfile', text: "$user:$pwd"
}