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.