Skip to content

Commit

Permalink
deploy: #114 - CodeDeploy 적용 및 Script 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sasca37 committed Feb 1, 2024
1 parent 6fa0bfa commit edc5cdb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/github-actions-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ on:

env:
S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME_DEV }}
# CODE_DEPLOY_APPLICATION_NAME:
# CODE_DEPLOY_DEPLOYMENT_GROUP:
CODE_DEPLOY_APPLICATION_NAME: dodal-code-deploy
CODE_DEPLOY_DEPLOYMENT_GROUP: deploy

permissions:
contents: read
Expand Down Expand Up @@ -55,8 +55,8 @@ jobs:
run: |
chmod +x ./build/libs/*.jar
cp ./build/libs/*.jar ./deploy/
# cp ./appspec.yml ./deploy/
# cp -r ./scripts ./deploy/
cp ./appspec.yml ./deploy/
cp -r ./scripts ./deploy/
# 압축파일 형태로 전달
- name: Make zip file
Expand All @@ -72,4 +72,18 @@ jobs:
run: |
aws s3 cp \
--region ap-northeast-2 \
--acl private ./deploy/deploy-dev.zip s3://${{ env.S3_BUCKET_NAME }}/deploy/deploy-dev.zip
--acl private ./deploy/deploy-dev.zip s3://${{ env.S3_BUCKET_NAME }}/deploy/deploy-dev.zip
# CodeDeploy
- name: Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEPLOY_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_SECRET_ACCESS_KEY }}
run: |
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP }} \
--file-exists-behavior OVERWRITE \
--ignore-application-stop-failures \
--s3-location bucket=${{ env.S3_BUCKET_NAME }},bundleType=zip,key=deploy/deploy-dev.zip \
--region ap-northeast-2
16 changes: 16 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/app/deploy
overwrite: yes
permissions:
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 300
runas: ec2-user
41 changes: 41 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

SERVICE_PATH="/home/ec2-user/app/deploy"

echo ">>> 모든 Docker 컨테이너를 정지하고 삭제합니다." >> $SERVICE_PATH/deploy.log
sudo docker stop $(sudo docker ps -qa)
sudo docker rm $(sudo docker ps -qa)

echo ">>> 모든 Docker 이미지를 삭제합니다." >> $SERVICE_PATH/deploy.log
sudo docker image prune -a -f

echo ">>> Redis 이미지를 가져옵니다." >> $SERVICE_PATH/deploy.log
sudo docker pull redis:alpine

echo ">>> Redis 컨테이너를 실행합니다." >> $SERVICE_PATH/deploy.log
sudo docker run --name redis -d -p 6379:6379 redis:alpine

BUILD_JAR=$(ls $SERVICE_PATH/*.jar)
JAR_NAME=$(basename $BUILD_JAR)

echo ">>> build 파일명: $JAR_NAME" >> $SERVICE_PATH/deploy.log

echo ">>> build 파일 복사" >> $SERVICE_PATH/deploy.log
DEPLOY_PATH=$SERVICE_PATH/
cp $BUILD_JAR $DEPLOY_PATH

echo ">>> 현재 실행중인 애플리케이션 pid 확인" >> $SERVICE_PATH/deploy.log
CURRENT_PID=$(pgrep -f $JAR_NAME)

if [ -z $CURRENT_PID ]
then
echo ">>> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다." >> $SERVICE_PATH/deploy.log
else
echo ">>> kill -15 $CURRENT_PID"
kill -15 $CURRENT_PID
sleep 5
fi

DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME
echo ">>> DEPLOY_JAR 배포" >> $SERVICE_PATH/deploy.log
nohup java -jar -Dspring.profiles.active=dev $DEPLOY_JAR >> /home/ec2-user/member-service.log 2> $SERVICE_PATH/member-service.log &

0 comments on commit edc5cdb

Please sign in to comment.