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

5 Comments

  • 3
    Avatar
    Pragnesh Patel

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

  • 0
    Avatar
    Salvador Salvador

    How can I get the username from a SSH Username with private key type credential? I was trying this:

    node {
    key = 'mySSHkey'
      stage('Test') {
        withCredentials([[$class: 'CertificateMultiBinding', credentialsId: key,
        keystoreVariable: 'keystore', aliasVariable: 'alias',
        passwordVariable: 'pass']]) {
          sh 'echo uname=$keystore alias=$alias pwd=$pass'
        }
      }

    }

     

    Jenkins complains with

     

    org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException: Credentials 'mySSHkey' is of type 'SSH Username with private key' where 'com.cloudbees.plugins.credentials.common.StandardCertificateCredentials' was expected

    Can you guide me about this?

  • 0
    Avatar
    Denys Digtiar

    The error message tells you that Binding implementation that you have chosen, i.e., `CertificateMultiBinding`, is expecting the credential of type `StandardCertificateCredentials`, but you have supplied a credential of type 'SSH Username with private key'.

    Have you used Snippet Generator to get this pipeline code? If yes, then there might be a defect please raise it appropriately. If not, please use Snippet Generator to get the correct code.

  • 0
    Avatar
    Salvador Salvador

    I didn't use the Snippet Generator because there is not such "snippet". So I tried to figure out how could be with using the post's examples but with a sshUserPrivateKey type.

    I've found this pull request on Credentials Binding plugin https://github.com/jenkinsci/credentials-binding-plugin/pull/18 and I think is not public available yet or something I'm not catching.

  • 0
    Avatar
    Denys Digtiar

    Hi Salvador,

    As far as I can see from the Github changelog the feature was released in version 1.13

Please sign in to leave a comment.