rashidcmb
rashidcmb

Reputation: 743

Checkout a tag in Jenkins pipeline

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

Upvotes: 6

Views: 27075

Answers (5)

Yves Schumann
Yves Schumann

Reputation: 44

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.

Upvotes: 0

Matthias M
Matthias M

Reputation: 14800

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.

Upvotes: 2

rashidcmb
rashidcmb

Reputation: 743

After spending, hours got here

Correct way to use GitSCM in declarative pipeline is

checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoURL, credentialsId: credential]], branches: [[name: tag-version]]],poll: false

Not like I found in most places in web

checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoURL], [credentialsId: credential]], branches: [[name: tag-version]]],poll: false

Upvotes: 15

yorammi
yorammi

Reputation: 6458

The authentication error has nothing to do with the tag - seems like 2 different issues.

You should add a credentialId to the userRemoteConfigs part, as such:

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

Also, you can use the following format for variables:

checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: repoURL], [credentialsId: credential]], branches: [[name: tag-version]]],poll: false

Upvotes: 0

iocanel
iocanel

Reputation: 580

I would expect it to work like a normal branch, Have you tried without the 'refs/tags/' prefix?

Upvotes: 2

Related Questions