Jenkins

Jenkins declarative parallel build pipeline

thxxyj 2024. 4. 30. 17:16
728x90

아래 블로그의 글이 상황별 parallel build 에 대해 잘 정리되어있음. 

https://velog.io/@cyeongy/jenkins-parallel-문법

 

jenkins parallel 문법

1. 간단한 병렬 빌드 2. 의존성이 있는 병렬 작업 3. 순서는 존재하나 의존성은 없는 병렬 작업, 빌드

velog.io

 

다음은 블로그 내용을 가져왔습니다.

 

1. 간단한 병렬 빌드

pipeline {
    agent any

    stages {
        stage('First Stage'){
            steps{
                echo "First Stage"
            }
        }
        stage('Second Stage') {
            parallel {
                stage('parallel 1st'){
                    steps{
                        echo 'p1'
                    }
                }

                stage('parallel 2st'){
                    steps{
                        echo 'p2'
                    }
                }

                stage('parallel 3rd'){
                    steps{
                        echo 'p3'
                    }
                }
            }
        }
        stage('Third Stage'){
            steps{
                echo 'Third Stage'
            }
        }
    }
}

 

 

2. 의존성이 있는 병렬 빌드

parallel1과 paralle2와 의존성이 존재할 때
ex) 데이터 백업 혹은 동기화

pipeline {
    agent any

    stages {
        stage('First Stage'){
            steps{
                echo "First Stage"
            }
        }
        stage('parallel1'){
            parallel {
                stage('parallel1 1st'){
                    steps{
                        echo 'p11'
                    }
                }

                stage('parallel1 2st'){
                    steps{
                        echo 'p12'
                    }
                }

                stage('parallel1 3rd'){
                    steps{
                        echo 'p13'
                    }
                }
            }
        }
        stage('parallel2') {
            parallel {
                stage('parallel2 1st'){
                    steps{
                        echo 'p21'
                    }
                }

                stage('parallel2 2st'){
                    steps{
                        echo 'p22'
                    }
                }

                stage('parallel2 3rd'){
                    steps{
                        echo 'p23'
                    }
                }
            }
        }
        
        stage('Third Stage'){
            steps{
                echo 'Third Stage'
            }
        }
    }
}

 

 

3. 순서는 존재하나 의존성은 없는 병렬 빌드

파이프라인끼리 의존성은 없지만 JOB의 순서가 있는 경우, 매우 일반적인 상황
ex) iOS, Android 앱을 동시에 빌드할 때

pipeline {
    agent any

    stages {
        stage('First Stage'){
            steps{
                echo "First Stage"
            }
        }
        stage('Second Stage'){
            parallel {
                stage('parallel1'){
                    stages {
                        stage('parallel1 1st'){
                            steps{
                                echo 'p11'
                            }
                        }

                        stage('parallel1 2st'){
                            steps{
                                echo 'p12'
                            }
                        }

                        stage('parallel1 3rd'){
                            steps{
                                echo 'p13'
                            }
                        }
                    }
                }
            
                stage('parallel2') {
                    stages {
                        stage('parallel2 1st'){
                            steps{
                                echo 'p21'
                            }
                        }

                        stage('parallel2 2st'){
                            steps{
                                echo 'p22'
                            }
                        }

                        stage('parallel2 3rd'){
                            steps{
                                echo 'p23'
                            }
                        }
                    }
                }
            }
        }
        
        stage('Third Stage'){
            steps{
                echo 'Third Stage'
            }
        }
    }
}

 


 

* parallel stage의 구조가 같고, 의존성이 없는 경우 (3번 case)

def StageList = ['StageA', 'StageB', 'StageC']

pipeline {
    agent { label "master" } 
	stages {
		stage ('Parallel') {
		    steps {
		        script {
		            def branchedStages = [:]
        			for(stg in StageList) {
        			    def stgName = stg
        			    branchedStages[stg] = {
        			        lock(label:NODE_LABEL, quantity:1, variable: "node") {
        				        def NODE_NAME = env.node 
        				        echo "Lock node: $NODE_NAME"
        			            stage("1st Stage"){
        			                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
        			                    def firstJob = "FISRT_JOB"
            							def firstBuild = build (
											job: firstJob,
											propagate: false,
											parameters: [
												string(name: "NODE_NAME", value: "${NODE_NAME}"),
												string(name: "PARAMETER1", value: "${PARAMETER1}")											
											]
										)
										JENKINS_BUILD_URL = firstBuild.getAbsoluteUrl()
										JENKINS_STATUS = firstBuild.getCurrentResult()
        			                }
        						}
        						stage("2nd Stage"){
        						    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
            						    def secondJob = "SECOND_JOB"
            							def secondBuild = build (
											job: secondJob,
											propagate: false,
											parameters: [
												string(name: "NODE_NAME", value: "${NODE_NAME}"),
												string(name: "PARAMETER1", value: "${PARAMETER1}"),
												string(name: "PARAMETER2", value: "${PARAMETER2}"),
												string(name: "PARAMETER3", value: "${PARAMETER3}")												
											]
										)
										JENKINS_BUILD_URL = secondBuild.getAbsoluteUrl()
										JENKINS_STATUS = secondBuild.getCurrentResult()
        						    }
        					    }
        					} // lock()
        			    }
        			}
        			parallel branchedStages
		        }
		    }
		}
	} // end of stages
} // end of pipeline
728x90