alphanumeric
alphanumeric

Reputation: 19329

How to store passwords in Jenkins

Using Jenkins Credentials I created MY_USERNAME entry choosing the User Name and Password type:

enter image description here

Now I can access this variable in Groovy script:

  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
  }

All the above commands are able to get the variable value. And all of them are masking the values replacing the real characters with the asterisk, such as ***********.

Now I need to save the real username and password values to a text file. How to save them to a file?

Upvotes: 1

Views: 453

Answers (1)

zett42
zett42

Reputation: 27756

Credentials are only masked in the console output. This works:

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

Upvotes: 3

Related Questions