Controlling the Flow with Stage, Lock, and Milestone
Controlling the Flow with Stage, Lock, and Milestone
https://www.jenkins.io/blog/2016/10/16/stage-lock-milestone/
此方案是按照开启并发执行, 多个building同时执行, 其中有一个building执行成功, 则必此buildding还要早的 building 都会被取消。
stage('Build') {
// The first milestone step starts tracking concurrent build order
milestone()
node {
echo "Building"
}
}
// This locked resource contains both Test stages as a single concurrency Unit.
// Only 1 concurrent build is allowed to utilize the test resources at a time.
// Newer builds are pulled off the queue first. When a build reaches the
// milestone at the end of the lock, all jobs started prior to the current
// build that are still waiting for the lock will be aborted
lock(resource: 'myResource', inversePrecedence: true){
node('test') {
stage('Unit Tests') {
echo "Unit Tests"
}
stage('System Tests') {
echo "System Tests"
}
}
milestone()
}
// The Deploy stage does not limit concurrency but requires manual input
// from a user. Several builds might reach this step waiting for input.
// When a user promotes a specific build all preceding builds are aborted,
// ensuring that the latest code is always deployed.
stage('Deploy') {
input "Deploy?"
milestone()
node {
echo "Deploying"
}
}
milestone后来者运行快,并达成了milestone,则前者运行慢的build则被抛弃。
Concurrent builds of the same job do not always run at the same rate. Depending on the network, the node used, compilation times, test times, etc. it is always possible for a newer build to complete faster than an older build. For example:
Build 1 is triggered
Build 2 is triggered
Build 2 builds faster than Build 1 and enters the Test stage sooner.
Rather than allowing Build 1 to continue and possibly overwrite the newer artifact produced in Build 2, you can use the
milestonestep to abort Build 1:
stage('Build') {
milestone()
echo "Building"
}
stage('Test') {
milestone()
echo "Testing"
}
https://stackoverflow.com/questions/40760716/jenkins-abort-running-build-if-new-one-is-started
另外一种方法, 实现只有一个building执行, 且后启动的building会终止前面的building。
// as a step in a scripted pipeline properties([disableConcurrentBuilds(abortPrevious: true)]) // as a directive in a declarative pipeline options { disableConcurrentBuilds abortPrevious: true }
下面使用lock,实现另外一种控制,
在并发开启的情况下,
当前一个building获取lock并执行, 后续被trigger触发的building, 探测lock还未释放, 则跳过主要业务逻辑。
https://www.jenkins.io/blog/2016/10/16/stage-lock-milestone/
https://www.bing.com/search?pc=MOZI&form=MOZLBR&q=jenkins+pipeline+sleep
https://plugins.jenkins.io/lockable-resources/
Skip executing the block if there is a queue
lock(resource: 'some_resource', skipIfLocked: true) {
echo 'Do something now or never!'
}
lock(resource: 'some_resource', skipIfLocked: true) { stage('Build') { echo "Building" sleep(time:10,unit:"SECONDS") } stage('Test') { echo "Testing" } }

浙公网安备 33010602011771号