git 在 Jenkins 管道中签出标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46241980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:54:15  来源:igfitidea点击:

Checkout a tag in Jenkins pipeline

gitjenkinsjenkins-pipeline

提问by rashidcmb

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

在 Jenkinsfile 中签出标签

回答by rashidcmb

After spending, hours got here

消费后,小时到了

Correct way to use GitSCM in declarative pipeline is

在声明性管道中使用 GitSCM 的正确方法是

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

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

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

回答by Mark Bidewell

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

也许不相关,但变量表达式只在双引号字符串中展开,而不是单引号字符串。

回答by Matthias M

I also have to quote the credential id

我还必须引用凭据 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.

'my-user-id' 是您将在凭据页面上找到的条目的 ID。

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

但这不是您在 gui 的下拉选择框中看到的标题。

回答by Yves Schumann

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.

结果将处于分离头模式,但在大多数情况下,无论如何这都不是问题。

回答by iocanel

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

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

回答by yorammi

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

身份验证错误与标签无关 - 似乎是 2 个不同的问题。

You should add a credentialIdto the userRemoteConfigspart, as such:

您应该将 a 添加credentialId到该userRemoteConfigs部分,例如:

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

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

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