Jenkins - 如果启动新的构建,则中止运行构建 [英] Jenkins - abort running build if new one is started
问题描述
我使用 Jenkins 和 Multibranch Pipeline.我为每个活跃的 git 分支都有一份工作.新构建是通过推送 git 存储库触发的.我想要的是如果新分支出现在同一分支中,则中止在当前分支中运行的构建.
I use Jenkins and Multibranch Pipeline. I have a job for each active git branch. New build is triggered by push in git repository. What I want is to abort running builds in current branch if new one appears in same branch.
例如:我提交并推送到分支feature1
.然后 BUILD_1
在 Jenkins 中启动.当 BUILD_1
仍在运行时,我再次提交并推送到分支 feature1
.我希望 BUILD_1
被中止并启动 BUILD_2
.
For example: I commit and push to branch feature1
. Then BUILD_1
started in Jenkins. I make another commit and push to branch feature1
while BUILD_1
is still running. I want BUILD_1
to be aborted and to start BUILD_2
.
我尝试使用 stage concurrency=x
选项和 stage-lock-milestone 功能,但未能解决我的问题.
I tried to use stage concurrency=x
option, and stage-lock-milestone feature, but didn't manage to solve my problem.
我也读过这个帖子 停止 Jenkins 工作如果启动了较新的情况,但没有解决我的问题.
Also I've read this thread Stopping Jenkins job in case newer one is started, but there is no solution for my problem.
你知道有什么解决办法吗?
Do you know any solution to this?
推荐答案
使用 Jenkins 脚本安全性,这里的许多解决方案变得困难,因为它们使用非白名单方法.
With Jenkins script security many of the solutions here become difficult since they are using non-whitelisted methods.
通过 Jenkinsfile 开始时的这些里程碑步骤,这对我有用:
With these milestone steps at the start of the Jenkinsfile, this is working for me:
def buildNumber = env.BUILD_NUMBER as int
if (buildNumber > 1) milestone(buildNumber - 1)
milestone(buildNumber)
这里的结果是:
- Build 1 运行并创建里程碑 1
- 在构建 1 运行时,构建 2 触发.它有里程碑 1 和里程碑 2.它通过了里程碑 1,这会导致构建 #1 中止.
这篇关于Jenkins - 如果启动新的构建,则中止运行构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!