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

Article ID:204897020
1 minute readKnowledge base

Issue

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

Environment

  • CloudBees Jenkins Enterprise

  • Pipeline plugin

Resolution

For an Unix agent

node('<MY_UNIX_AGENT>') { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { sh 'echo uname=$USERNAME pwd=$PASSWORD' } }

Windows agent

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

Retrieve in Groovy var

node('<MY_AGENT>') { 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.