본문 바로가기

Web Development/Back-end

[CICD] Github Action (CI) + CodeDeploy (CD)

Github Action + Code Deploy + S3

1. Github Secret 설정 (Repository - Setting - Secrets - Actions에서 키 내용 등록)

-> AWS IAM의 ACCESSKEY_ID 와 ACCESSKEY_SECRET을 각각 생성해준다.

Docker Key는 생성안해도됨

2. S3 버킷 생성

-> 버킷생성

-> 폴더 생성 (build 파일 저장용)

 

3. AWS Role 생성

-> AWS S3 Full Access & CodeDeploy Full Access 권한 롤 생성 (github-action-role)

-> Code Deploy Role 설정

4. AWS EC2 인스턴스 생성 & Role 설정

-> 인스턴스 생성 (20.04 또는 그 이하 버전으로 할 것)

-> 인스턴스에 Role 적용

 

5. AWS Code Deploy 설정

-> 새 애플리케이션 생성

-> 그룹생성

인스턴스 셋팅

 

6. EC2 인스턴스 내 Code Deploy Agent 설치

$ sudo apt update # APT GET UPDATE
$ sudo apt install wget # WGET Install
$ sudo apt install ruby-full # Code Deploy Agent Dependency
$ wget https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install
$ chmod +x ./install # Install 파일 권한 변경
$ sudo ./install auto # Install 파일 실행
$ sudo service codedeploy-agent status # code-deployagent status Check

 

7. Github WorkFlow 설정

-> WorkFlow.yml 파일 작성 [Github Action]

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build" 
  build:
    runs-on: ubuntu-latest
    
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    # 버전확인
    - name: checkout release
      uses: actions/checkout@v3

    # Runs a single command using the runners shell 
    # 아래 명령어를 실행
    - name: Clean temp directory
      run: |
        rm -rf *
        cp -r $GITHUB_WORKSPACE . 
    # 압축하기 tar cvfz (폴더명) (압축 폴더명)
    - name: archive drcloud 
      run: tar cvfz ./drcloud.tar.gz *
   # AWS Credential 설정 (Github Action Credential 셋팅해둔것에 맞추어 )
    - name: AWS configure credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.MY_ACCESS_KEY }}
        aws-secret-access-key: ${{ secrets.MY_SECRET_KEY }}
        aws-region: ap-northeast-2
    # S3 업로드 설정 명령어 (S3 본인에 맞추어 아래 항목들을 수정하시오)
    - name: upload to S3
      run: aws s3 cp --region ap-northeast-2 ./drcloud.tar.gz s3://blockmonkey-assets/test/
    # Code Deploy 설정 (아래 항목들을 수정하시오)
    - name: deploy with AWS codeDeploy
      run: aws deploy create-deployment
        --application-name test
        --deployment-config-name CodeDeployDefault.OneAtATime
        --deployment-group-name testgroup
        --s3-location bucket=blockmonkey-assets,bundleType=tgz,key=test/drcloud.tar.gz

 

-> .appspec.yml (배포 이후 할 작업에 대한 명시 파일) [Code Deploy Agent]

Code Deploy Agent는 s3에서 파일을 해당 인스턴스에 내려 받고 파일 권한 설정.

version: 0.0
os: linux

files:
  - source: /
    destination: /home/ubuntu/drcloud-deploy

permissions:
  - object: /home/ubuntu/drcloud-deploy
    owner: ubuntu
    group: ubuntu
    mode: 755