Skip to content

Commit

Permalink
feat(workflow): workflow script 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
junhyeongkim2 committed Oct 14, 2024
1 parent 944011c commit fee4227
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 8 deletions.
8 changes: 4 additions & 4 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 0.0
os: linux

files:
- source: /
- source: /
destination: /home/ubuntu/develop/backend/build/libs
overwrite: yes

Expand All @@ -16,15 +16,15 @@ permissions:

hooks:
ApplicationStop:
- location: scripts/stop_application.sh
- location: scripts/cleaning_old_image.sh
timeout: 300
runas: ubuntu
BeforeInstall:
- location: scripts/before_install.sh
- location: scripts/preparing_new_deployment.sh
timeout: 300
runas: ubuntu
AfterInstall:
- location: scripts/after_install.sh
- location: scripts/build_new_deployment.sh
timeout: 300
runas: ubuntu
ApplicationStart:
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ dependencies {
implementation 'com.google.firebase:firebase-admin:9.3.0'
implementation 'com.fasterxml.jackson.core:jackson-core:2.18.0'

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

compileOnly 'javax.servlet:javax.servlet-api:4.0.1'

implementation 'com.squareup.okhttp3:okhttp:4.10.0'
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ services:
- campride_network
- app-tier



nginx:
image: nginx:latest
restart: unless-stopped
Expand All @@ -132,7 +130,8 @@ services:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
depends_on:
- app
- app-blue
- app-green
networks:
- campride_network
- app-tier
Expand Down
14 changes: 14 additions & 0 deletions scripts/build_new_deployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
echo "Building new version deployment"
cd /home/ubuntu/develop/backend
cp -R /home/ubuntu/develop/backend/build/libs/* build/libs_new/

# 현재 실행 중인 컨테이너 확인
CURRENT_CONTAINER=$(docker ps --filter "name=campride-api-server" --format "{{.Names}}")
if [ "$CURRENT_CONTAINER" == "campride-api-server-blue" ]; then
NEW_CONTAINER="campride-api-server-green"
else
NEW_CONTAINER="campride-api-server-blue"
fi

# 새 버전의 이미지 빌드
docker-compose build $NEW_CONTAINER
3 changes: 3 additions & 0 deletions scripts/cleaning_old_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Cleaning up old deployment artifacts"
cd /home/ubuntu/develop/backend
docker image prune -af
3 changes: 3 additions & 0 deletions scripts/preparing_new_deployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Preparing for new deployment"
cd /home/ubuntu/develop/backend
mkdir -p build/libs_new
40 changes: 40 additions & 0 deletions scripts/start_application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
echo "Starting new version"
cd /home/ubuntu/develop/backend

# 현재 실행 중인 컨테이너 확인
CURRENT_CONTAINER=$(docker ps --filter "name=campride-api-server" --format "{{.Names}}")
if [ "$CURRENT_CONTAINER" == "campride-api-server-blue" ]; then
NEW_CONTAINER="campride-api-server-green"
NEW_PORT=8081
CURRENT_PORT=8080
else
NEW_CONTAINER="campride-api-server-blue"
NEW_PORT=8080
CURRENT_PORT=8081
fi

# 새 컨테이너 시작
docker-compose up -d $NEW_CONTAINER

# 새 컨테이너가 준비될 때까지 대기
echo "Waiting for the new container to be ready..."
for i in {1..30}; do
if curl -s http://localhost:$NEW_PORT/actuator/health | grep -q "UP"; then
echo "New container is ready"
break
fi
if [ $i -eq 30 ]; then
echo "New container failed to start"
exit 1
fi
sleep 10
done

# Nginx 설정 업데이트
NGINX_CONF="/home/ubuntu/develop/backend/data/nginx/nginx.conf"
sed -i "s/proxy_pass http:\/\/$CURRENT_CONTAINER:$CURRENT_PORT/proxy_pass http:\/\/$NEW_CONTAINER:$NEW_PORT/" $NGINX_CONF

# Nginx 설정 리로드
docker-compose exec -T nginx nginx -s reload

echo "Switched traffic to $NEW_CONTAINER on port $NEW_PORT"
32 changes: 32 additions & 0 deletions scripts/validate_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
echo "Validating new deployment"
cd /home/ubuntu/develop/backend

# 현재 활성 컨테이너 확인
NEW_CONTAINER=$(docker ps --filter "name=campride-api-server" --format "{{.Names}}")
OLD_CONTAINER=$(docker ps -a --filter "name=campride-api-server" --filter "status=exited" --format "{{.Names}}")

# 새 버전 상태 확인
for i in {1..5}; do
response=$(curl -sS http://localhost:8080/actuator/health)
if [[ $response == *"UP"* ]]; then
echo "New version is healthy"
# 새 버전의 라이브러리를 메인 디렉토리로 이동
mv build/libs build/libs_old
mv build/libs_new build/libs
# 이전 버전 컨테이너 중지
docker-compose stop $OLD_CONTAINER
exit 0
fi
sleep 10
done

echo "New version is not healthy. Rolling back."

# 롤백
NGINX_CONF="/home/ubuntu/develop/backend/data/nginx/nginx.conf"
sed -i "s/proxy_pass http:\/\/$NEW_CONTAINER:8080/proxy_pass http:\/\/$OLD_CONTAINER:8080/" $NGINX_CONF
docker-compose exec -T nginx nginx -s reload
docker-compose stop $NEW_CONTAINER
docker-compose start $OLD_CONTAINER
echo "Rolled back to $OLD_CONTAINER"
exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ CorsConfigurationSource corsConfigurationSource() {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers("/api/v1/token/refreshtoken", "/api/v1/login**", "/ws/**", "/wss/**", "https://fcm.googleapis.com/v1/projects/campride-87f0d/messages:send");
.requestMatchers("/api/v1/token/refreshtoken", "/api/v1/login**", "/ws/**", "/wss/**",
"https://fcm.googleapis.com/v1/projects/campride-87f0d/messages:send", "/actuator/health");
}

@Bean
Expand Down

0 comments on commit fee4227

Please sign in to comment.