Jenkins 是否可以自动检测并构建 git 存储库中新创建的标签?
如果我们的 Jenkins CI 服务器能够在 Github 存储库中创建标签时自动检测、部署和构建标签,那就太好了。
这可能吗?
It would be nice for our Jenkins CI server to automatically detect, deploy and build tags as they are created in our Github repository.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过以下配置,您可以使作业构建所有标签:
+refs/tags/*:refs/ remotes/origin/tags/*
*/tags/*
这种方法有一个缺点:该作业将构建所有标签,而不仅仅是新添加的标签。因此,在创建作业后,将为每个现有标签触发一次。因此,您可能想让作业一开始不执行任何操作,然后等到所有现有标签都已处理完毕,然后才配置您想要为每个新标签完成的构建步骤。
由于标签在 git 中不会更改,因此每个新标签只会触发一次该作业。
With the following configuration, you can make a job build all tags:
+refs/tags/*:refs/remotes/origin/tags/*
*/tags/*
This approach has one drawback: The job will build all tags and not just newly added tags. So after you have created the job, it will be triggered once for every existing tag. So you probably want to have the job do nothing at first, then wait until all existing tags have been processed, and only then configure the build steps you want to be done for every new tag.
Since tags don't change in git, the job will then only be triggered once for every new tag.
为了克服@oberlies'答案的缺点,即所有标签都将被构建,我使用特殊的触发器构建来代替。触发器构建使用与主构建和以下(后)构建步骤相同的 git 存储库和分支。
构建->执行shell:
Build -> 注入环境变量:
构建后操作 -> : 在其他项目上触发参数化构建
最后,在您的主构建中,使用以下字符串参数勾选“此构建已参数化”
并在“源代码管理”部分中的“要构建的分支”字段中使用“$TAG”。
To overcome the drawback of @oberlies' answer that all tags will be built I'm using a special trigger build instead. The trigger build uses the same git repository and branch as the main build and the following (post) build steps.
Build -> Execute shell:
Build -> Inject environment variables:
Post-build Actions -> : Trigger parameterized build on other projects
Finally, in your main build, tick "This build is parameterized" with the following string parameter
And in the "Source Code management" section use "$TAG" in the "Branches to build" field.
您可以安装一个 post-receive 挂钩,它检查标签是否已提交,并在 jenkins 中创建构建。
钩子看起来像这样 [*]:
[*] 注意:这只是一个稍微不同的脚本的快速转换,所以这里很可能存在一些小错误。这主要是为了展示这个想法。
在詹金斯方面,您需要配置参数化作业。唯一的参数是“分支”。
这提供了一种相当安全和健壮的构建方式。要进行测试,请通过 Web 界面运行构建,它会询问参数的值。
You can install a post-receive hook, which checks if a tag was commited, and creates a build in jenkins.
The hook can look something like this [*]:
[*] note: this is just a quick conversion from a slightly different script, so it's quite probable that there are some small bugs here. This is mostly to show the idea.
On the jenkins side, you need to configure a parametrized job. The only parameter is 'branch'.
This gives a fairly secure and robust way to build. To test, run a build through the web interface, it'll ask for the value of the parameter.
在现代(?)多分支管道的世界中,构建标签的工作原理如下。
不要忘记为分支添加构建策略;该插件完全禁用默认设置!
In the world of modern (?) multi-branch pipelines, building tags works as follows.
Don't forget to add a build strategy for branches as well; the plugin disables the defaults completely!
您可以使用选项“Git Publisher”,它是 Git 插件 在成功构建/部署后创建标签。
You can use the option "Git Publisher" which comes as part of the Git Plugin to create a tag after a successful build/deploy.