back to listing index

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

[web search]
Original source (cloudbees.zendesk.com)
Tags: jenkins jenkins-workflow credentials jenkins-pipeline
Clipped on: 2016-03-08

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

January 19, 2016 00:42

Issue

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

Environment

  • 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.

Was this article helpful? 0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.