[CICD] Github action, S3, CodeDeploy를 활용한 CICD 구축

Continuous Integration


IAM 사용자 추가

+ 보안 자격 증명에서 엑세스 키 발급 후 csv 파일 저장

 

 

WorkFlow 작성

name: freelec-springboot2-webservice

on:
  push:
    branches:
      - main # (1) 브랜치 지정
  workflow_dispatch: # (2) 수동 실행

env:
  S3_BUCKET_NAME: "freelec-deploy"
  PROJECT_NAME: "freelec-springboot2-webservice"


jobs:
  build:
    runs-on: ubuntu-latest # (3)

    steps:
      - name: Checkout
        uses: actions/checkout@v2 # (4)

      - name: Set up JDK 11
        uses: actions/setup-java@v1 # (5)
        with:
          java-version: 11

      - name: Grant execute permission for gradlew
        run: chmod +x ./gradlew # (6)
        shell: bash

      - name: Build with Gradle
        run: ./gradlew clean build # (7)
        shell: bash

# 파일명에 시간 추가
      - name: Get current time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH-mm-ss # (1)
          utcOffset: "+09:00"

      - name: Show Current Time
        run: echo "CurrentTime=${{steps.current-time.outputs.formattedTime}}" # (2)
        shell: bash


      ### S3 업로드 ###
      - name: Make zip file
        run: zip -r ./$GITHUB_SHA.zip .
        shell: bash

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Upload to S3
        run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$PROJECT_NAME/$GITHUB_SHA.zip

 

GIthub Secret에 AWS Key 등록하기

S3 생성할 때 받은 key랑 region을 등록한 다음 git push를 하면 Actions에 들어가서 진행 상태를 볼 수 있다.

 

 

 

 

 

 

 

Continuous Delivery


IAM에서 EC2에 대한 역할 설정

  1. 오른쪽 상단의 역할 만들기
  2. AWS 서비스, EC2 선택
  3. 이전 IAM 설정과 동일한 S3FullAccess, CodeDeployFullAccess 추가
  4. 태그 생성

EC2 -> 보안 -> IAM 역할 수정

 

IAM -> 역할 만들기 -> codeDeploy 선택 -> 생성

CodeDeploy 생성 -> 배포 그룹 생성

에이전트 설치X & 로드 밸런싱 활성화X

 

EC2에 CodeDeploy 에이전트 설치

CodeDeploy Agent 설치 레퍼런스

# 패키지 매니저 업데이트, ruby 설치
sudo yum update
sudo yum install ruby
sudo yum install wget

# 서울 리전에 있는 CodeDeploy 리소스 키트 파일 다운로드
cd /home/ec2-user
wget https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install

# 설치 파일에 실행 권한 부여
chmod +x ./install

# 설치 진행 및 Agent 상태 확인
sudo ./install auto
sudo service codedeploy-agent status


#running이라고 출력이 안되면 실행
sudo service codedeploy-agent start

 

배포스크립트 작성

 

appspec.yml 루트 폴더에 작성

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ec2-user/app/step2/freelec-springboot2-webservice # 프로젝트 이름
    overwrite: yes

permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user

destination은 s3에서 받아온 프로젝트 위치

 

 

기존 workflow 파일에 아래 내용 추가

      ### codeDeploy
      - name: Code Deploy
        run: aws deploy create-deployment --application-name logging-system-deploy --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name develop --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=$PROJECT_NAME/$GITHUB_SHA.zip

 

아래 내용이 기억나지 않는다면 CodeDeploy 애플리케이션에 들어가면 확인할 수 있다.

  •  application-name
    • CodeDeploy 애플리케이션의 이름
  • deployment-config-name
    • 배포 그룹 설정에서 선택했던 배포 방식
  • deployment-group-name
    • 배포 그룹의 이름
  • s3-location
    • jar를 S3에서 가지고 오기 위해 차례로 bucket 이름, 파일 타입, 파일 경로를 입력

 

정상 실행됐다면 배포내역과 지정한 경로에서 확인할 수 있다.

 

전체 흐름

Github Actions(workflow.yml)

  1. github에 push된 코드를 build 테스트
  2. S3에 소스 코드 업로드
  3. CodeDeploy를 통해 S3에 업로드된 파일을 EC2에 배포

CodeDeploy Agent(Appspec.yml)

  • CodeDeploy를 통해 받아온 Appspec 파일을 읽어 명시된 스크립트 실행

ShellScript(Appspec.yml에 명시된 .sh 파일)

  • 스크립트에 작성된 내용(기존 서버를 종료하고 업데이트된 서버 실행)을 실행

 

Reference

https://wbluke.tistory.com/40

'DevOps > 배포' 카테고리의 다른 글

SCP 배포 방법  (0) 2023.08.03
[무중단 배포] Nginx + CodeDeploy + Github Actions  (0) 2023.08.03
[EC2] 배포 및 간단한 쉘 스크립트 작성  (2) 2023.07.31
[EC2] Linux 인스턴스 생성  (0) 2023.07.31
Jar, War 차이  (0) 2023.07.22