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.

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.