From 211291a0ce1294cfb5c8f3e65845ca9e528afa73 Mon Sep 17 00:00:00 2001 From: sosow0212 Date: Tue, 17 Oct 2023 16:33:09 +0900 Subject: [PATCH] =?UTF-8?q?infra:=20=EB=AC=B4=EC=A4=91=EB=8B=A8=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/backend-deploy.yml | 25 ++++++++--- .../src/main/resources/application-prod.yml | 2 +- backend/src/main/resources/application.yml | 3 +- shellscript/bluegreen.sh | 42 +++++++++++++++++++ 4 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 shellscript/bluegreen.sh diff --git a/.github/workflows/backend-deploy.yml b/.github/workflows/backend-deploy.yml index 7e98c297c..caf53a3af 100644 --- a/.github/workflows/backend-deploy.yml +++ b/.github/workflows/backend-deploy.yml @@ -80,10 +80,18 @@ jobs: script: | sudo docker pull woowacarffeine/backend:latest - sudo docker stop backend || true - sudo docker container prune -f + + if sudo docker ps | grep ":8080"; then + BEFORE_PORT=8080 + NEW_PORT=8081 + NEW_ACTUATOR_PORT=8088 + else + BEFORE_PORT=8081 + NEW_PORT=8080 + NEW_ACTUATOR_PORT=8089 + fi - sudo docker run -d -p 8080:8080 -p 8088:8088 \ + sudo docker run -d -p $NEW_PORT:8080 -p $NEW_ACTUATOR_PORT:8088 \ -e "SPRING_PROFILE=prod" \ -e "ENCRYPT_KEY=${{secrets.JASYPT_KEY}}" \ -e "DATABASE_USERNAME=${{secrets.DATABASE_USERNAME}}" \ @@ -93,5 +101,12 @@ jobs: -e "SLACK_WEBHOOK_URL=${{secrets.SLACK_WEBHOOK_URL}}" \ --name backend \ woowacarffeine/backend:latest - - sudo docker image prune -a -f + + + - name: Execute bluegreen.sh + run: ./bluegreen.sh + env: + NEW_PORT: $NEW_PORT + NEW_ACTUATOR_PORT: $NEW_ACTUATOR_PORT + BEFORE_PORT: $BEFORE_PORT + shell: bash diff --git a/backend/src/main/resources/application-prod.yml b/backend/src/main/resources/application-prod.yml index 36c37cf0b..dba563a69 100644 --- a/backend/src/main/resources/application-prod.yml +++ b/backend/src/main/resources/application-prod.yml @@ -19,7 +19,7 @@ spring: database-platform: org.hibernate.dialect.MySQL5InnoDBDialect initialize-city: - enabled: true + enabled: false initialize-charge: enabled: false diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index d8a575bb6..85b26241c 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -1,5 +1,6 @@ server: port: 8080 + shutdown: graceful servlet: context-path: /api tomcat: @@ -36,7 +37,7 @@ management: endpoints: web: exposure: - include: prometheus + include: prometheus, health jwt: secret: ENC(t6iKCv1Nj+ygRxG2oZCw3l4v9y0ALS2GfQwYIKvO0tRuPzFp4UYRZYfBFdBW+xQl77rOLbfIPJ13tys/tXj3Qw==) diff --git a/shellscript/bluegreen.sh b/shellscript/bluegreen.sh new file mode 100644 index 000000000..abe0b3030 --- /dev/null +++ b/shellscript/bluegreen.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +BEFORE_PORT="$BEFORE_PORT" +NEW_PORT="$NEW_PORT" +NEW_ACTUATOR_PORT="$NEW_ACTUATOR_PORT" + +echo "기존 포트 : $BEFORE_PORT" +echo "새로운 포트: $NEW_PORT" +echo "새로운 ACTUATOR_PORT: $NEW_ACTUATOR_PORT" + +count=0 +for count in {0..20} +do + echo "서버 상태 확인(${count}/20)"; + + UP=$(curl -s http://127.0.0.1:${NEW_ACTUATOR_PORT}/actuator/health-check) + + if [ "${UP}" != '{"status":"up"}' ] + then + sleep 10 + continue + else + break + fi +done + +if [ $count -eq 20 ] +then + echo "새로운 서버 배포를 실패했습니다." + exit 1 +fi + +export BACKEND_PORT=$NEW_PORT +envsubst '${BACKEND_PORT}' < backend.template > backend.conf +sudo mv backend.conf /etc/nginx/conf.d/ +sudo nginx -s reload + +if sudo docker ps | grep ${BEFORE_PORT}; then + sudo docker stop $(docker ps -q --filter "expose=${BEFORE_PORT}") +fi + +sudo docker container prune -f