Checkout a tag in Jenkins pipeline

♀尐吖头ヾ 提交于 2019-12-10 18:12:47

问题


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


回答1:


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




回答2:


Maybe not relevant, but variable expressions are only expanded in double quoted strings, not single quoted strings.




回答3:


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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/46241980/checkout-a-tag-in-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!