Skip to content

Commit

Permalink
Merge pull request #99 from LikeLion-12th-SKHU/develop
Browse files Browse the repository at this point in the history
Develop -> main push
  • Loading branch information
ttttkii913 authored Sep 8, 2024
2 parents 6fef25a + d63cc98 commit 9dbbf56
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Java CI/CD with Gradle and AWS CodeDeploy

on:
push:
branches:
- main

permissions:
contents: read

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

- name: Build with Gradle
run: ./gradlew clean build -x test

- name: Prepare artifacts for deployment
run: |
mkdir -p before-deploy
cp scripts/*.sh before-deploy/
cp appspec.yml before-deploy/
cp build/libs/*.jar before-deploy/
cd before-deploy && zip -r before-deploy *
cd ../ && mkdir -p deploy
mv before-deploy/before-deploy.zip deploy/walkmate.zip
- name: Deploy to S3 (GitHub Artifacts)
uses: actions/upload-artifact@v2
with:
name: deploy
path: deploy

- name: AWS ์ž๊ฒฉ์ฆ๋ช… ์„ค์ •
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ap-northeast-2

- name: S3์— ๋ฐฐํฌ
run: aws s3 cp deploy/walkmate.zip s3://likelion-build-hy/walkmate.zip

- name: AWS CodeDeploy๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฐํฌ
run: |
aws deploy create-deployment \
--application-name walkmate-deploy \
--deployment-group-name walkmate-deploy-group \
--s3-location bucket=likelion-build-hy,key=walkmate.zip,bundleType=zip \
--region ap-northeast-2
26 changes: 26 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 0.0 #CodeDeploy ๋ฒ„์ „์„ ์ด์•ผ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์กฐ๊ฑด 0.0์œผ๋กœ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค.
os: linux
files:
- source: / #destination์œผ๋กœ ์ด๋™์‹œํ‚ฌ ํŒŒ์ผ. ์—ฌ๊ธฐ์„œ๋Š” ์ „์ฒด ํŒŒ์ผ์„ ์˜๋ฏธ.
destination: /home/ec2-user/app/zip/ #source์—์„œ ์ง€์ •๋œ ํŒŒ์ผ์„ ๋ฐ›๋Š” ์œ„์น˜
overwrite: yes #๊ธฐ์กด ํŒŒ์ผ๋“ค์„ ๋ฎ์–ด์“ธ์ง€ ์—ฌ๋ถ€

permissions: #CodeDeploy์—์„œ EC2 ์„œ๋ฒ„๋กœ ๋„˜๊ฒจ์ค€ ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ec2-user ๊ถŒํ•œ์„ ๊ฐ–๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user

hooks:
AfterInstall:
- location: stop.sh #์—”์ง„์—‘์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
timeout: 60 #์Šคํฌ๋ฆฝํŠธ 60์ดˆ ์ด์ƒ ์ˆ˜ํ–‰๋˜๋ฉด ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค.
runas: ec2-user #stop.sh๋ฅผ ec2-user ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
ApplicationStart:
- location: start.sh #์—”์ง„์—‘์Šค์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ Port๋กœ ์ƒˆ ๋ฒ„์ „์˜ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
timeout: 60
runas: ec2-user
ValidateService:
- location: health.sh #์ƒˆ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
timeout: 60
runas: ec2-user
39 changes: 39 additions & 0 deletions scripts/health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
source ${ABSDIR}/profile.sh
source ${ABSDIR}/switch.sh

IDLE_PORT=$(find_idle_port)

echo "> Health Check Start!"
echo "> IDLE_PORT: $IDLE_PORT"
echo "> curl -s http://127.0.0.1:$IDLE_PORT/profile"
sleep 10

for RETRY_COUNT in {1..10}
do
RESPONSE=$(curl -s http://127.0.0.1:${IDLE_PORT}/profile)
UP_COUNT=$(echo ${RESPONSE} | grep 'real' | wc -l)

if [ ${UP_COUNT} -ge 1 ] # Nginx์™€ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ํฌํŠธ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ž˜ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค
then # $up_count >= 1 ("real" ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ๊ฒ€์ฆ)
echo "> Health Check ์„ฑ๊ณต"
switch_proxy # ์ž˜ ์‹คํ–‰๋˜์–ด ์žˆ๋‹ค๋ฉด ํ”„๋ก์‹œ ์„ค์ •์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค
break
else
echo "> Health Check์˜ ์‘๋‹ต์„ ์•Œ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ํ˜น์€ ์‹คํ–‰ ์ƒํƒœ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."
echo "> Health Check: ${RESPONSE}"
fi

if [ ${RETRY_COUNT} -eq 10 ]
then
echo "> Health check ์‹คํŒจ. "
echo "> ์—”์ง„์—‘์Šค์— ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๊ณ  ๋ฐฐํฌ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."
exit 1
fi

echo "> Health check ์—ฐ๊ฒฐ ์‹คํŒจ. ์žฌ์‹œ๋„..."
sleep 10
done
35 changes: 35 additions & 0 deletions scripts/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

# ์‰ฌ๊ณ  ์žˆ๋Š” profile ์ฐพ๊ธฐ: real1์ด ์‚ฌ์šฉ ์ค‘์ด๋ฉด real2๊ฐ€ ์‰ฌ๊ณ  ์žˆ๊ณ  ๋ฐ˜๋Œ€๋ฉด real1์ด ์‰ฌ๊ณ  ์žˆ์Œ

function find_idle_profile() {
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://cookie-house.store/profile) # ํ˜„์žฌ Nginx๊ฐ€ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰ ์ค‘์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์‘๋‹ต๊ฐ’์œผ๋กœ ์ƒํƒœ์ฝ”๋“œ๋ฅผ ์ „๋‹ฌ๋ฐ›์Œ

if [ ${RESPONSE_CODE} -ge 400 ] # 400๋ฒˆ๋Œ€ ์ด์ƒ์˜ ์˜ค๋ฅ˜์ผ ๊ฒฝ์šฐ real2๋ฅผ ์‚ฌ์šฉ
then
CURRENT_PROFILE=real2
else
CURRENT_PROFILE=$(curl -s https://hayeongyou.shop/profile)
fi

if [ ${CURRENT_PROFILE} == real1 ]
then
IDLE_PROFILE=real2
else
IDLE_PROFILE=real1
fi

echo "${IDLE_PROFILE}" # bash ์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐ˜ํ™˜ ๊ธฐ๋Šฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— echo๋กœ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—์„œ ๊ทธ ๊ฐ’์„ ์žก์•„์„œ ์‚ฌ์šฉ
}

# ์‰ฌ๊ณ  ์žˆ๋Š” profile์˜ port ์ฐพ๊ธฐ
function find_idle_port() {
IDLE_PROFILE=$(find_idle_profile)

if [ ${IDLE_PROFILE} == real1 ]
then
echo "8081"
else
echo "8082"
fi
}
32 changes: 32 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
source ${ABSDIR}/profile.sh

REPOSITORY=/home/ec2-user/app

echo "> Build ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค."

cp $REPOSITORY/zip/*.jar $REPOSITORY/

echo "> ์ƒˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ"
JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1)

echo "> JAR NAME: $JAR_NAME"

echo "> $JAR_NAME ์— ์‹คํ–‰ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค."

chmod +x $JAR_NAME

IDLE_PROFILE=$(find_idle_profile)

echo "> ์ƒˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ $IDLE_PROFILE ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค."

# ์„ค์ • ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ  active profile์„ ํ†ตํ•ด ๊ตฌ๋™๋  ํฌํŠธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
nohup java -jar \
-Dspring.config.location=$REPOSITORY/config/application.yml,\
$REPOSITORY/config/application-prod.yml,\
$REPOSITORY/config/application-$IDLE_PROFILE.yml \
-Dspring.profiles.active=$IDLE_PROFILE,prod \
$JAR_NAME > $REPOSITORY/nohup.out 2>&1 &
20 changes: 20 additions & 0 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0) # ํ˜„์žฌ stop.sh๊ฐ€ ์†ํ•ด ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

ABSDIR=$(dirname $ABSPATH)
source ${ABSDIR}/profile.sh # ์ผ์ข…์˜ import ๊ตฌ๋ฌธ์œผ๋กœ stop.sh์—์„œ๋„ profile.sh์˜ function์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

IDLE_PORT=$(find_idle_port)

echo "> $IDLE_PORT ์—์„œ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ"
IDLE_PID=$(lsof -ti tcp:${IDLE_PORT})

if [ -z ${IDLE_PID} ]
then
echo "> ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
else
echo "> kill -9 $IDLE_PID"
kill -9 ${IDLE_PID}
sleep 5
fi
16 changes: 16 additions & 0 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
source $ABSDIR/profile.sh

function switch_proxy() {
IDLE_PORT=$(find_idle_port)

echo "> ์ „ํ™˜ํ•  port: $IDLE_PORT"
echo "> Port ์ „ํ™˜"
echo "set \$service_url http://127.0.0.1:${IDLE_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc # ์—”์ง„์—‘์Šค๊ฐ€ ๋ณ€๊ฒฝํ•  ํ”„๋ก์‹œ ์ฃผ์†Œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ service-url.inc๋กœ ๋ฎ์–ด ์”๋‹ˆ๋‹ค.

echo "> ์—”์ง„์—‘์Šค Reload"
sudo systemctl reload nginx
}
2 changes: 2 additions & 0 deletions src/main/resources/application-real1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8081
2 changes: 2 additions & 0 deletions src/main/resources/application-real2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8082
Binary file added src/main/resources/config.tgz
Binary file not shown.

0 comments on commit 9dbbf56

Please sign in to comment.