Jenkins Git 插件:如何构建特定标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10195900/
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
Jenkins Git Plugin: How to build specific tag?
提问by monkHyman
I am having issues getting Jenkins to build a specified tag. The tag is part of a parametrized build, but I do not know how to pass this through to the git plugin to just build that tag. This has been taking 3 hours of my day and I have conceded defeat to the masters at stack overflow.
我在让 Jenkins 构建指定标签时遇到问题。该标签是参数化构建的一部分,但我不知道如何将其传递给 git 插件以构建该标签。这花了我一天的 3 个小时,我在堆栈溢出时向大师们承认了失败。
回答by Emmanuel Keller
I was able to do that by using the "branches to build" parameter:
我可以通过使用“要构建的分支”参数来做到这一点:
Branch Specifier (blank for default): tags/[tag-name]
Replace [tag-name] by the name of your tag.
将 [tag-name] 替换为您的标签名称。
回答by gotgenes
None of these answers were sufficient for me, using Jenkins CI v.1.555, Git Client plugin v.1.6.4, and Git plugin 2.0.4.
这些答案对我来说都不够,使用 Jenkins CI v.1.555、Git Client 插件 v.1.6.4 和 Git 插件 2.0.4。
I wanted a job to build for one Git repository for one specific, fixed (i.e., non-parameterized) tag. I had to cobble together a solution from the various answers plus the "build a Git tag" blog postcited by Thilo.
我想要为一个特定的、固定的(即非参数化的)标签构建一个 Git 存储库的工作。我不得不从各种答案以及Thilo 引用的“构建 Git 标签”博客文章中拼凑出一个解决方案。
- Make sure you push your tag to the remote repository with
git push --tags
- In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced".
- In the field for Refspec, add the following text:
+refs/tags/*:refs/remotes/origin/tags/*
- Under "Branches to build", "Branch specifier", put
*/tags/<TAG_TO_BUILD>
(replacing<TAG_TO_BUILD>
with your actual tag name).
- 确保将标签推送到远程存储库
git push --tags
- 在您工作的“Git 存储库”部分,在“源代码管理”标题下,单击“高级”。
- 在 Refspec 字段中,添加以下文本:
+refs/tags/*:refs/remotes/origin/tags/*
- 在“要构建的分支”、“分支说明符”下,输入
*/tags/<TAG_TO_BUILD>
(替换<TAG_TO_BUILD>
为您的实际标记名称)。
Adding the Refspec for me turned out to be critical. Although it seemed the git repositories were fetching all the remote information by default when I left it blank, the Git plugin would nevertheless completely fail to find my tag. Only when I explicitly specified "get the remote tags" in the Refspec field was the Git plugin able to identify and build from my tag.
事实证明,为我添加 Refspec 至关重要。尽管当我将其留空时,git 存储库似乎默认获取所有远程信息,但 Git 插件仍然完全无法找到我的标签。只有当我在 Refspec 字段中明确指定“获取远程标签”时,Git 插件才能识别和构建我的标签。
Update 2014-5-7: Unfortunately, this solution does come with an undesirable side-effect for Jenkins CI (v.1.555) and the Git repository push notification mechanism à la Stash Webhook to Jenkins: any time anybranch on the repository is updated in a push, the tag build jobs will also fire again. This leads to a lot of unnecessary re-builds of the same tag jobs over and over again. I have tried configuring the jobs both with and without the "Force polling using workspace" option, and it seemed to have no effect. The only way I could prevent Jenkins from making the unnecessary builds for the tag jobs is to clear the Refspec field (i.e., delete the +refs/tags/*:refs/remotes/origin/tags/*
).
2014 年 5 月 7日更新:不幸的是,这个解决方案确实给 Jenkins CI (v.1.555) 和 Git 存储库推送通知机制带来了不良的副作用 à la Stash Webhook to Jenkins:任何时候存储库上的任何分支被更新在推送中,标签构建作业也将再次触发。这会导致一次又一次地对相同的标记作业进行大量不必要的重新构建。我曾尝试使用和不使用“使用工作区强制轮询”选项配置作业,但似乎没有任何效果。我可以阻止 Jenkins 为标记作业进行不必要的构建的唯一方法是清除 Refspec 字段(即删除+refs/tags/*:refs/remotes/origin/tags/*
)。
If anyone finds a more elegant solution, please edit this answer with an update. I suspect, for example, that maybe this wouldn't happen if the refspec specifically was +refs/tags/<TAG TO BUILD>:refs/remotes/origin/tags/<TAG TO BUILD>
rather than the asterisk catch-all. For now, however, this solution is working for us, we just remove the extra Refspec after the job succeeds.
如果有人找到更优雅的解决方案,请编辑此答案并进行更新。例如,我怀疑如果 refspec 特别是+refs/tags/<TAG TO BUILD>:refs/remotes/origin/tags/<TAG TO BUILD>
而不是星号包罗万象,这可能不会发生。然而,就目前而言,这个解决方案对我们有用,我们只是在工作成功后删除额外的 Refspec。
回答by Andrew T Finnell
Can't you tell Jenkins to build from a Ref name? If so then it's
你不能告诉 Jenkins 从 Ref 名称构建吗?如果是这样,那就是
refs/tags/tag-name
From all the questions I see about Jenkins and Hudson, I'd suggest switching to TeamCity. I haven't had to edit any configuration files to get TeamCity to work.
从我看到的关于 Jenkins 和 Hudson 的所有问题来看,我建议改用 TeamCity。我无需编辑任何配置文件即可使 TeamCity 正常工作。
回答by Vincent de Lagabbe
If you are using Jenkins pipelines and want to checkout a specific tag (eg: a TAG
parameter of your build), here is what you can do:
如果您正在使用 Jenkins 管道并想要签出特定标签(例如:TAG
构建的参数),您可以执行以下操作:
stage('Checkout') {
steps {
checkout scm: [$class: 'GitSCM', userRemoteConfigs: [[url: 'YOUR_GIT_REPO_URL.git', credentialsId: 'YOUR_GIT_CREDENTIALS_ID' ]], branches: [[name: 'refs/tags/${TAG}']]], poll: false
}
}
回答by Hasan Shaik
I did something like this and it worked :
我做了这样的事情,它奏效了:
Source Code Management
Git
Repositories
Advance
Name: ref
Refspec : +refs/tags/*:refs/remotes/origin/tags/*
Branches to build
Branch Specifier (blank for 'any') : v0.9.5.2
Jenkins log confirmed that it was getting the source from the tag
Jenkins 日志确认它正在从标签中获取源
Checking out Revision 0b4d6e810546663e931cccb45640583b596c24b9
(v0.9.5.2)
检出修订版0b4d6e810546663e931cccb45640583b596c24b9
(v0.9.5.2)
回答by m1ld
In a latest Jenkins (1.639 and above) you can:
在最新的 Jenkins(1.639 及更高版本)中,您可以:
- just specify name of tag in a field 'Branches to build'.
- in a parametrized build you can use parameter as variable in a same field 'Branches to build' i.e. ${Branch_to_build}.
- you can install Git Parameter Pluginwhich will provide to you functionality by listing of all available branches and tags.
- 只需在“要构建的分支”字段中指定标签名称。
- 在参数化构建中,您可以将参数用作同一字段“要构建的分支”中的变量,即 ${Branch_to_build}。
- 您可以安装Git Parameter Plugin,它将通过列出所有可用的分支和标签为您提供功能。
回答by Nebu
I set the Advanced->Refspec field to refs/tags/[your tag name]
. This seems simpler than the various other suggestions for Refspec, but it worked just fine for me.
我将 Advanced->Refspec 字段设置为refs/tags/[your tag name]
. 这似乎比 Refspec 的各种其他建议更简单,但对我来说效果很好。
UPDATE 23/7/2014- Actually, after further testing, it turns out this didn't work as expected. It appears that the HEAD version was still being checked out. Please undo this as the accepted answer. I ended up getting a working solution by following the post from gotgenesin this thread(30th March). The issue mentioned in that post of unnecessary triggering of builds was not an issue for me, as my job is triggered from an upstream job, not from polling SCM.
2014年 7 月 23 日更新- 实际上,经过进一步测试,结果表明这并没有按预期工作。看来 HEAD 版本仍在检查中。请将此作为已接受的答案撤消。我最终通过关注本主题中gotgenes的帖子(3 月 30 日)获得了一个可行的解决方案。那个帖子中提到的不必要触发构建的问题对我来说不是问题,因为我的工作是由上游工作触发的,而不是轮询 SCM。
UPDATE APR-2018- Note in the comments that this does work for one person, and agrees with Jenkins documentation.
更新 APR-2018- 在评论中注意这确实适用于一个人,并同意 Jenkins 文档。
回答by Thilo
I was able to get Jenkins to build a tag by setting the Refspec and Branch Specifier as detailed in this blog post.
我能够通过设置 Refspec 和 Branch Specifier 来让 Jenkins 构建一个标签,如本博文中所述。
I also had to set the Repository Name (to "origin" in my case) so that I could reference it in the Refspec (otherwise it would apparently use a randomly generated name).
我还必须设置存储库名称(在我的情况下为“来源”),以便我可以在 Refspec 中引用它(否则它显然会使用随机生成的名称)。
回答by riverfall
You can build even a tag type, for example 1.2.3-alpha43
, using wildcards:
您甚至可以构建标记类型,例如1.2.3-alpha43
,使用通配符:
Refspec:+refs/tags/*:refs/remotes/origin/tags/*
参考规格:+refs/tags/*:refs/remotes/origin/tags/*
Branch specifier:origin/tags/1.2.3-alpha*
分支说明符:origin/tags/1.2.3-alpha*
You can also tick "Build when a change is pushed to GitHub" to trigger the push, but you have to add "create"action to the webhook
您也可以勾选“在将更改推送到 GitHub 时构建”来触发推送,但您必须向webhook添加“创建”操作
回答by Hugh Perkins
What I did in the end was:
我最后做的是:
- created a new branch
jenkins-target
, and got jenkins to track that - merge from whichever branch or tag I want to build onto the
jenkins-target
- once the build was working, tests passing etc, just simply create a tag from the
jenkins-target
branch
- 创建了一个新分支
jenkins-target
,并让 jenkins 跟踪它 - 从我想要构建的任何分支或标签合并到
jenkins-target
- 一旦构建工作,测试通过等,只需从
jenkins-target
分支创建一个标签
I'm not sure if this will work for everyone, my project was quite small, not too many tags and stuff, but it's dead easy to do, dont have to mess around with refspecs and parameters and stuff :-)
我不确定这是否适用于每个人,我的项目很小,没有太多标签和东西,但它很容易做到,不必乱用 refspecs 和参数和东西:-)