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()
    echo '====checkoutDetails.credentialsId'
    echo checkoutDetails.credentialsId

    echo '====checkoutDetails.userRemoteConfigs.credentialsId'
    echo checkoutDetails.userRemoteConfig.credentialsId

    echo '====scm.userRemoteConfigs.credentialsId'
    echo scm.userRemoteConfig.credentialsId

    echo '====scm.credentialsId'
    echo scm.credentialsId

    echo '====scm.credentialId'
    echo scm.credentialId

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.

        echo '====checkoutDetails.userRemoteConfigs.credentialsId'
        echo checkoutDetails.userRemoteConfig[0].credentialsId
        echo '====scm.userRemoteConfigs[0].credentialsId'
        echo scm.userRemoteConfig[0].credentialsId

        echo '====checkoutDetails.userRemoteConfigs[0].credentialsId'
        echo checkoutDetails.userRemoteConfig[0].credentialsId.toString()
        echo '====scm.userRemoteConfigs.credentialsId'
        echo scm.userRemoteConfig[0].credentialsId.toString()


[Pipeline] echo (hide)
[Pipeline] echo
[Pipeline] echo

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: '', 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
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

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      scm userRemoteConfigs is +refs/heads/JENKINS-39905:refs/remotes/origin/JENKINS-39905 => (origin)
11:34:10      scm userRemoteConfigs.url is
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  [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





1 Like