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