Skip to content

Commit daf817b

Browse files
committed
fix: ci-cd.yml 및 docker-compose.yml 수정
1 parent 08f567b commit daf817b

File tree

4 files changed

+142
-17
lines changed

4 files changed

+142
-17
lines changed

.github/workflows/ci-cd.yml

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,57 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11+
upload-deployment:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Copy deployment files to EC2
17+
uses: appleboy/scp-action@v0.1.4
18+
with:
19+
host: ${{ secrets.EC2_HOST }}
20+
username: ubuntu
21+
key: ${{ secrets.EC2_PRIVATE_KEY }}
22+
source: "deployment"
23+
target: "~/NBE5-7-2-TEAM08"
24+
25+
get-active-color:
26+
runs-on: ubuntu-latest
27+
needs: upload-deployment
28+
outputs:
29+
new_color: ${{ steps.set_color.outputs.NEW_COLOR }}
30+
steps:
31+
- name: SSH로 EC2에서 active_color.txt 열기
32+
id: get_color
33+
uses: appleboy/ssh-action@v1
34+
with:
35+
host: ${{ secrets.EC2_HOST }}
36+
username: ubuntu
37+
key: ${{ secrets.EC2_PRIVATE_KEY }}
38+
script: |
39+
if [ -f ~/NBE5-7-2-TEAM08/deployment/active_color.txt ]; then
40+
cat ~/NBE5-7-2-TEAM08/deployment/active_color.txt
41+
else
42+
echo "blue"
43+
fi
44+
continue-on-error: true
45+
46+
- name: 다음 배포 색상 결정
47+
id: set_color
48+
run: |
49+
echo "CURRENT_COLOR=$(ssh -o StrictHostKeyChecking=no -i <(echo '${{ secrets.EC2_PRIVATE_KEY }}') ubuntu@${{ secrets.EC2_HOST }} 'cat ~/NBE5-7-2-TEAM08/deployment/active_color.txt 2>/dev/null || echo blue')" >> $GITHUB_ENV
50+
51+
if [ "$CURRENT_COLOR" = "blue" ]; then
52+
echo "NEW_COLOR=green" >> "$GITHUB_OUTPUT"
53+
else
54+
echo "NEW_COLOR=blue" >> "$GITHUB_OUTPUT"
55+
fi
56+
1157
backend:
1258
runs-on: ubuntu-latest
59+
needs: get-active-color
60+
env:
61+
NEW_COLOR: ${{ needs.get-active-color.outputs.new_color }}
1362
defaults:
1463
run:
1564
working-directory: backend
@@ -31,12 +80,15 @@ jobs:
3180
password: ${{ secrets.DOCKER_PASSWORD }}
3281

3382
- name: Docker build image (backend)
34-
run: docker build --platform linux/amd64 -t limkanghyun/dev-chat-backend .
83+
run: docker build --platform linux/amd64 -t limkanghyun/dev-chat-backend:${NEW_COLOR} .
3584
- name: Docker push backend image
36-
run: docker push limkanghyun/dev-chat-backend
85+
run: docker push limkanghyun/dev-chat-backend:${NEW_COLOR}
3786

3887
frontend:
3988
runs-on: ubuntu-latest
89+
needs: get-active-color
90+
env:
91+
NEW_COLOR: ${{ needs.get-active-color.outputs.new_color }}
4092
defaults:
4193
run:
4294
working-directory: frontend
@@ -58,9 +110,9 @@ jobs:
58110
password: ${{ secrets.DOCKER_PASSWORD }}
59111

60112
- name: Docker build image(frontend)
61-
run: docker build --platform linux/amd64 -t limkanghyun/dev-chat-frontend .
113+
run: docker build --platform linux/amd64 -t limkanghyun/dev-chat-frontend:${NEW_COLOR} .
62114
- name: Docker push frontend image
63-
run: docker push limkanghyun/dev-chat-frontend
115+
run: docker push limkanghyun/dev-chat-frontend:${NEW_COLOR}
64116

65117
deploy:
66118
runs-on: ubuntu-latest
@@ -70,23 +122,14 @@ jobs:
70122
- name: Checkout repository
71123
uses: actions/checkout@v4
72124

73-
- name: Copy deployment files to EC2
74-
uses: appleboy/scp-action@v0.1.4
75-
with:
76-
host: ${{ secrets.EC2_HOST }}
77-
username: ubuntu
78-
key: ${{ secrets.EC2_PRIVATE_KEY }}
79-
source: "deployment"
80-
target: "~/NBE5-7-2-TEAM08"
81-
82125
- name: Deploy to EC2 via SSH
83126
uses: appleboy/ssh-action@v1
84127
with:
85128
host: ${{ secrets.EC2_HOST }}
86129
username: ubuntu
87130
key: ${{ secrets.EC2_PRIVATE_KEY }}
88131
script: |
89-
cd ~/NBE5-7-2-TEAM08
132+
cd ~/NBE5-7-2-TEAM08/deployment
90133
chmod +x ./deploy.sh
91134
./deploy.sh
92135
# 중단방식

deployment/active_color.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
blue

deployment/deploy.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
# 최신 도커 이미지 내려받기 (pull)
4+
docker-compose pull dev-chat-backend-blue dev-chat-backend-green dev-chat-frontend-blue dev-chat-frontend-green
5+
6+
ACTIVE_COLOR_FILE="$(dirname "$0")/../active_color.txt"
7+
8+
# active_color.txt 없으면 초기값 blue 생성
9+
if [ ! -f "$ACTIVE_COLOR_FILE" ]; then
10+
echo "blue" > "$ACTIVE_COLOR_FILE"
11+
fi
12+
13+
ACTIVE_COLOR=$(cat "$ACTIVE_COLOR_FILE")
14+
15+
if [ "$ACTIVE_COLOR" == "blue" ]; then
16+
NEW_COLOR="green"
17+
NEW_BACKEND_PORT=8082
18+
NEW_FRONTEND_PORT=3001
19+
else
20+
NEW_COLOR="blue"
21+
NEW_BACKEND_PORT=8081
22+
NEW_FRONTEND_PORT=3000
23+
fi
24+
25+
# 새롭게 띄울 컨테이너 실행
26+
docker-compose up -d dev-chat-backend-$NEW_COLOR
27+
docker-compose up -d dev-chat-frontend-$NEW_COLOR
28+
29+
# 새롭게 띄운 dev-chat-backend-$NEW_COLOR 컨테이너의 헬스체크 (정상작동 확인)
30+
echo "새로운 컨테이너 헬스체크 중..."
31+
for i in {1..30}; do
32+
STATUS=$(curl -s http://localhost:$NEW_BACKEND_PORT/health | grep "OK")
33+
if [ "$STATUS" != "" ]; then
34+
echo "[$NEW_COLOR] 컨테이너 헬스 체크 통과!"
35+
break;
36+
fi
37+
echo -n "."
38+
sleep 1
39+
done
40+
41+
if [ "$STATUS" == "" ]; then
42+
echo " [$NEW_COLOR] 컨테이너 헬스 체크 실패! 롤백합니다..."
43+
docker-compose stop dev-chat-backend-$NEW_COLOR
44+
docker-compose stop dev-chat-frontend-$NEW_COLOR
45+
exit 1
46+
fi
47+
48+
# 프론트엔드 헬스체크
49+
echo "새로운 프론트엔드 컨테이너 헬스체크 중..."
50+
for i in {1..30}; do
51+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$NEW_FRONTEND_PORT/index.html)
52+
if [ "$HTTP_CODE" == "200" ]; then
53+
echo "[$NEW_COLOR] 프론트엔드 헬스 체크 통과!"
54+
break
55+
fi
56+
echo -n "."
57+
sleep 1
58+
done
59+
60+
if [ "$HTTP_CODE" != "200" ]; then
61+
echo "[$NEW_COLOR] 프론트엔드 헬스 체크 실패! 롤백합니다..."
62+
docker-compose stop dev-chat-backend-$NEW_COLOR
63+
docker-compose stop dev-chat-frontend-$NEW_COLOR
64+
exit 1
65+
fi
66+
67+
# nginx upstream 설정 파일 변경 (green -> blue / blue -> green)
68+
sed -i "s/dev-chat-backend-$ACTIVE_COLOR/dev-chat-backend-$NEW_COLOR/g" ./nginx.conf
69+
sed -i "s/dev-chat-frontend-$ACTIVE_COLOR/dev-chat-frontend-$NEW_COLOR/g" ./nginx.conf
70+
71+
# nginx reload
72+
docker exec nginx_proxy nginx -s reload
73+
74+
# 기존 컨테이너 종료
75+
docker-compose stop dev-chat-backend-$ACTIVE_COLOR
76+
docker-compose stop dev-chat-frontend-$ACTIVE_COLOR
77+
78+
# active_color.txt 업데이트
79+
echo $NEW_COLOR > "$(dirname "$0")/../active_color.txt"
80+
81+
echo "배포 완료: 현재 활성화된 서비스는 $NEW_COLOR 입니다."

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22

33
dev-chat-backend-blue:
4-
image: limkanghyun/dev-chat-backend
4+
image: limkanghyun/dev-chat-backend:blue
55
container_name: dev-chat-backend-blue
66
ports:
77
- "8081:8080" # 내부는 8080이지만 외부는 8082
@@ -15,7 +15,7 @@ services:
1515
- dev-chat-network
1616

1717
dev-chat-backend-green:
18-
image: limkanghyun/dev-chat-backend
18+
image: limkanghyun/dev-chat-backend:green
1919
container_name: dev-chat-backend-green
2020
ports:
2121
- "8082:8080" # 내부는 8080이지만 외부는 8082
@@ -29,7 +29,7 @@ services:
2929
- dev-chat-network
3030

3131
dev-chat-frontend-blue:
32-
image: limkanghyun/dev-chat-frontend
32+
image: limkanghyun/dev-chat-frontend:blue
3333
container_name: dev-chat-frontend-blue
3434
ports:
3535
- "3000:80"

0 commit comments

Comments
 (0)