Nice programing

Jenkins 파이프 라인의 조건부 단계 / 단계

nicepro 2020. 11. 26. 19:50
반응형

Jenkins 파이프 라인의 조건부 단계 / 단계


특정 브랜치를 빌드하는 경우에만 빌드 단계 / 단계를 어떻게 실행합니까?

예를 들어 분기가 호출 된 경우에만 배포 단계를 실행하고 deployment나머지는 모두 그대로 둡니다.


다음은 선언적 파이프 라인 구문에서 동일한 작업을 수행하는 몇 가지 예입니다.

stage('master-branch-stuff'){
  agent any
  when{
    branch 'master'
  }
  steps {
    echo 'run this stage - ony if the branch = master branch'
  }
}

stage('feature-branch-stuff') {
    agent label:'test-node'
    when { branch "feature/*" }
    steps {
        echo 'run this stage - only if the branch name started with feature/'
    }
}

stage('expression-branch') {
    agent label:'some-node'
    when {
    expression {
        return env.BRANCH_NAME != 'master';
        }
    }
    steps {
        echo 'run this stage - when branch is not equal to master'
    }
}

stage('env-specific-stuff') {
    agent label:'test-node'
    when { 
      environment name: 'NAME', value: 'this' 
    }
    steps {
        echo 'run this stage - only if the env name and value matches'
    }
}

더 효과적인 방법-https: //issues.jenkins-ci.org/browse/JENKINS-41187
또한보세요-https: //jenkins.io/doc/book/pipeline/syntax/#when



새로운 WHEN 조항
REF 업데이트 : https://jenkins.io/blog/2018/04/09/whats-in-declarative

같음-두 값 (문자열, 변수, 숫자, 부울)을 비교하고 같으면 true를 반환합니다. 나는 솔직히 우리가 이것을 더 일찍 추가하는 것을 어떻게 놓쳤는 지 잘 모르겠습니다! not {equals ...} 조합을 사용하여 "not equals"비교를 수행 할 수도 있습니다.

changeRequest-가장 간단한 형식으로이 파이프 라인이 GitHub 풀 요청과 같은 변경 요청을 빌드하는 경우 true를 반환합니다. 또한 "마스터 브랜치에 대한 변경 요청입니까?"라고 질문 할 수 있도록 변경 요청에 대해 더 자세한 검사를 수행 할 수도 있습니다. 그리고 훨씬 더.

buildingTag-파이프 라인이 브랜치 나 특정 커밋 참조가 아닌 SCM의 태그에 대해 실행 중인지 확인하는 간단한 조건입니다.

tag-태그 이름 자체를 확인할 수있는 buildingTag에 해당하는 더 자세한 내용입니다.


Just use if and env.BRANCH_NAME, example:

    if (env.BRANCH_NAME == "deployment") {                                          
        ... do some build ...
    } else {                                   
        ... do something else ...
    }                                                                       

참고URL : https://stackoverflow.com/questions/37690920/conditional-step-stage-in-jenkins-pipeline

반응형