본문 바로가기
IT Knowledge/Jenkins

MultiBranches pipleline 방식 주의사항!(Branch Indexing)

by Seok. 2023. 9. 3.
반응형

[이슈사항]

MultiBranches Pipleline 새로운 브랜치를 indexing 하면서, Jenkinsfile 참조하여 빌드를 실행하였다.

그런데, Jenkinsfile에는 Deploy & Service Restart 로직까지 넣어두면서, 의도하지 않는 배포가 발생하였다.

 

 

[원하는 결과]

사용자가 pipeline을 실행하였을때만, 빌드&배포가 발생하기를 바란다. 

(Branch 가 indexing 될때는 pipeline이 실행되지 않기를 바란다.)

 

 

[해결방안]

1. Branch API 2.3.0 and Basic Branch Build Strategies 1.3.0 설치해서 사용.

  • "skip initial build on branch indexing" 옵션을 사용한다.


 

2. "Jenkinsfile" Declarative pipleline 사용

// execute this before anything else, including requesting any time on an agent
if(currentBuild.getBuildCauses().toString().contains('BranchIndexingCause')) {
  print"INFO: Build skipped due to trigger being Branch Indexing"
  currentBuild.result='ABORTED'
  // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful
  return
}

Build Causes "BranchIndexingCause" 경우, 트리거된 빌드는 아무것도 실행하지 않고 건너뜁니다.

 


 

3. 조건부 실행문 작성

  • When, expression 구문 사용
  • When expression 블록의 조건이 참이라면, Stage 나머지 코드가 실행된다. (조건이 참이 아니라면 코드는 실행되지 않는다)
pipeline {
    agent none
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                expression {
                    currentBuild.buildCauses.toString().contains("Push event to branch")
                }
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

 

참고 : <https://www.jvt.me/posts/2020/02/23/jenkins-multibranch-skip-branch-index/>

반응형

댓글