Test: 블루-그린 무중단 배포 테스트 #19
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/active.conf) | |
echo "Current Nginx configuration: $CURRENT_CONF" | |
if [[ "$CURRENT_CONF" == *"nginx.blue.conf" ]]; then | |
NEW_CONF="/etc/nginx/nginx.green.conf" | |
OLD_CONTAINER="blue-container" | |
NEW_CONTAINER="green-container" | |
NEW_PORT=8001 | |
else | |
NEW_CONF="/etc/nginx/nginx.blue.conf" | |
OLD_CONTAINER="green-container" | |
NEW_CONTAINER="blue-container" | |
NEW_PORT=8000 | |
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 | |
sudo docker run -d -p $NEW_PORT:8080 --network="host" --name $NEW_CONTAINER ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server | |
# Nginx 설정 파일 교체 및 재시작 | |
sudo ln -sf $NEW_CONF /etc/nginx/active.conf | |
sudo systemctl reload nginx | |
# 이전 컨테이너 중지 및 제거 | |
for OLD_CONTAINER_NAME in qtudy-server-container $OLD_CONTAINER; do | |
if sudo docker ps -a --format '{{.Names}}' | grep -Eq "^${OLD_CONTAINER_NAME}\$"; then | |
sudo docker stop $OLD_CONTAINER_NAME | |
sudo docker rm $OLD_CONTAINER_NAME | |
fi | |
done | |
# 실행 중인 Docker 컨테이너 확인 | |
sudo docker ps | |
# 사용하지 않는 Docker 이미지 정리 | |
sudo docker image prune -f |