-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from LikeLion-12th-SKHU/develop
Develop -> main push
- Loading branch information
Showing
10 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
server: | ||
port: 8081 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
server: | ||
port: 8082 |
Binary file not shown.