Skip to content

Commit

Permalink
#114 - Github Actions CI & CD 프로필별 분리
Browse files Browse the repository at this point in the history
#114 - Github Actions CI & CD 프로필별 분리
  • Loading branch information
sasca37 authored Feb 1, 2024
2 parents ed4ae2e + ac3447c commit cae6651
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 63 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/github-actions-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Java CI with Gradle

on:
push:
branches: [ "deploy/#114" ]

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

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- name: make application-dev.yml
run: |
cd ./src/main/resources
touch ./application-dev.yml
echo "${{ secrets.APPLICATION_DEV }}" > ./application-dev.yml
touch ./application.yml
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
cd ../../test/resources
touch ./application.yml
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
- name: create-json
id: create-json
uses: jsdaniell/[email protected]
with:
name: "dodal-fcm.json"
json: ${{ secrets.FCM_JSON }}
dir: src/main/resources

- name: Build with Gradle
run: |
./gradlew cleanQuerydslSourceDir
./gradlew build
# 전송할 JAR 파일을 담을 디렉토리 생성
- name: Make Directory for deliver
run: mkdir ./deploy

- name: Copy Jar
run: |
chmod +x ./build/libs/*.jar
cp ./build/libs/*.jar ./deploy/
cp ./appspec-dev.yml ./deploy/appspec.yml
mkdir -p ./deploy/scripts
cp ./scripts/deploy-dev.sh ./deploy/scripts/deploy-dev.sh
# 압축파일 형태로 전달
- name: Make zip fileㅊ
run: |
cd ./deploy
zip -r ./deploy-dev.zip .
# S3 Bucket으로 copy
- name: Deliver to AWS S3
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_S3_ACCESS_KEY_DEV }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_S3_SECRET_KEY_DEV }}
run: |
aws s3 cp \
--region ap-northeast-2 \
--acl private ./deploy/deploy-dev.zip s3://${{ env.S3_BUCKET_NAME }}/deploy/deploy-dev.zip
# CodeDeploy
- name: Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_CODE_DEPLOY_ACCESS_KEY_DEV }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_CODE_DEPLOY_SECRET_KEY_DEV }}
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
90 changes: 90 additions & 0 deletions .github/workflows/github-actions-prd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Java CI with Gradle

on:
push:
branches: [ "main" ]

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

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- name: make application-prd.yml
run: |
cd ./src/main/resources
touch ./application-prd.yml
echo "${{ secrets.APPLICATION_PRD }}" > ./application-prd.yml
touch ./application.yml
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
cd ../../test/resources
touch ./application.yml
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
- name: create-json
id: create-json
uses: jsdaniell/[email protected]
with:
name: "dodal-fcm.json"
json: ${{ secrets.FCM_JSON }}
dir: src/main/resources

- name: Build with Gradle
run: |
./gradlew cleanQuerydslSourceDir
./gradlew build
# 전송할 JAR 파일을 담을 디렉토리 생성
- name: Make Directory for deliver
run: mkdir ./deploy

- name: Copy Jar
run: |
chmod +x ./build/libs/*.jar
cp ./build/libs/*.jar ./deploy/
cp ./appspec-prd.yml ./deploy/appspec.yml
mkdir -p ./deploy/scripts
cp ./scripts/deploy-prd.sh ./deploy/scripts/deploy-prd.sh
# 압축파일 형태로 전달
- name: Make zip file
run: |
cd ./deploy
zip -r ./deploy-prd.zip .
# S3 Bucket으로 copy
- name: Deliver to AWS S3
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_S3_ACCESS_KEY_PRD }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_S3_SECRET_KEY_PRD }}
run: |
aws s3 cp \
--region ap-northeast-2 \
--acl private ./deploy/deploy-prd.zip s3://${{ env.S3_BUCKET_NAME }}/deploy/deploy-prd.zip
# CodeDeploy
- name: Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_CODE_DEPLOY_ACCESS_KEY_PRD }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_CODE_DEPLOY_SECRET_KEY_PRD }}
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-prd.zip \
--region ap-northeast-2
62 changes: 0 additions & 62 deletions .github/workflows/github-actions.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ FodyWeavers.xsd

# ignore dev & prod
**/application-dev.yml
**/application-prod.yml
**/application-prd.yml
**/docker-compose-local.yml
**/docker-compose-dev.yml
**/application.yml
Expand Down
16 changes: 16 additions & 0 deletions appspec-dev.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-dev.sh
timeout: 300
runas: ec2-user
16 changes: 16 additions & 0 deletions appspec-prd.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-prd.sh
timeout: 300
runas: ec2-user
41 changes: 41 additions & 0 deletions scripts/deploy-dev.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/dodal-app.log 2> $SERVICE_PATH/dodal-app.log &
41 changes: 41 additions & 0 deletions scripts/deploy-prd.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=prd $DEPLOY_JAR >> /home/ec2-user/dodal-app.log 2> $SERVICE_PATH/dodal-app.log &

0 comments on commit cae6651

Please sign in to comment.