본문 바로가기

DevOps와 CI CD

Jenkins를 이용한 CI/CD Pipeline 구축 (5) Pipeline (Advanced Jenkins 사용)

Pipeline

각 단계별로 순서와 실행을 자동으로 할 수 있는 작업

 

 

이 작업은 스크립트로도 할 수 있음

(1) Declarative : Jenkins pipeline의 최신 기능, 보다 풍부한 기능 제공

 

(2) Scripted (Groovy + DSL)

 

- 차이점

(1) 시작 시, 유효성 검사 유무

(2) 특정 Stage 실행 가능 여부

(3) 제어문

(4) Option

Declarative Pipelines 작성 예시

pipeline {
	agent any
    
    environment {
     mvnHome = "/home/jenkins/test"
   }
    
    stages {
    	steps('build') {
        	//
        }
        steps('test') {
			//
		}
        steps('deploy') {
			//
		}  
    }
}

 

Scripted Pipelines 작성 예시

node {
	def mvnHome = "/home/jenkins/test"

	stage('build') {
    	//
    }
    
    stage('test') {
    	//
    }
    
    stage('deploy') {
    	//
    }
}

시스템 : 마스터, 노드, 에이전트, 엑시큐터

모든 젠킨스 파이프라인은 코드를 실행하기 위해 하나 이상의 시스템 필요

(1) 마스터 Master

젠킨스 인스턴스 제어에 중심이 되는 역할

다른 시스템에 정의되지 않았다면 잡을 실행하는 기본 장소

무거운 작업은 다른 시스템에서 수행하는 것이 좋음

    - 마스터에서 수행되는 잡에는 모든 데이터, 환경 설정, 작업에 대해 마스터와 같은 접근 권한이 있는 보안 위협 때문

    - 마스터가 과부하로 인해 중단되면 안 되기 때문

 

(2) 노드 (Node)

젠킨스 잡을 실행할 수 있는 시스템

마스터 혹은 에이전트가 포함되며 도커와 같은 컨테이너를 의미할 때도 있음

 

 

(3) 에이전트 (Agent)

구 젠킨스의 슬레이브 (slave), 전통적으로 마스터가 아닌 시스템을 의미

마스터에 의해 관리되고 필요에 의해 할당되어 각 잡의 수행을 담당

에이전트와 노드의 관계

    - 스크립트 방식 : 노드는 에이전트가 있는 시스템을 지칭

    - 서술적 : 특정 에이전트를 명시해 노드를 할당

 

 

 

(4) 엑시큐터 (Executor)

노드나 에이전트에 잡을 실행시키는 장소

노드는 엑시큐터를 여러개를 갖고 있을 수도 있고 ,하나도 갖고 있지 않을 수도 있음

버전에 따라 1개 이상 지정해야 하는 경우도 있음

 

 

 

 

파이프라인 스크립트 연습

1

pipeline {
    agent any
    stages {
        stage('Compile') {
            steps {
                echo "Compiled successfully!";
            }
        }
        stage('JUnit') {
            steps {
                echo "JUnit passed successfully!";
            }
        }
        stage('Code Analysis') {
            steps {
                echo "Code Analysis completed successfully!";
            }
        }
        stage('Deploy') {
            steps {
                echo "Deployed successfully!";
            }
        }
    }
}

 

2. 

 

pipeline {
    agent any
    stages {
        stage('Compile') {
            steps {
                echo "Compiled successfully!";
            }
        }
        stage('JUnit') {
            steps {
                echo "JUnit passed successfully!";
            }
        }
        stage('Code Analysis') {
            steps {
                echo "Code Analysis completed successfully!";
            }
        }
        stage('Deploy') {
            steps {
                echo "Deployed successfully!";
            }
        }
    }

    post {
      always {
        echo "This will always run"
      }
      success {
        echo "This will run when the run finished successfully"
      }
      failure {
        echo "This will run if failed"
      }
      unstable {
        echo "This will run when the run was marked as unstable"
      }
      changed {
        echo "This will run when the state of the pipeline has changed"
      }
    }
}

 

3. git에서 코드 가져와서 파이프라인 실행하기

.sh 는 리눅스에서 실행 가능한 파일 , 윈도우는 .bat

 

pipeline {
    agent any
    stages {
        stage('Git clone') {
            steps {
                git 'https://github.com/joneconsulting/jenkins_pipeline_script';
            }
        }
        stage('Compile') {
            steps {
                echo "Compiled successfully!";
                sh './build.sh'
            }
        }
        stage('JUnit') {
            steps {
                echo "JUnit passed successfully!";
                sh './unit.sh'
            }
        }
        stage('Code Analysis') {
            steps {
                echo "Code Analysis completed successfully!";
                sh './quality.sh'
            }
        }
        stage('Deploy') {
            steps {
                echo "Deployed successfully!";
                sh './deploy.sh'
            }
        }
    }
}

 

 

4. Maven 빌드 추가하기

pipeline {
    agent any
    tools { 
      maven 'Maven3.8.5'
    }
    stages {
        stage('github clone') {
            steps {
                git branch: 'main', url: 'https://github.com/joneconsulting/cicd-web-project.git'; 
            }
        }
        
        stage('build') {
            steps {
                sh '''
                    echo build start
                    mvn clean compile package -DskipTests=true
                '''
            }
        }
    }
}

 

 

5. 4번의 빌드 결과물을 Tomcat 서버에 배포

pipeline {
    agent any
    tools { 
      maven 'Maven3.8.5'
    }
    stages {
        stage('github clone') {
            steps {
                git branch: 'main', url: 'https://github.com/joneconsulting/cicd-web-project.git'; 
            }
        }
        stage('build') {
            steps {
                sh '''
                    echo build start
                    mvn clean compile package -DskipTests=true
                '''
            }
        }
        stage('deploy') {
            steps {
                ~~~
            }
        }
    }
}

 

 

 

참고

현재 도커에서 실행되고 있는 jenkins는 리눅스에서 실행되고 있는 것

리눅스에서는 .sh (쉘스크립트 파일) 을 실행할 수 있고 윈도우 에서는 .bat (배치 파일)