Fix: 포트 충돌 컨테이너 해결 #30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: deploy on prod server | |
on: | |
push: | |
branches: [feat/blue-green] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 체크아웃 | |
uses: actions/checkout@v4 | |
with: | |
submodules: true | |
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
- name: 서브모듈 업데이트 | |
run: | | |
git submodule update --remote | |
- name: JDK 11 설치 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'corretto' | |
java-version: '11' | |
cache: 'gradle' | |
- name: Gradle에 실행 권한 부여 | |
run: chmod +x gradlew | |
- name: 빌드 | |
run: ./gradlew build -x test | |
- name: DockerHub 로그인 | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Docker 이미지 빌드 & DockerHub에 Push | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./docker/Dockerfile-prod | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod | |
- name: EC2 서버에 배포 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.PROD_SERVER_HOST }} | |
username: ${{ secrets.PROD_SERVER_USERNAME }} | |
key: ${{ secrets.PROD_SERVER_PEM_KEY }} | |
envs: GITHUB_SHA, DOCKERHUB_USERNAME | |
script: | | |
# 현재 활성화된 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 ps | |
# 사용하지 않는 Docker 이미지 정리 | |
sudo docker image prune -f |