Jenkins Pipeline

연수·2022년 3월 6일
0

CI/CD

목록 보기
3/4

✔️ Github 연동

[연동 방법]

  1. SSH 키 사용
    • SSH 키를 생성하여 각각 public key와 private key를 github과 jenkins에 입력해준다.
    • Jenkins global credentials 생성
    • jenkins pipeline 생성 시 credentials 함께 적용
  2. github private token 사용
    • github personal access token 생성
    • 해당 토큰을 이용해 Jenkins global credential 생성
    • Jenkins 시스템 설정에서 github server 추가

[Webhook]

  1. repository - settings - webhooks에서 webhook 추가
    • Payload URL : {jenkins URL}/github-webhook
    • Content type : application/json
  2. trigger가 동작하는 브랜치에 push 동작을 수행하면 jenkins pipeline이 동작한다.

 

✔️ AWS 연동

[연동 방법]

  1. aws credentials, ECR 관련 플러그인 설치
  2. AWS 엑세스 키 생성
  3. Jenkins에서 aws credential 등록

 


✔️ Jenkins Pipeline

  1. Jenkins 서버에 aws-cli 설치 (는 꼭 안 해도 될듯)

    $ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    $ unzip awscliv2.zip
    $ sudo ./aws/install
    $ aws --version # 설치 확인
    $ aws configure
    $ aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 337260748354.dkr.ecr.ap-northeast-1.amazonaws.com
  2. Jenkinsfile

    [참고] https://teichae.tistory.com/entry/Jenkins-Pipeline을-이용한-Docker-Image를-ECR로-Push

    node {
         stage('Clone repository') {
             checkout scm
         }
    
         stage('Build spring image') {
             spring = docker.build("{리포지토리 주소}/spring_prd:latest", "-f {spring Dockerfile 경로} .")
         }
    
         stage('Push spring image') {
             sh 'rm  ~/.dockercfg || true'
             sh 'rm ~/.docker/config.json || true'
             
             docker.withRegistry('https://{리포지토리 주소}', 'ecr:ap-northeast-1:aws') {
                 spring.push("latest")
         }
      }
    
         stage('Build nginx image') {
             nginx = docker.build("{리포지토리 주소}/nginx_prd:latest", "-f {nginx Dockerfile 경로} .")
         }
    
         stage('Push nginx image') {
             docker.withRegistry('https://{리포지토리 주소}', 'ecr:ap-northeast-1:aws') {
                 nginx.push("latest")
         }
    
         stage('Deploy nginx') {
             withAWS(region: 'ap-northeast-1', credentials: 'aws') {
                         sh 'aws ecs update-service --region ap-northeast-1 --cluster ecs-prd-nginx-01 --service ecs-service-prd-nginx-01 --force-new-deployment'
                     }
         }
    
         stage('Deploy spring') {
             withAWS(region: 'ap-northeast-1', credentials: 'aws') {
                         sh 'aws ecs update-service --region ap-northeast-1 --cluster ecs-prd-spring-01 --service ecs-service-prd-spring-01 --force-new-deployment'
                     }
         }
    
      }
    }
  3. 파이프라인 구성

    • Build Triggers: github hook trigger for GITScm polling
    • Pipeline Definition: Pipeline script from SCM
      • SCM: Git (repo url과 credentials 입력)
      • branch: dev/main
      • script path: Jenkinsfile

 


✔️ Jenkins Pipeline

  1. Clone Repository
  2. Build spring boot image
  3. push spring boot image to ECR
  4. build nginx image
  5. push nginx image to ECR
  6. deploy nginx
  7. deploy spring boot

 


✔️ 자동 배포 및 롤링 업데이트

  • git push를 하면 Jenkins pipeline이 실행된다.
  • pipeline의 deploy 단계까지 수행되면 새로운 컨테이너가 띄워지고 기존 컨테이너는 중지된다.
  • 이러한 과정으로 2개의 컨테이너가 계속해서 유지된다.

 

profile
DCDI

0개의 댓글