简体   繁体   English

当在同一个提交上推送新标签时,jenkins 不会触发构建

[英]jenkins does not trigger build when a new tag is pushed on the same commit

I want to trigger jenkins build if a new tag is pushed to a remote repository.如果将新标签推送到远程存储库,我想触发 jenkins 构建。 I have seen number of posts, but none seems to be working for me.我看过很多帖子,但似乎没有一个对我有用。 My build is triggered successfully when I push a tag having new commits in it, but if I push a new tag on old commits it does not trigger the build.当我推送一个包含新提交的标签时,我的构建被成功触发,但是如果我在旧提交上推送一个新标签,它不会触发构建。

I have configured it using git plugin in Jenkins and adding Refscpec value as +refs/tags/*:refs/remotes/origin/tags/* and Branch specifier as */tags/*我已经在 J​​enkins 中使用 git 插件对其进行了配置,并将 Refscpec 值添加为+refs/tags/*:refs/remotes/origin/tags/*并将分支说明符添加为*/tags/*

Now if run:现在如果运行:

git push origin master
git tag release-v1
git push origin release-v1

Build is triggered successfully for tag release-v1 But now if i do:为标记 release-v1 成功触发了构建但是现在如果我这样做:

git push origin release-v2

Build is not triggered.不触发构建。

This means jenkins is always looking for commit ids, if there is a new commit id linked with the tag it will build the job.这意味着 jenkins 总是在寻找提交 id,如果有一个新的提交 id 与标签链接,它将构建作业。 But i want the jenkins job to run in case I want to release already committed code for another feature with a new tag name.但是我希望 jenkins 作业能够运行,以防我想使用新的标记名称为另一个功能发布已经提交的代码。

I was facing the same issue and created a workaround for that.我遇到了同样的问题并为此创建了一个解决方法。 I decided to tag with suffix _uat, _prod etc. which helped me to achieve the goal.我决定使用后缀 _uat、_prod 等进行标记,这有助于我实现目标。 You will also require two Jenkins jobs for that.为此,您还需要两个 Jenkins 工作。 One will trigger another if condition matched.如果条件匹配,一个将触发另一个。

Step 1 git tag -a release-v1_uat -m "Commit message"步骤 1 git tag -a release-v1_uat -m "提交消息"
git push origin release-v1_uat git push origin release-v1_uat

Create two Jenkins Jobs创建两个 Jenkins 作业

Job 1工作 1
Define repository定义存储库
In Advance section's "Refspec" field put:在提前部分的“Refspec”字段中输入:
+refs/tags/ _uat:refs/remotes/origin/tags/ _uat +refs/tags/ _uat:refs/remotes/origin/tags/ _uat

In "Branches to build" section:在“要构建的分支”部分:
**/tags/*_uat **/标签/*_uat

BuildTriggers构建触发器
CHECK-> GitHub hook trigger for GITScm polling CHECK-> 用于 GITScm 轮询的 GitHub 钩子触发器

BUILDSTEP构建步骤
ExecuteShell执行外壳
TAG=$(git describe --tags --abbrev=0) TAG=$(git describe --tags --abbrev=0)
echo $TAG回声$TAG
echo $TAG > /tmp/tagname echo $TAG > /tmp/tagname
result= echo $TAG | sed 's/.*\\(....\\)/\\1/'结果= echo $TAG | sed 's/.*\\(....\\)/\\1/' echo $TAG | sed 's/.*\\(....\\)/\\1/'
if [[ $result == _uat ]];如果 [[ $result == _uat ]]; then echo yes;然后回声是; else (exit 1);否则(出口1); fi

Click on Advance right below execute shell单击执行 shell 下方的 Advance
Exit code to set build unstable (It will prevent the job to get triggered from any other Tag)退出代码以设置构建不稳定(这将防止作业从任何其他标签触发)
Put 1 in the Box将 1 放入盒子中

Post-build Actions构建后操作
Check: Delete WorkSpace After Build检查:构建后删除工作区

Job 2 (Your Main Job)工作 2 (你的主要工作)
Go to "Build after other projects are built"转到“在其他项目构建后构建”
Mention you job 1 name in the box and select "Trigger only if build is stable"在框中提及您的工作 1 名称并选择“仅在构建稳定时触发”

Do not forget to get your tag value from the file by doing cat /tmp/tagname which generated in the job 1不要忘记通过执行在作业 1 中生成的 cat /tmp/tagname 从文件中获取标签值

There is always a room for improvement, please share if you have some better workaround/solution.总有改进的余地,如果您有更好的解决方法/解决方案,请分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM