简体   繁体   中英

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

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

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

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.

我希望它像普通分支一样工作,您是否尝试过不使用“refs/tags/”前缀?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM