-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] CI/CD
- Loading branch information
Showing
4 changed files
with
196 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,89 @@ | ||
# 워크플로우의 이름 지정 | ||
name: CD | ||
|
||
# 해당 workflow가 언제 실행될 것인지에 대한 트리거를 지정 | ||
on: | ||
push: | ||
branches: [ develop ] # develop branch로 push 될 때 실행됩니다. | ||
|
||
env: | ||
S3_BUCKET_NAME: cecd1921-bucket | ||
|
||
jobs: | ||
build: | ||
name: Code deployment | ||
|
||
# 실행 환경 | ||
runs-on: ubuntu-22.04 | ||
|
||
steps: | ||
|
||
- name: checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: 'corretto' | ||
|
||
- name: Change to server directory # 'server' 디렉토리로 이동 | ||
run: cd server | ||
shell: bash | ||
|
||
- name: make application.yml | ||
run: | | ||
## create application.yml | ||
mkdir ./src/main/resources | ||
cd ./src/main/resources | ||
touch ./application.yml | ||
echo "${{ secrets.CD_APPLICATION }}" >> ./application.yml | ||
cat ./application.yml | ||
shell: bash | ||
|
||
- name: Grant execute permission for gradlew | ||
run: chmod +x gradlew | ||
|
||
- name: Build with Gradle # 실제 application build(-x 옵션을 통해 test는 제외) | ||
run: ./gradlew build -x test | ||
|
||
- name: Make Directory | ||
run: mkdir -p deploy | ||
|
||
- name: Copy Jar | ||
run: cp ./build/libs/*.jar ./deploy | ||
|
||
- name: Copy appspec.yml | ||
run: cp appspec.yml ./deploy | ||
|
||
- name: Copy script | ||
run: cp ./scripts/*.sh ./deploy | ||
|
||
- name: Make zip file | ||
run: zip -r ./cecd-1921.zip ./deploy | ||
shell: bash | ||
|
||
- name: Configure AWS credentials | ||
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: Upload to S3 | ||
run: aws s3 cp --region ap-northeast-2 ./cecd-1921.zip s3://$S3_BUCKET_NAME/ | ||
|
||
- name: Deploy | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} | ||
run: | ||
aws deploy create-deployment | ||
--application-name cecd-1921-deploy | ||
--deployment-group-name cecd-1921-deploy | ||
--file-exists-behavior OVERWRITE | ||
--s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=cecd-1921.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 |
---|---|---|
|
@@ -37,6 +37,7 @@ application.yml | |
### VS Code ### | ||
.vscode/ | ||
*.yml | ||
!appspec.yml | ||
*.sql | ||
#/src/main/resources/*.json | ||
#*.b64 |
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,19 @@ | ||
version: 0.0 | ||
os: linux | ||
|
||
files: | ||
- source: / | ||
destination: /home/ubuntu/app | ||
overwrite: yes | ||
|
||
permissions: | ||
- object: / | ||
pattern: "**" | ||
owner: ubuntu | ||
group: ubuntu | ||
|
||
hooks: | ||
AfterInstall: | ||
- location: deploy.sh | ||
timeout: 60 | ||
runas: ubuntu |
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,87 @@ | ||
#!/bin/bash | ||
BUILD_PATH=$(ls /home/ubuntu/app/server-0.0.1-SNAPSHOT.jar) | ||
JAR_NAME=$(basename $BUILD_PATH) | ||
echo "> build 파일명: $JAR_NAME" | ||
|
||
echo "> build 파일 복사" | ||
DEPLOY_PATH=/home/ubuntu/app/ | ||
cp $BUILD_PATH $DEPLOY_PATH | ||
|
||
echo "> 현재 구동중인 Port 확인" | ||
BLUE_PROFILE=$(curl -s http://localhost/profile) | ||
echo "> $BLUE_PROFILE" | ||
|
||
# 쉬고 있는 set 찾기: set1이 사용중이면 set2가 쉬고 있고, 반대면 set1이 쉬고 있음 | ||
if [ $BLUE_PROFILE == set1 ] | ||
then | ||
GREEN_PROFILE=set2 | ||
GREEN_PORT=8082 | ||
elif [ $BLUE_PROFILE == set2 ] | ||
then | ||
GREEN_PROFILE=set1 | ||
GREEN_PORT=8081 | ||
else | ||
echo "> 일치하는 Profile이 없습니다. Profile: $BLUE_PROFILE" | ||
echo "> set1을 할당합니다. IDLE_PROFILE: set1" | ||
GREEN_PROFILE=set1 | ||
GREEN_PORT=8081 | ||
fi | ||
|
||
echo "> application.jar 교체" | ||
GREEN_APPLICATION=$GREEN_PROFILE-server.jar | ||
GREEN_APPLICATION_PATH=$DEPLOY_PATH$GREEN_APPLICATION | ||
|
||
ln -Tfs $DEPLOY_PATH$JAR_NAME $GREEN_APPLICATION_PATH | ||
|
||
echo "----------------------------------------------------------------------" | ||
echo "> $GREEN_PROFILE 배포" | ||
nohup java -jar -Duser.timezone=Asia/Seoul -Dspring.profiles.active=$GREEN_PROFILE $GREEN_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & | ||
|
||
echo "----------------------------------------------------------------------" | ||
|
||
echo "> nginx 포트 스위칭" | ||
echo "set \$service_url http://127.0.0.1:${GREEN_PORT};" | sudo tee /etc/nginx/conf.d/service-url.inc | ||
sudo nginx -s reload | ||
|
||
echo "> $GREEN_PROFILE 10초 후 Health check 시작" | ||
echo "> curl -s http://localhost:$GREEN_PORT/actuator/health " | ||
sleep 10 | ||
|
||
for retry_count in {1..10} | ||
do | ||
response=$(curl -s http://localhost:$GREEN_PORT/actuator/health) | ||
up_count=$(echo $response | grep 'UP' | wc -l) | ||
|
||
if [ $up_count -ge 1 ] | ||
then | ||
echo "> Health check 성공" | ||
|
||
break | ||
else | ||
echo "> Health check의 응답을 알 수 없거나 혹은 status가 UP이 아닙니다." | ||
echo "> Health check: ${response}" | ||
fi | ||
|
||
if [ $retry_count -eq 10 ] | ||
then | ||
echo "> Health check 실패. " | ||
echo "> Nginx에 연결하지 않고 배포를 종료합니다." | ||
exit 1 | ||
fi | ||
|
||
echo "> Health check 연결 실패. 재시도..." | ||
sleep 10 | ||
done | ||
|
||
echo "> $BLUE_PROFILE 에서 구동중인 애플리케이션 pid 확인" | ||
BLUE_PID=$(pgrep -f $BLUE_PROFILE-server.jar) | ||
|
||
if [ -z $BLUE_PID ] | ||
then | ||
echo "> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다." | ||
else | ||
echo "> 기존 ${BLUE_PROFILE} 서버 중단" | ||
echo "> kill -15 $BLUE_PID" | ||
kill -15 $BLUE_PID | ||
sleep 5 | ||
fi |