Github Action ๋ฐฐํฌ ์ž๋™ํ™” ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์ถ• A-Z

์ž‘๋…„์— 10์›”์— ์ฒ˜์Œ aws์— ์„œ๋ฒ„ ๋ฐฐํฌ๋ฅผ ์‹œ๋„ํ–ˆ์—ˆ๋‹ค. ํ•ด์ปคํ†ค์„ ์ง„ํ–‰ํ•˜๋‹ค ๊ฐœ๋ฐœ์„ ๋๋‚ด๊ณ  ๋ฐฐํฌ๋ฅผ ์‹œ๋„ํ–ˆ๋˜๊ฑด๋ฐ ์•„๋ฌด๋Ÿฐ ์ง€์‹๋„ ์—†์ด ๋งจ๋•…์— ํ•ด๋”ฉํ•˜๋‹ค๋ณด๋‹ˆ ์ˆ˜์—†์ด ์‚ฝ์งˆ์„ ํ–ˆ๋‹ค. jpa + mysql์„ ์‚ฌ์šฉํ•œ ํ”„๋กœ์ ํŠธ์˜€์ง€๋งŒ rds ์„ธํŒ…๋„ ํ•˜์ง€ ์•Š๊ณ  elastic beanstalk์œผ๋กœ ๋ฐฐํฌํ•˜๋ ค ํ–ˆ์—ˆ๋‹ค. ์•„๋ฌดํŠผ 1๋…„์ด ์ง€๋‚œ ์ง€๊ธˆ ์—ฌ์ „ํžˆ aws๋Š” ์–ด๋ ต๋‹ค. ์ด๋ฒˆ์— github action ci/cd ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌ์ถ•ํ–ˆ๋˜ ๊ณผ์ •์„ ๋‚จ๊ฒจ๋‘๋ คํ•œ๋‹ค.

 

 

 

์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

  • ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์€ ์šฐ๋ถ„ํˆฌ๋กœ ํ•˜์˜€๊ณ  ๋”ฐ๋ผํ•  ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋„ ๋„ˆ๋ฌด ๋งŽ์œผ๋‹ˆ ์ƒ๋žต

 

 

ํƒœ๊ทธ ๊ฐ’ ์„ค์ •

  • ์ง€๊ธˆ ์„ค์ •ํ•˜๋Š” ํƒœ๊ทธ๋Š” ๋’ค์—์„œ codedeploy๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค

 

 

์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ํŽธ์ง‘

  • ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™์€ ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ์—ด๋ฉด ๋œ๋‹ค.
  • ํ•„์ž๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์—ด์–ด์ฃผ์—ˆ๋‹ค.
  • ssh ์ ‘์†์„ ์œ„ํ•œ 22๋ฒˆ ํฌํŠธ
  • ์Šคํ”„๋ง ์‹คํ–‰์„ ์œ„ํ•œ 8080 ํฌํŠธ
  • http, https ์ ‘์†์„ ์œ„ํ•œ 80, 443 ํฌํŠธ
  • mysql์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ 3306 ํฌํŠธ๋ฅผ ์—ด์–ด๋‘์—ˆ๋‹ค.

 

 

ํƒ„๋ ฅ์  IP ์—ฐ๊ฒฐ

  • ํƒ„๋ ฅ์  IP ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด์ „์— ๋งŒ๋“  ec2์— ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.
  • ํƒ„๋ ฅ์  IP๋ฅผ ์—ฐ๊ฒฐํ•˜์ง€ ์•Š์œผ๋ฉด ec2์˜ IP๊ฐ€ ๊ณ„์† ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.
  • ๋ฌด์—‡๋ณด๋‹ค ํƒ„๋ ฅ์  IP๋ฅผ ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๊ณ  ec2๊ฐ€ ์ผœ์ ธ์žˆ์œผ๋ฉด ์š”๊ธˆ์ด ๋ถ€๊ณผ๋œ๋‹ค.
  • ๋ฐ˜๋Œ€๋กœ ํƒ„๋ ฅ์  IP๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ec2์— ์—ฐ๊ฒฐํ•˜์ง€ ์•Š์œผ๋ฉด ์š”๊ธˆ์ด ๋ถ€๊ณผ๋œ๋‹ค.

 

 

ssh๋ฅผ ํ†ตํ•ด ec2 ์ ‘์†

  • ๊ธˆ๋ฐฉ ํ•ด๊ฒฐํ–ˆ๋˜ ๋ฌธ์ œ์ด๊ธด ํ•˜์ง€๋งŒ C๋“œ๋ผ์ด๋ธŒ ๋ฐ”๋กœ ์•„๋ž˜์— pem ํ‚ค๋ฅผ ๋„ฃ์œผ๋‹ˆ ๋“ค์–ด๊ฐ€์ง€์ง€ ์•Š์•˜๋‹ค.
  • ๋ฐ”ํƒ•ํ™”๋ฉด์— pemํ‚ค๋ฅผ ๋‘๊ณ  ec2์— ์ ‘์†ํ•˜๋‹ˆ ์ž˜ ๋“ค์–ด๊ฐ€์กŒ๋‹ค. ์ด์œ ๋Š”..๋ชจ๋ฅด๊ฒ ๋‹ค

 

 

ec2 ์„ธํŒ…

sudo apt update && sudo apt upgrade # ubuntu ์—…๋ฐ์ดํŠธ ๋ฐ ์—…๊ทธ๋ ˆ์ด๋“œ
sudo apt install openjdk-17-jdk # ์ž๋ฐ” ์„ค์น˜
java -version # ๋ฒ„์ „ ํ™•์ธ

sudo apt-get install git # ๊นƒ ์„ค์น˜
git --version # ๊นƒ ๋ฒ„์ „ ํ™•์ธ

# ์•„๋ž˜๋Š” aws ๊ฐ€์ด๋“œ๋ผ์ธ ๋”ฐ๋ผํ•œ๊ฒƒ
sudo apt install ruby-full
sudo apt install wget
wget https://{bucket-name}.s3.{region-identifier}.amazonaws.com/latest/install
wget https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status  #code deploy ์ž‘๋™ ํ™•์ธ

 

 

IAM ๋“ฑ๋ก

  • ์‚ฌ์ง„๊ณผ ๊ฐ™์ด s3 ์™€ deploy์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

 

์œ„์—์„œ ๋งŒ๋“  IAM์„ ec2์— ๋“ฑ๋กํ•ด์ค€๋‹ค.

 

 

(์ฐธ๊ณ ) pemํ‚ค ์—†์ด ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ec2์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ธํŒ…

  • ec2 ์„œ๋ฒ„์—์„œ ์•„๋ž˜ ํŒŒ์ผ์— PasswordAuthentication์„ yes๋กœ ๋ณ€๊ฒฝ
  • sudo vi /etc/ssh/sshd_config sudo su - #ubuntu์— ๊ถŒํ•œ ๋ถ€์—ฌ passwd ubuntu #ํŒจ์Šค์›Œ๋“œ ๋ณ€๊ฒฝ exit ํ›„ #ec2๋กœ ๋‚˜๊ฐ€์„œ sudo service ssh restart #์žฌ์‹œ์ž‘

  • ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ssh ubuntu@{ip}.ap-northeast-2.compute.amazonaws.com ์„ ํ†ตํ•ด ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘์† ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

S3 ์ƒ์„ฑ

  • ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ s3์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์„ธํŒ…ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

 

IAM ์‚ฌ์šฉ์ž ์ƒ์„ฑ

  • ์•„๋ž˜์™€ ๊ฐ™์ด ์•ก์„ธ์Šค ํ‚ค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • ์ด ์•ก์„ธ์Šค ํ‚ค๋Š” ์žŠ์–ด๋ฒ„๋ฆฌ์ง€ ์•Š๊ฒŒ ๋”ฐ๋กœ ์ €์žฅํ•ด๋‘ฌ์•ผ ํ•œ๋‹ค.
  • ๋‚˜์ค‘์— github action ์—์„œ ์‚ฌ์šฉ ์˜ˆ์ •

 

 

github setting

  • AWS_ACCESS_KEY_ID์™€ AWS_SECRET_ACCESS_KEY๋Š” ๋ฐ”๋กœ ์œ„์—์„œ ๋ฐ›์€ ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด์ค€๋‹ค.
  • AWS_REGION์€ ap-northeast-2๋กœ ์„ธํŒ…ํ•˜๋ฉด ๋œ๋‹ค.

 

 

Code Deploy ๊ถŒํ•œ ๋ถ€์—ฌ ๋ฐ ์ƒ์„ฑ

  • ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋ถ€์—ฌ ๋ฐ ์ƒ์„ฑ

 

 

์ƒ์„ฑ ํ›„ deployment group ์„ค์ •

  • ์œ„์—์„œ ๋งŒ๋“  IAM์„ code-deploy์— ๋“ฑ๋กํ•ด์ค€๋‹ค.

  • ์ œ์ผ ์ฒ˜์Œ ec2์— ๋“ฑ๋กํ–ˆ๋˜ ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•˜์—ฌ ec2์™€ code deploy๋ฅผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

 

 

./github/workflows/deploy.tml ์ƒ์„ฑ

name: CI-CD

# ์ด๋ฒคํŠธ ํŠธ๋ฆฌ๊ฑฐ ์„ค์ •
# push ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์‹คํ–‰
# release ๋ธŒ๋žœ์น˜์— push๊ฐ€ ์ด๋ฃจ์–ด์กŒ์„ ๋•Œ๋งŒ ์›Œํฌํ”Œ๋กœ์šฐ ์‹คํ–‰
on:
  push:
    branches:
      - release

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜
env:
  S3_BUCKET_NAME: colony-deploy-bucket
  CODE_DEPLOY_APPLICATION_NAME: CODE-DEPLOY-COLONY-TEST
  CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: CODE-DEPLOY-GROUP

# ์ž‘์—… ์ •์˜
jobs:
  build:
    runs-on: ubuntu-latest # ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „์˜ ์šฐ๋ถ„ํˆฌ๋ฅผ ์‚ฌ์šฉ

    steps:
      - name: Checkout # ์ €์žฅ์†Œ ์ฒดํฌ์•„์›ƒ
        uses: actions/checkout@v2

      - name: Set up JDK 17 # JDK 17 ์„ค์น˜
        uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: 17

      - name: Grant execute permission for gradlew # gradlew ์‹คํ–‰ ๊ถŒํ•œ ๋ถ€์—ฌ
        run: chmod +x ./gradlew
        shell: bash

      - name: Build with Gradle  # Gradle์„ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œ
        run: ./gradlew build
        shell: bash

      - name: Make zip file # zip ํŒŒ์ผ ์ƒ์„ฑ
        run: zip -r ./$GITHUB_SHA .
        shell: bash

      - name: Configure AWS credentials # AWS ์ธ์ฆ ์ •๋ณด ์„ค์ •
        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 # ์ƒ์„ฑ๋œ zip ํŒŒ์ผ์„ S3์— ์—…๋กœ๋“œ
        run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip

      - name: Code Deploy
        run: |
          aws deploy create-deployment \
          --deployment-config-name CodeDeployDefault.AllAtOnce \
          --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
          --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
          --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=$GITHUB_SHA.zip

 

 

appspec.yml ์ƒ์„ฑ

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/github-action
    overwrite: yes

permissions: # ํŒŒ์ผ ๋ฐ ํด๋”์˜ ๊ถŒํ•œ ์„ค์ •
  - object: /
    pattern: "**"
    owner: ubuntu
    group: ubuntu

hooks: # ํ›„ํฌ ์‹คํ–‰ ์„ค์ •
  ApplicationStart:
    - location: scripts/gh_deploy.sh # ์‹คํ–‰ํ•  ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์˜ ์œ„์น˜
      timeout: 60
      runas: ubuntu

 

 

codedeploy-agent๋ฅผ ec2์— ์ ์šฉ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์žฌ์‹œ์ž‘ ๋ฐ ๋กœ๊ทธ์„ค์ •

  • ์—ฌ๊ธฐ๊นŒ์ง€ ๋๋‹ค๋ฉด ec2 ํ„ฐ๋ฏธ๋„์— codedeploy-agent๋ฅผ ์žฌ์‹œ์ž‘ํ•˜์—ฌ ์ ์šฉ์‹œํ‚จ๋‹ค.
  • sudo service codedeploy-agent restart (์ข€ ๊ฑธ๋ฆผ) tail -F /var/log/aws/codedeploy-agent/codedeploy-agent.log

 

 

snapshot.jar ์ƒ์„ฑ๋˜์ง€ ์•Š๋„๋ก ์„ค์ •

  • jarํŒŒ์ผ์„ ์ฐพ์•„์„œ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๊ฒŒ ํ•˜๋Š”๋ฐ snapshot๊นŒ์ง€ ์žˆ๋‹ค๋ฉด jarํŒŒ์ผ์ด ๋‘๊ฐœ๊ฐ€ ๋œ๋‹ค.
  • jarํŒŒ์ผ์ด ํ•˜๋‚˜์—ฌ์•ผ๋งŒ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ธฐ์— build ์‹œ snapshot.jar๋Š” ์ƒ์„ฑ๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.
//SNAPSHOT ์ƒ์„ฑ x 
jar { 
	enabled = false 
}

 

 

sh ํŒŒ์ผ ์ƒ์„ฑ

  • ์—ฌ๊ธฐ์„œ ์‚ฝ์งˆ์„ ๊ฝค๋‚˜ ํ–ˆ๋‹ค.
  • ymlํŒŒ์ผ์— ํ•„์š”ํ•œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ ์„ธํŒ…ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„  ec2 ๋‚ด๋ถ€์— ์„ธํŒ…์ด ํ•„์š”ํ–ˆ๋‹ค.
  • vim ~/.bashhrc ์— ๋“ค์–ด๊ฐ€ export๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค.
  • export SPRING_PROFILES_ACTIVE = dev
  • ์ด๋ ‡๊ฒŒ ํ•œ ํ›„ sh ํŒŒ์ผ์— source ~/.bashrc ๋ฅผ ์ถ”๊ฐ€ํ•ด jar์‹คํ–‰ ์ „ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์ฃผ๋ฉด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.
#!/bin/bash
PROJECT_NAME="colony"
JAR_PATH="/home/ubuntu/colony/build/libs/*.jar"
DEPLOY_PATH=/home/ubuntu/$PROJECT_NAME/
DEPLOY_LOG_PATH="/home/ubuntu/$PROJECT_NAME/deploy.log"
DEPLOY_ERR_LOG_PATH="/home/ubuntu/$PROJECT_NAME/deploy_err.log"
APPLICATION_LOG_PATH="/home/ubuntu/$PROJECT_NAME/application.log"
BUILD_JAR=$(ls $JAR_PATH)
JAR_NAME=$(basename $BUILD_JAR)

echo "===== ๋ฐฐํฌ ์‹œ์ž‘ : $(date +%c) =====" >> $DEPLOY_LOG_PATH

echo "> build ํŒŒ์ผ๋ช…: $JAR_NAME" >> $DEPLOY_LOG_PATH
echo "> build ํŒŒ์ผ ๋ณต์‚ฌ" >> $DEPLOY_LOG_PATH
cp $BUILD_JAR $DEPLOY_PATH

echo "> ํ˜„์žฌ ๋™์ž‘์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ์ฒดํฌ" >> $DEPLOY_LOG_PATH
CURRENT_PID=$(pgrep -f $JAR_NAME)

if [ -z $CURRENT_PID ]
then
  echo "> ํ˜„์žฌ ๋™์ž‘์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์กด์žฌ X" >> $DEPLOY_LOG_PATH
else
  echo "> ํ˜„์žฌ ๋™์ž‘์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์กด์žฌ O" >> $DEPLOY_LOG_PATH
  echo "> ํ˜„์žฌ ๋™์ž‘์ค‘์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ•์ œ ์ข…๋ฃŒ ์ง„ํ–‰" >> $DEPLOY_LOG_PATH
  echo "> kill -9 $CURRENT_PID" >> $DEPLOY_LOG_PATH
  kill -9 $CURRENT_PID
fi

DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME
echo "> DEPLOY_JAR ๋ฐฐํฌ" >> $DEPLOY_LOG_PATH
source ~/.bashrc  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ธํŒ…
nohup java -jar $DEPLOY_JAR >> $APPLICATION_LOG_PATH 2> $DEPLOY_ERR_LOG_PATH &

sleep 3

echo "> ๋ฐฐํฌ ์ข…๋ฃŒ : $(date +%c)" >> $DEPLOY_LOG_PATH
  • ์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด ci/cd ๋ฐฐํฌ ์ž๋™ํ™” ์„ธํŒ…์€ ๋์ด๋‚œ๋‹ค.
  • ์ด์ œ release ๋ธŒ๋žœ์น˜์— push ๋˜๋ฉด ๋นŒ๋“œ ํ›„ s3๋กœ zip ํŒŒ์ผ์ด ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋˜๊ณ  ec2์—์„œ jarํŒŒ์ผ์ด ์‹คํ–‰๋˜๊ฒŒ ๋œ๋‹ค.

 

 

 

์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด  ci/cd ๋ฐฐํฌ ์ž๋™ํ™” ์„ธํŒ…์€ ๋์ด๋‚œ๋‹ค.

์ด์ œ release ๋ธŒ๋žœ์น˜์— push ๋˜๋ฉด ๋นŒ๋“œ ํ›„ s3๋กœ zip ํŒŒ์ผ์ด ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋˜๊ณ  ec2์—์„œ jarํŒŒ์ผ์ด ์‹คํ–‰๋˜๊ฒŒ ๋œ๋‹ค.