Last active
October 1, 2018 06:00
-
-
Save naive924/3c7f4f8f0595fbda1a30ef1aa876ed47 to your computer and use it in GitHub Desktop.
Jenkins – 同一 branch を Groovy を使って排他制御
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 同一案件番号のジョブが実行されていたら待つ | |
waitForSameTargetJOB(); | |
// マスタ系処理を実行 | |
: | |
: | |
// ------------------------------- | |
def waitForSameTargetJOB () { | |
def retrycount = 5000 | |
def sleepSec = 3 | |
def canProcess = true; | |
for(i in 1..retrycount) { | |
if(isSameJOBRunning() == false) { | |
return; | |
} | |
sleep(sleepSec * 1000) | |
if(i >= retrycount -1){ | |
throw new hudson.AbortException("Retry exceeded maximum."); | |
} | |
} | |
} | |
def isSameJOBRunning() { | |
def isRunning = false; | |
currentJob.getBuilds().each { | |
if(it.isInProgress()){ | |
def branch_id = it. getBuildVariables()['TARGET_BRANCH_ID'] | |
if( branch_id.equals(target_branch_id) && it.environment.get("BUILD_NUMBER") < myBuildNo) { | |
println " *** Waiting for the depending job. $branch_id " | |
isRunning = true; | |
return; | |
} | |
} | |
} | |
return isRunning; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment