Checkout a tag in Jenkins pipeline

Viewed 21246

Tried using

checkout scm: [$class: 'GitSCM', 
  userRemoteConfigs: [[url: '${repoURL}']], 
  branches: [[name: 'refs/tags/${tag-version}']]],poll: false

This fails with an Authentication error. Is there any way other than using

withCredentials

to checkout tag in a Jenkinsfile

5 Answers

I also have to quote the credential id

stage('checkout') {
    steps {
        checkout([$class: 'GitSCM', branches: [[name: tagVersion]],
                  userRemoteConfigs: [[url: 'ssh://git@repo',
                                       credentialsId: 'my-user-id']]
                ])
        }
    }

Annoation

'my-user-id' is the id of the entry you will find on the credentials page.

But it's not the title you see in the dropdown select box in the gui.

If one don't want to fiddle around with the cryptic syntax, I've been using this solution to switch to a dedicated tag or branch, especially if it's a job parameter and not clear if the given value is a branch or a tag:

git(
    credentialsId: '<your-cred-id>',
    url: "<your-repo-url>"
)
sh(script:"""
    git checkout \$(git rev-parse --verify ${GIVEN_BRANCH_OR_TAG})
""")

The result will be in detached head mode but for most cases that's not a problem anyway.

Related