Skip to content

Instantly share code, notes, and snippets.

@naive924
Last active October 1, 2018 06:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naive924/3c7f4f8f0595fbda1a30ef1aa876ed47 to your computer and use it in GitHub Desktop.
Save naive924/3c7f4f8f0595fbda1a30ef1aa876ed47 to your computer and use it in GitHub Desktop.
Jenkins – 同一 branch を Groovy を使って排他制御
// 同一案件番号のジョブが実行されていたら待つ
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