How to get checkout credentials ID which is set for pipeline or multibranch projects?

I can get almost all git informations i need, whether from env variables, or by git commands or using checkoutInfo = checkout scm . The only thing i dont know how to get is credentials id used in jobs settings (those in ‘Credentials’ field) which are used for default check out. Whether it is user-password or key. I it possible to somehow get this string? Maybe some additional plugin?

So far im using these (the ones relevant to topic): git, git client github, Gitbub Branch Source, Bitbucket branch source, pipeline,

Its VERY hard to google this topic because results only show solutions for using fixed credentials in GitSCM.

None of these returned proper value

checkoutDetails = checkout scm                        
echo checkoutDetails.toString()
echo '+++++++scm.toString()'
echo scm.toString()
                        
try{
    echo '====checkoutDetails.credentialsId'
    echo checkoutDetails.credentialsId
}catch(e){}

try{
    echo '====checkoutDetails.userRemoteConfigs.credentialsId'
    echo checkoutDetails.userRemoteConfig.credentialsId
}catch(e){}

try{
    echo '====scm.userRemoteConfigs.credentialsId'
    echo scm.userRemoteConfig.credentialsId
}catch(e){}

try{
    echo '====scm.credentialsId'
    echo scm.credentialsId
}catch(e){}

try{
    echo '====scm.credentialId'
    echo scm.credentialId
}catch(e){}

You’re very close with those. For historical reasons, the git plugin userRemoteConfig is a list of configurations rather than a single configuration. The Pipeline Syntax Snippet Generator at .../pipeline-syntax shows you that when you press the “Add” button to add another repository to the list of repositories.

You want scm.userRemoteConfig[0].credentialsId.

I don’t think there are very many useful cases where a remote configuration should be a list, since all of the repositories in the list are assumed to have shared history. However, we don’t want to break compatibility for those users who depend on it being a list, so it remains a list.

Thanks for response but I tried these and still none of them worked. Im using Bitbucket Source Plugin.

    try{
        echo '====checkoutDetails.userRemoteConfigs.credentialsId'
        echo checkoutDetails.userRemoteConfig[0].credentialsId
    }catch(e){}
    
    try{
        echo '====scm.userRemoteConfigs[0].credentialsId'
        echo scm.userRemoteConfig[0].credentialsId
    }catch(e){}

    try{
        echo '====checkoutDetails.userRemoteConfigs[0].credentialsId'
        echo checkoutDetails.userRemoteConfig[0].credentialsId.toString()
    }catch(e){}
    
    try{
        echo '====scm.userRemoteConfigs.credentialsId'
        echo scm.userRemoteConfig[0].credentialsId.toString()
    }catch(e){}

log

====checkoutDetails.credentialsId
[Pipeline] echo (hide)
null
[Pipeline] echo
====checkoutDetails.userRemoteConfigs[0].credentialsId
[Pipeline] echo
====scm.userRemoteConfigs[0].credentialsId

Are you sure that you are using a credential in that checkout? I inserted the following into my Bitbucket branch source test case and it reported the expected values:

  stage('Checkout') {
    checkout([$class: 'GitSCM',
                branches: [[name: '*/JENKINS-39905']],
                extensions: [[$class: 'CloneOption', honorRefspec: true, noTags: true]],
                userRemoteConfigs: [[url: 'https://bitbucket.org/markewaite/jenkins-bugs.git', refspec: '+refs/heads/JENKINS-39905:refs/remotes/origin/JENKINS-39905', credentialsId: 'jenkins-bugs-repo-scan-password-bitbucket']]])
    echo "scm userRemoteConfigs is ${scm.userRemoteConfigs[0]}"
    echo "scm userRemoteConfigs.url is ${scm.userRemoteConfigs[0].url}"
    echo "scm userRemoteConfigs.refspec is ${scm.userRemoteConfigs[0].refspec}"
    echo "scm userRemoteConfigs.credentialsId is ${scm.userRemoteConfigs[0].credentialsId}"

The output was:

07:37:17  scm userRemoteConfigs.url is git@bitbucket.org:markewaite/jenkins-bugs.git
07:37:17  scm userRemoteConfigs.refspec is +refs/heads/JENKINS-39905:refs/remotes/origin/JENKINS-39905
07:37:17  scm userRemoteConfigs.credentialsId is jenkins-bugs-repo-scan-password-bitbucket
1 Like

No. As you see in my first post, Im using this checkout, which AFAIK uses credentials set in jobs settings.

checkout scm  

I dont use GiTSCM because as you see it would not make sense for my case.

I switched my Bitbucket multibranch Pipeline example to checkout scm. I see a value for credentialsId. The job definition with checkout scm reports the credential used for the checkout. The output looks like this:

11:34:10  [Pipeline] echo
11:34:10  
11:34:10      scm userRemoteConfigs is +refs/heads/JENKINS-39905:refs/remotes/origin/JENKINS-39905 => git@bitbucket.org:markewaite/jenkins-bugs.git (origin)
11:34:10      scm userRemoteConfigs.url is git@bitbucket.org:markewaite/jenkins-bugs.git
11:34:10      scm userRemoteConfigs.refspec is +refs/heads/JENKINS-39905:refs/remotes/origin/JENKINS-39905
11:34:10      scm userRemoteConfigs.credentialsId is markewaite-bitbucket-rsa-private-key
11:34:10      
11:34:10  [Pipeline] }

Maybe you’re not using a multibranch Pipeline but some other form of Pipeline job?

Thank you! It works!!! Problem was trivial …
this works

scm.userRemoteConfigs[0].credentialsId

not

scm.userRemoteConfig[0].credentialsId

:slight_smile:

2 Likes