From d1e282460b74730f9e3f2b8bf527ffb155d0ee8b Mon Sep 17 00:00:00 2001 From: kimyechan Date: Sun, 30 Jun 2024 18:50:02 +0900 Subject: [PATCH 1/9] =?UTF-8?q?Test:=20=EB=B8=94=EB=A3=A8-=EA=B7=B8?= =?UTF-8?q?=EB=A6=B0=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index 825c94f..23f27c9 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -2,7 +2,7 @@ name: deploy on prod server on: push: - branches: [main] + branches: [feat/blue-green] jobs: deploy: @@ -53,10 +53,9 @@ jobs: host: ${{ secrets.PROD_SERVER_HOST }} username: ${{ secrets.PROD_SERVER_USERNAME }} key: ${{ secrets.PROD_SERVER_PEM_KEY }} - envs: GITHUB_SHA + envs: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} 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 + chmod +x /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh + /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh \ No newline at end of file From 58410aa170c115158edb007743dd81b1f94ce8d5 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Sun, 30 Jun 2024 18:55:07 +0900 Subject: [PATCH 2/9] =?UTF-8?q?Test:=20=EB=B8=94=EB=A3=A8-=EA=B7=B8?= =?UTF-8?q?=EB=A6=B0=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index 23f27c9..c83cf95 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -53,9 +53,9 @@ jobs: host: ${{ secrets.PROD_SERVER_HOST }} username: ${{ secrets.PROD_SERVER_USERNAME }} key: ${{ secrets.PROD_SERVER_PEM_KEY }} - envs: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - script: | - chmod +x /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh - /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh \ No newline at end of file + env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + script: | + chmod +x /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh + /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh \ No newline at end of file From a19df3c76b9af256af5651b446fee2a5b6ea2ab9 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Sun, 30 Jun 2024 18:57:09 +0900 Subject: [PATCH 3/9] =?UTF-8?q?Test:=20=EB=B8=94=EB=A3=A8-=EA=B7=B8?= =?UTF-8?q?=EB=A6=B0=20=EB=AC=B4=EC=A4=91=EB=8B=A8=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 Test: 블루-그린 무중단 배포 테스트 --- .github/workflows/deploy-prod.yaml | 61 ++++++++++++++++++++++++++---- scripts/zero-downtime-deploy.sh | 29 ++++++++++++++ 2 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 scripts/zero-downtime-deploy.sh diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index c83cf95..efb38b8 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -47,15 +47,62 @@ jobs: push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod - - name: EC2 서버에 배포 + - name: EC2 서버에 배포# uses: appleboy/ssh-action@master with: host: ${{ secrets.PROD_SERVER_HOST }} username: ${{ secrets.PROD_SERVER_USERNAME }} key: ${{ secrets.PROD_SERVER_PEM_KEY }} - env: - DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - script: | - chmod +x /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh - /home/${{ secrets.PROD_SERVER_USERNAME }}/scripts/zero-downtime-deploy.sh \ No newline at end of file + 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)" == "200" || "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:$NEW_PORT)" == "404" ]]; do + echo "Waiting for the new container to respond with HTTP 200 or 404..." + 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 \ No newline at end of file diff --git a/scripts/zero-downtime-deploy.sh b/scripts/zero-downtime-deploy.sh new file mode 100644 index 0000000..3c942ca --- /dev/null +++ b/scripts/zero-downtime-deploy.sh @@ -0,0 +1,29 @@ +set -e + +# DockerHub 로그인 +echo $DOCKERHUB_TOKEN | sudo docker login -u $DOCKERHUB_USERNAME --password-stdin + +# 새로운 버전 배포 (qtudy-blue) +sudo docker pull $DOCKERHUB_USERNAME/qtudy-server:prod +sudo docker run -d -p 8002:8080 --name qtudy-server-blue $DOCKERHUB_USERNAME/qtudy-server:prod +sudo mv /etc/nginx/conf.d/qtudy-green.conf /etc/nginx/conf.d/qtudy-green.conf.backup +sudo mv /etc/nginx/conf.d/qtudy-blue.conf /etc/nginx/conf.d/qtudy-green.conf +sudo systemctl restart nginx + +# 새로운 버전 확인 +sleep 30 +if curl -f http://127.0.0.1; then + echo "새 버전이 정상적으로 실행되고 있습니다. 이전 컨테이너를 정리합니다." + sudo docker stop qtudy-server-container + sudo docker rm qtudy-server-container + sudo docker image prune -f +else + echo "새 버전 실행에 실패했습니다. 롤백을 진행합니다." + sudo mv /etc/nginx/conf.d/qtudy-green.conf /etc/nginx/conf.d/qtudy-blue.conf + sudo mv /etc/nginx/conf.d/qtudy-green.conf.backup /etc/nginx/conf.d/qtudy-green.conf + sudo systemctl restart nginx + sudo docker stop qtudy-server-blue + sudo docker rm qtudy-server-blue + sudo docker image prune -f + exit 1 +fi \ No newline at end of file From 53a5ab2496bc5e91f5b0e34fd4a9ab1b4327f446 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 1 Jul 2024 12:42:26 +0900 Subject: [PATCH 4/9] =?UTF-8?q?Feat:=20Actuator=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=97=AC=EC=8A=A4?= =?UTF-8?q?=EC=B2=B4=ED=81=AC=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 2 +- build.gradle | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index efb38b8..eab510f 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -86,7 +86,7 @@ jobs: sleep 10 # 헬스 체크를 통해 새 컨테이너가 정상적으로 실행 중인지 확인 - until [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:$NEW_PORT)" == "200" || "$(curl -s -o /dev/null -w ''%{http_code}'' http://localhost:$NEW_PORT)" == "404" ]]; do + 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 or 404..." sleep 2 done diff --git a/build.gradle b/build.gradle index 90dbf9e..0a2a3d4 100644 --- a/build.gradle +++ b/build.gradle @@ -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' From 8702ce44ccfe060f267e7ce724136f191008e7b4 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 1 Jul 2024 12:46:29 +0900 Subject: [PATCH 5/9] =?UTF-8?q?Fix:=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8?= =?UTF-8?q?=20=EC=98=A4=ED=83=80=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index eab510f..6fcd3c0 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -86,8 +86,8 @@ jobs: 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 or 404..." + 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 From 098dd59930acfacb06471fbe9882ad6b2c6c6f99 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 1 Jul 2024 12:51:01 +0900 Subject: [PATCH 6/9] =?UTF-8?q?Fix:=20=ED=8F=AC=ED=8A=B8=20=EC=B6=A9?= =?UTF-8?q?=EB=8F=8C=20=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index 6fcd3c0..88da5f1 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -47,7 +47,7 @@ jobs: push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod - - name: EC2 서버에 배포# + - name: EC2 서버에 배포 uses: appleboy/ssh-action@master with: host: ${{ secrets.PROD_SERVER_HOST }} From a93cca7f8c77cfc49cb9f5f3334be1a93eebd6de Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 1 Jul 2024 13:33:14 +0900 Subject: [PATCH 7/9] =?UTF-8?q?Fix:=20=EB=B8=8C=EB=9E=9C=EC=B9=98=20main?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index 88da5f1..5227943 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -2,7 +2,7 @@ name: deploy on prod server on: push: - branches: [feat/blue-green] + branches: [main] jobs: deploy: From e2f59498ef1cffc4513c7331fd191186bdd20328 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 1 Jul 2024 13:51:23 +0900 Subject: [PATCH 8/9] =?UTF-8?q?Remove:=20=EC=8A=A4=ED=81=AC=EB=A6=BD?= =?UTF-8?q?=ED=8A=B8=20=EC=89=98=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/zero-downtime-deploy.sh | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 scripts/zero-downtime-deploy.sh diff --git a/scripts/zero-downtime-deploy.sh b/scripts/zero-downtime-deploy.sh deleted file mode 100644 index 3c942ca..0000000 --- a/scripts/zero-downtime-deploy.sh +++ /dev/null @@ -1,29 +0,0 @@ -set -e - -# DockerHub 로그인 -echo $DOCKERHUB_TOKEN | sudo docker login -u $DOCKERHUB_USERNAME --password-stdin - -# 새로운 버전 배포 (qtudy-blue) -sudo docker pull $DOCKERHUB_USERNAME/qtudy-server:prod -sudo docker run -d -p 8002:8080 --name qtudy-server-blue $DOCKERHUB_USERNAME/qtudy-server:prod -sudo mv /etc/nginx/conf.d/qtudy-green.conf /etc/nginx/conf.d/qtudy-green.conf.backup -sudo mv /etc/nginx/conf.d/qtudy-blue.conf /etc/nginx/conf.d/qtudy-green.conf -sudo systemctl restart nginx - -# 새로운 버전 확인 -sleep 30 -if curl -f http://127.0.0.1; then - echo "새 버전이 정상적으로 실행되고 있습니다. 이전 컨테이너를 정리합니다." - sudo docker stop qtudy-server-container - sudo docker rm qtudy-server-container - sudo docker image prune -f -else - echo "새 버전 실행에 실패했습니다. 롤백을 진행합니다." - sudo mv /etc/nginx/conf.d/qtudy-green.conf /etc/nginx/conf.d/qtudy-blue.conf - sudo mv /etc/nginx/conf.d/qtudy-green.conf.backup /etc/nginx/conf.d/qtudy-green.conf - sudo systemctl restart nginx - sudo docker stop qtudy-server-blue - sudo docker rm qtudy-server-blue - sudo docker image prune -f - exit 1 -fi \ No newline at end of file From 9bdc485e167af8321467378768fe1b8df704dcc6 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Tue, 2 Jul 2024 11:44:03 +0900 Subject: [PATCH 9/9] =?UTF-8?q?Chore:=20Docker=20ps=20=EA=B5=AC=EB=AC=B8?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-prod.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/deploy-prod.yaml b/.github/workflows/deploy-prod.yaml index 5227943..db5872e 100644 --- a/.github/workflows/deploy-prod.yaml +++ b/.github/workflows/deploy-prod.yaml @@ -101,8 +101,5 @@ jobs: sudo docker stop $OLD_CONTAINER sudo docker rm $OLD_CONTAINER - # 실행 중인 Docker 컨테이너 확인 - sudo docker ps - # 사용하지 않는 Docker 이미지 정리 sudo docker image prune -f \ No newline at end of file