Fetch a userid and password from a Credential object in a Pipeline job.

Issue

  • Fetch a userid and password from a Credential object in a Pipeline job.

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline plugin

Resolution

For an Unix slave

node('<MY_UNIX_SLAVE>') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {

sh 'echo uname=$USERNAME pwd=$PASSWORD'
 }
}

Windows slave

node('<MY_WINDOWS_SLAVE>') {
  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',
                    usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
bat '''
      echo %USERNAME%
    '''
  }
}

Retrive in Groovy var

node('<MY_SLAVE>') {
  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',
                    usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {

println(env.USERNAME)
  }
}

 

To retrieve in Groovy var, it is recommended to be running the latest release of Pipeline plugin and credentials-binding-plugin 1.4 to avoid JENKINS-27389.

Have more questions? Submit a request

1 Comments

  • 3
    Avatar
    Pragnesh Patel

    How can i get User-Scoped Credentials (Personal Credentials) ?

Please sign in to leave a comment.