git 如果新标签被释放,jenkins 会触发构建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29742847/
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-09 03:05:55  来源:igfitidea点击:

jenkins trigger build if new tag is released

gitjenkinstags

提问by Kingalione

I want to configure jenkins so that it starts building if a new tag is released in any branch of an git repository. How do I configure this behaviour?

我想配置 jenkins,以便它在 git 存储库的任何分支中发布新标签时开始构建。如何配置此行为?

I'm very new in jenkins so excuse me if this might be a very basic question but I couldn't find it out on myself.

我是詹金斯的新手,所以请原谅我,如果这可能是一个非常基本的问题,但我自己找不到。

git jenkins config

git詹金斯配置

Triggering: build trigger

触发: 构建触发器

Thanks for any help

谢谢你的帮助

采纳答案by Stan E

What do you mean by new tag? Does it has some template name?

你说的新标签是什么意思?它有一些模板名称吗?

You can surely define it in Advanced--> Refspec-->refs/tags/{tagname}.

您当然可以在Advanced--> Refspec--> 中定义它refs/tags/{tagname}

You can even do refs/tags/*for finding really ANY new tags.

你甚至可以refs/tags/*找到真正的任何新标签。

enter image description here

enter image description here

回答by albertski

Set refspec to:+refs/tags/*:refs/remotes/origin/tags/*

将 refspec 设置为:+refs/tags/*:refs/remotes/origin/tags/*

branch specifier:**

分支说明符:**

Under build triggers check Build when a change is pushed to GitHub

在构建触发器下检查构建时将更改推送到 GitHub

回答by Samuel Henrique

Please note that the approach in the answer provided by stanjerdoesn't make Jenkins trigger builds on new tags if they point to commits that were built before. For example, you tag release v1.0.0 (to make jenkins deploy this release), then on the future you have to rollback to v1.0.0, tagging its commit again, but with v1.0.0-rollback, Jenkins won't deploy your rollback because it will check the hash the tag points to, not the hash of the tag itself.

请注意,如果 stanjer 提供的答案中的方法指向之前构建的提交,则不会使 Jenkins 在新标签上触发构建。例如,您标记版本 v1.0.0(以使 jenkins 部署此版本),然后在将来您必须回滚到 v1.0.0,再次标记其提交,但是使用 v1.0.0-rollback,Jenkins 不会部署您的回滚,因为它将检查标签指向的哈希值,而不是标签本身的哈希值。

In summary, jenkins will only build new tags if they point to commits that are not tagged already, and this is currently not tweakable.

总而言之,jenkins 只会在指向尚未标记的提交时构建新标记,并且目前无法调整。

It would be awesome if one could use Jenkins as a CD tool working with tags for deploys and rollbacks.

如果可以将 Jenkins 用作 CD 工具,使用标签进行部署和回滚,那就太棒了。

More info here https://groups.google.com/forum/#!msg/jenkinsci-users/mYxtDNMz1ZI/xbX9-xM9BQAJ

更多信息在这里https://groups.google.com/forum/#!msg/jenkinsci-users/mYxtDNMz1ZI/xbX9-xM9BQAJ

回答by Sergey

Previous doesn't work for me. In my case works refspec in single quotes:

以前对我不起作用。在我的情况下,单引号中的 refspec 工作:

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' 分支说明符:**/tags/**

UPDATE: I have Jenkins 2.120. To make job work which is triggered by tag need to do the following steps:

更新:我有詹金斯 2.120。要使由标签触发的作业工作需要执行以下步骤:

1) create job with:

1)创建工作:

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' 分支说明符:**/tags/**

2) Run build

2)运行构建

3) Reconfigure the same job to parameters:

3)将相同的作业重新配置为参数:

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' 分支说明符:**

4) Run build

4)运行构建

5) Reconfigure the same job to parameters:

5)将相同的作业重新配置为参数:

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' Branch Specifier: **/tags/**

Refspec: '+refs/tags/*':'refs/remotes/origin/tags/*' 分支说明符:**/tags/**

6) Run the build

6) 运行构建

Only after this magic steps, when I tag the branch it automatically trigger Jenkins

只有在这个神奇的步骤之后,当我标记分支时,它才会自动触发 Jenkins

回答by efrenster

@albertski answer works but do not forget below additional settings: 1. Setup hook from Bitbucket to Jenkins 2. Polling SCM need to be checked

@albertski 回答有效,但不要忘记以下附加设置: 1. 从 Bitbucket 到 Jenkins 的设置挂钩 2. 需要检查轮询 SCM

You can test the trigger by adding new git tag from a commit in your bitbucket repo.

您可以通过从您的 bitbucket 存储库中的提交添加新的 git 标签来测试触发器。

回答by SaundersB

They released a new "buildingTag" that can be used in a when block.

他们发布了一个新的“buildingTag”,可以在 when 块中使用。

buildingTag - A simple condition that just checks if the Pipeline is running against a tag in SCM, rather than a branch or a specific commit reference.

https://jenkins.io/blog/2018/04/09/whats-in-declarative/

https://jenkins.io/blog/2018/04/09/whats-in-declarative/

回答by George

Combined @albertski and @Sergey answers works for me.

结合@albertski 和@Sergey 的答案对我有用。

Path: Jenkins> {YourJob}> Configure> Pipeline> Definition(Pipeline script from SCM) > SCM(Git)

路径:Jenkins> {YourJob}> Configure> Pipeline> Definition(Pipeline script from SCM) > SCM(Git)

Options:

选项:

Repositories> Advanced...> Refspec+refs/tags/v*:refs/remotes/origin/tags/v*

存储库>高级...> Refspec+refs/tags/v*:refs/remotes/origin/tags/v*

Branches to build> Branch Specifier (blank for 'any')**/tags/v*

要构建的分支>分支说明符(“任何”为空)**/tags/v*

Set v* if you want build tags started with v, such as v0.1.0, v1.0.5...

如果希望构建标签以 v 开头,请设置 v*,例如 v0.1.0、v1.0.5...