Skip to content

Commit

Permalink
서버를 무중단 배포할 수 있게 한다. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn113 committed Jan 8, 2025
1 parent 8a01f11 commit 7613a2f
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 213 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/server_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ jobs:
- name: View
run: cat .env

- name: Compose Docker image
- name: Compose Docker image and Update Nginx configuration
env:
DOCKER_APP_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}

run: docker compose -f compose.yml up -d
run: |
chmod +x ./scripts/deploy.sh # 실행 권한 부여
sudo -E ./scripts/deploy.sh # 환경 변수 유지 및 실행
- name: Docker remove unused images
run: docker image prune -af
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
**▷ 개발 기간 : 2024.11 ~ 현재</br>**
**▷ 개발 인원 : 1명**

서버 주소: https://api.fluppy.run
API 명세서: https://api.fluffy.run/docs/index.html
웹 프론트엔드 주소: https://fluppy.run
## 프로젝트 소개

## Server
플러피(Fluffy)는 온라인 시험 문제 제작 및 관리 서비스입니다.

## 프로젝트 개요

- 서버 주소: https://api.fluppy.run
- API 명세서: https://api.fluffy.run/docs/index.html
- 웹 프론트엔드 주소: https://fluppy.run
- 서비스 개발 블로그: https://alstn113.tistory.com/tag/플러피

## 서버

### 기술 스택

Expand All @@ -16,7 +23,7 @@ API 명세서: https://api.fluffy.run/docs/index.html
- redis
- supabase(postgreSQL)

## Web Frontend
## 웹 프론트엔드

### 기술 스택

Expand Down
3 changes: 3 additions & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ dependencies {
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'

// monitoring
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
2 changes: 1 addition & 1 deletion server/compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
redis:
container_name: fluffy-redis
container_name: redis
image: redis:7.4.1
restart: always
ports:
Expand Down
34 changes: 22 additions & 12 deletions server/compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
x-app: &app
image: ${DOCKER_APP_IMAGE}
env_file:
- .env
environment:
TZ: Asia/Seoul
SPRING_PROFILES_ACTIVE: prod
restart: always
depends_on:
- redis

services:
app:
container_name: fluffy-app
image: ${DOCKER_APP_IMAGE}
ports:
- '8080:8080'
env_file:
- .env
environment:
TZ: Asia/Seoul
SPRING_PROFILES_ACTIVE: prod
restart: always
redis:
container_name: fluffy-redis
container_name: redis
image: redis:7.4.1
restart: always
ports:
- '6379:6379'
app-blue:
<<: *app
container_name: app-blue
ports:
- '8080:8080'
app-green:
<<: *app
container_name: app-green
ports:
- '8081:8080'
50 changes: 50 additions & 0 deletions server/scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

NGINX_CONFIG_PATH="/etc/nginx/sites-available/api.fluffy.run"
HOST_HEALTH_CHECK_ENDPOINT="http://localhost:8082/actuator/health"
HEALTH_CHECK_ATTEMPTS=5
HEALTH_CHECK_DELAY=3

health_check() {
for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do
echo "Health check attempt ($i/$HEALTH_CHECK_ATTEMPTS)"
response=$(curl -s -o /dev/null -w "%{http_code}" $HOST_HEALTH_CHECK_ENDPOINT)

if [ $response -eq 200 ]; then
echo "Health check passed"
return 0
fi

sleep $HEALTH_CHECK_DELAY
done

echo "Health check failed"
return 1
}

switch_container() {
local prev_container=$1
local next_container=$2

docker compose -f compose.yml up $next_container -d

if ! health_check; then
echo "Health check failed, rolling back"
docker compose -f compose.yml down $next_container
return 1
fi

sed -i "s/server $prev_container:8080;/server $next_container:8080;/" "$NGINX_CONFIG_PATH"

sudo nginx -s reload
}

IS_GREEN=$(docker container ps | grep app-green)

if [ -z "$IS_GREEN" ]; then
echo "### BLUE >> GREEN ###"
switch_container "app-blue" "app-green"
else
echo "### GREEN >> BLUE ###"
switch_container "app-green" "app-blue"
fi
19 changes: 17 additions & 2 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
spring:
profiles:
active: local

---
spring:
config:
Expand Down Expand Up @@ -61,6 +60,14 @@ logging:
org.springframework.orm.transaction: DEBUG
org.hibernate.orm.jdbc.bind: trace

management:
server:
port: 8082
endpoints:
web:
exposure:
include: health

---
spring:
config:
Expand All @@ -84,7 +91,7 @@ spring:
ddl-auto: validate
data:
redis:
host: fluffy-redis
host: redis
port: 6379

api-host: https://api.fluffy.run
Expand Down Expand Up @@ -118,3 +125,11 @@ logging:
org.springframework.orm.jpa: DEBUG
org.springframework.orm.transaction: DEBUG
org.hibernate.orm.jdbc.bind: trace

management:
server:
port: 8082
endpoints:
web:
exposure:
include: health
Loading

0 comments on commit 7613a2f

Please sign in to comment.