Skip to content

Commit

Permalink
Merge pull request #80 from capstone-five-ai/feat/blue-green
Browse files Browse the repository at this point in the history
feat: Nginx 도입 및 블루/그린 무중단 배포 적용
  • Loading branch information
yujamint authored Jul 25, 2024
2 parents 18e836b + 9bdc485 commit 1f8282b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
55 changes: 49 additions & 6 deletions .github/workflows/deploy-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,53 @@ jobs:
host: ${{ secrets.PROD_SERVER_HOST }}
username: ${{ secrets.PROD_SERVER_USERNAME }}
key: ${{ secrets.PROD_SERVER_PEM_KEY }}
envs: GITHUB_SHA
envs: GITHUB_SHA, DOCKERHUB_USERNAME
script: |
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker pull ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server:prod
sudo docker run -d -p 8080:8080 --name qtudy-server-container ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server:prod
sudo docker image prune -f
# 현재 활성화된 Nginx 설정 파일 확인
CURRENT_CONF=$(sudo readlink /etc/nginx/sites-enabled/current)
echo "Current Nginx configuration: $CURRENT_CONF"
if [[ "$CURRENT_CONF" == *"blue" ]]; then
NEW_CONF="/etc/nginx/sites-available/green"
OLD_CONTAINER="blue-container"
NEW_CONTAINER="green-container"
NEW_PORT=8081
else
NEW_CONF="/etc/nginx/sites-available/blue"
OLD_CONTAINER="green-container"
NEW_CONTAINER="blue-container"
NEW_PORT=8080
fi
echo "New Nginx configuration: $NEW_CONF"
echo "Old container: $OLD_CONTAINER"
echo "New container: $NEW_CONTAINER"
echo "New port: $NEW_PORT"
# 새 Docker 이미지 가져오기
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod
# 새로운 컨테이너 실행
sudo docker run -d -p $NEW_PORT:8080 --name $NEW_CONTAINER ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod
# 새로운 컨테이너가 준비될 때까지 대기
echo "Waiting for the new container to be ready..."
sleep 10
# 헬스 체크를 통해 새 컨테이너가 정상적으로 실행 중인지 확인
until [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:$NEW_PORT/actuator/health)" == "200" ]]; do
echo "Waiting for the new container to respond with HTTP 200..."
sleep 2
done
echo "New container is ready. Switching Nginx configuration."
# Nginx 설정 파일 교체 및 재시작
sudo ln -sf $NEW_CONF /etc/nginx/sites-enabled/current
sudo systemctl reload nginx
# 이전 컨테이너 중지 및 제거
sudo docker stop $OLD_CONTAINER
sudo docker rm $OLD_CONTAINER
# 사용하지 않는 Docker 이미지 정리
sudo docker image prune -f
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {
implementation 'org.mapstruct:mapstruct:1.5.3.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'

// Spring Boot Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// GPT 스크립트 테스트 편의성을 위해 추가
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation 'com.theokanning.openai-gpt3-java:service:0.18.2'
Expand Down

0 comments on commit 1f8282b

Please sign in to comment.