Skip to content

Commit 30691b7

Browse files
committed
Merge pull request #112 from focus-to-level-up/feat/blue-green
FEAT: Change deploy mode about blue-green
2 parents 07f7796 + f298053 commit 30691b7

9 files changed

Lines changed: 145 additions & 12 deletions

File tree

.github/workflows/cicd-template.yml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ jobs:
8282
source: docker-compose-${{ inputs.environment }}.yml
8383
target: /opt/focus-to-level-up
8484

85+
- name: Copy deploy script to EC2
86+
uses: appleboy/scp-action@master
87+
with:
88+
host: ${{ env.EC2_HOST }}
89+
username: ${{ secrets.EC2_USERNAME }}
90+
key: ${{ env.EC2_SSH_KEY }}
91+
port: 22
92+
source: scripts/deploy.sh
93+
target: /opt/focus-to-level-up
94+
8595
- name: Copy DB init scripts to EC2
8696
uses: appleboy/scp-action@master
8797
with:
@@ -113,19 +123,28 @@ jobs:
113123
port: 22
114124
script: |
115125
set -e
116-
126+
127+
# .env 파일 생성
117128
cat > /opt/focus-to-level-up/.env << 'ENV_EOF'
118129
${{ inputs.environment == 'prod' && secrets.ENV_FILE_PROD || secrets.ENV_FILE_DEV }}
119130
ENV_EOF
120-
131+
132+
# 쉘 환경 변수 설정
121133
export IMAGE_TAG="${IMAGE_TAG}"
122134
export DOCKER_USERNAME="${DOCKER_USERNAME}"
123135
export DOCKER_REPO="${DOCKER_REPO}"
124-
136+
137+
# Docker 로그인
125138
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
126-
127-
docker compose -f /opt/focus-to-level-up/docker-compose.yml pull app
128-
docker compose -f /opt/focus-to-level-up/docker-compose.yml up -d
139+
140+
# DB 및 공통 서비스 확인
141+
if [ "${{ inputs.environment }}" != "prod" ]; then
142+
docker compose -f /opt/focus-to-level-up/docker-compose.yml up -d mysql redis
143+
sleep 15
144+
fi
145+
146+
# docker compose -f /opt/focus-to-level-up/docker-compose.yml pull app
147+
# docker compose -f /opt/focus-to-level-up/docker-compose.yml up -d
129148
130149
# Extract DB credentials from .env file
131150
DB_ROOT_PASSWORD=$(grep '^DB_ROOT_PASSWORD=' /opt/focus-to-level-up/.env | cut -d '=' -f2)
@@ -152,5 +171,12 @@ jobs:
152171
docker exec focus-mysql mysql -uroot -p${DB_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON focus_meta_db.* TO '${DB_USERNAME}'@'%';" || true
153172
docker exec focus-mysql mysql -uroot -p${DB_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;" || true
154173
fi
174+
175+
# 6. 배포 스크립트 실행 권한 부여 및 실행
176+
echo "Executing Blue-Green Deployment Script..."
177+
chmod +x /opt/focus-to-level-up/scripts/deploy.sh
178+
179+
# deploy.sh 실행
180+
/opt/focus-to-level-up/scripts/deploy.sh
155181
156182
docker image prune -f

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RUN gradle dependencies --no-daemon
77
COPY src /build/src
88
RUN gradle build -x test --parallel
99

10-
FROM openjdk:17.0.1-slim
10+
FROM amazoncorretto:17
1111
WORKDIR /app
1212

1313
COPY --from=builder /build/build/libs/*-SNAPSHOT.jar ./app.jar

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ dependencies {
2929
implementation 'org.springframework.boot:spring-boot-starter-web'
3030
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
3131
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
32-
implementation 'org.springframework.boot:spring-boot-starter-security'
32+
implementation 'org.springframework.boot:spring-boot-starter-security'
33+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
3334

3435
// database
3536
runtimeOnly 'com.mysql:mysql-connector-j'

docker-compose-dev.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
# docker-compose-dev.yml
12
version: '3.8'
23

34
services:
4-
app:
5+
app-blue:
56
image: ${DOCKER_USERNAME}/${DOCKER_REPO}:${IMAGE_TAG}
6-
container_name: focus-to-levelup-app
7+
container_name: focus-to-levelup-app-blue
78
restart: always
89
ports:
9-
- "8080:8080"
10+
- "8081:8080"
1011
env_file:
1112
- .env
1213
environment:
1314
- TZ=Asia/Seoul
1415
depends_on:
1516
- mysql
17+
- redis
18+
networks:
19+
- focus-network
20+
21+
app-green:
22+
image: ${DOCKER_USERNAME}/${DOCKER_REPO}:${IMAGE_TAG}
23+
container_name: focus-to-levelup-app-green
24+
restart: always
25+
ports:
26+
- "8082:8080"
27+
env_file:
28+
- .env
29+
environment:
30+
- TZ=Asia/Seoul
31+
depends_on:
32+
- mysql
33+
- redis
1634
networks:
1735
- focus-network
1836

docker-compose-prod.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# docker-compose.yml (prod)
12
version: '3.8'
23

34
services:

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# docker-compose.yml (local)
12
version: '3.8'
23

34
services:
@@ -24,8 +25,21 @@ services:
2425
networks:
2526
- focus-network
2627

28+
redis:
29+
image: redis:latest
30+
platform: linux/amd64
31+
container_name: focus-redis
32+
restart: always
33+
ports:
34+
- "6379:6379"
35+
volumes:
36+
- redis-data:/data
37+
networks:
38+
- focus-network
39+
2740
volumes:
2841
mysql-data:
42+
redis-data:
2943

3044
networks:
3145
focus-network:

scripts/deploy.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
3+
# 실행 중 에러 발생 시 종료
4+
set -e
5+
6+
echo "🚀 Blue-Green 배포 시작..."
7+
8+
# 1. 현재 실행 중인 서비스 확인
9+
IS_BLUE=$(docker compose -f /opt/focus-to-level-up/docker-compose.yml ps | grep app-blue || true)
10+
11+
if [ -z "$IS_BLUE" ]; then
12+
echo "### 현재: GREEN (또는 없음) => 배포 타겟: BLUE ###"
13+
TARGET_SERVICE="app-blue"
14+
TARGET_PORT="8081"
15+
STOP_SERVICE="app-green"
16+
else
17+
echo "### 현재: BLUE => 배포 타겟: GREEN ###"
18+
TARGET_SERVICE="app-green"
19+
TARGET_PORT="8082"
20+
STOP_SERVICE="app-blue"
21+
fi
22+
23+
echo "### 2. $TARGET_SERVICE 이미지 Pull 및 실행... ###"
24+
docker compose -f /opt/focus-to-level-up/docker-compose.yml pull $TARGET_SERVICE
25+
docker compose -f /opt/focus-to-level-up/docker-compose.yml up -d $TARGET_SERVICE
26+
27+
echo "### 3. Health Check 시작 (최대 150초 대기) ###"
28+
# 반복 횟수를 10 -> 30으로 늘림 (5초 * 30회 = 150초)
29+
for i in {1..30}; do
30+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:$TARGET_PORT/actuator/health || true)
31+
32+
# 200 OK가 나오면 성공
33+
if [ "$HTTP_CODE" -eq 200 ]; then
34+
echo "✅ Health Check 성공! (HTTP Status: $HTTP_CODE)"
35+
break
36+
fi
37+
38+
# 마지막 시도까지 실패하면
39+
if [ $i -eq 30 ]; then
40+
echo "❌ Health Check 실패... (HTTP Status: $HTTP_CODE)"
41+
echo "🔍 실패 원인 파악을 위해 컨테이너 로그를 출력합니다:"
42+
# 실패 시 컨테이너 로그를 찍어서 왜 안 떴는지 확인
43+
docker compose -f /opt/focus-to-level-up/docker-compose.yml logs --tail=50 $TARGET_SERVICE
44+
45+
echo "배포를 중단하고 새로 띄운 컨테이너를 종료합니다."
46+
docker compose -f /opt/focus-to-level-up/docker-compose.yml stop $TARGET_SERVICE
47+
exit 1
48+
fi
49+
50+
echo "⏳ 대기 중... ($i/30) - Res: $HTTP_CODE"
51+
sleep 5
52+
done
53+
54+
echo "### 4. Nginx 트래픽 전환 ($TARGET_PORT) ###"
55+
# Nginx 설정 변경
56+
echo "proxy_pass http://127.0.0.1:$TARGET_PORT;" | sudo tee /etc/nginx/conf.d/service-env.inc
57+
58+
# Nginx 설정 문법 검사 및 Reload
59+
if sudo nginx -t; then
60+
sudo nginx -s reload
61+
else
62+
echo "❌ Nginx 설정 오류! 배포를 중단합니다."
63+
docker compose -f /opt/focus-to-level-up/docker-compose.yml stop $TARGET_SERVICE
64+
exit 1
65+
fi
66+
67+
echo "### 트래픽 전환 완료. 기존 연결 처리를 위해 10초 대기... ###"
68+
sleep 10
69+
70+
echo "### 5. 이전 버전 ($STOP_SERVICE) 종료 ###"
71+
docker compose -f /opt/focus-to-level-up/docker-compose.yml stop $STOP_SERVICE
72+
73+
echo "✅ 배포 완료!"

src/main/java/com/studioedge/focus_to_levelup_server/global/config/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class SecurityConfig {
2828
private static final String[] PERMIT_URL_ARRAY = {
2929
// health check (for ALB, ECS)
3030
"/health",
31+
"/actuator/health",
3132

3233
// test endpoint
3334
"/test/**",

src/main/resources/application-dev.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ spring:
1616
hibernate:
1717
hbm2ddl:
1818
auto: update
19-
2019
batch:
2120
jdbc:
2221
schema: classpath:org/springframework/batch/core/schema-mysql.sql

0 commit comments

Comments
 (0)