Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 운영 서버 배포 시 개발 서버 프로필로 활성화되는 문제 해결 #943

Merged
merged 8 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions .github/workflows/backend-cd-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ on:

env:
DOCKERHUB_REPOSITORY: ody-official
DOCKER_CONTAINER_NAME: ody-prod-app

jobs:
build-and-push:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release-be/')
if: github.event.pull_request.merged == true && (startsWith(github.event.pull_request.head.ref, 'release-be/') || startsWith(github.event.pull_request.head.ref, 'hotfix-be/'))

runs-on: ubuntu-latest
env:
TZ: 'Asia/Seoul'
Expand Down Expand Up @@ -67,40 +67,36 @@ jobs:

- name: Back Up Image For Rollback
run: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest || true
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-latest || true
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-latest ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-previous || true
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-previous || true

- name: Docker Image Build And Push
run: docker build --platform linux/arm64 -t ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod -f Dockerfile . --build-arg JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} --push
run: docker build --platform linux/arm64 -t ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod_v2 -f Dockerfile . --push

pull-and-deploy:
needs: build-and-push

runs-on: ${{ matrix.environment }}

strategy:
max-parallel: 1 # 직렬처리
matrix:
environment: [ prod-a, prod-b ]

runs-on: prod
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Clean Up Legacy Image And Pull
- name: Clean Up Legacy Image
run: |
docker image prune -a -f
docker pull --platform linux/arm64 ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod
docker compose down || true
docker rmi $(docker images -q) -f || true

- name: Set up Container And Run Docker Image
- name: Docker Compose up
run: |
docker stop $DOCKER_CONTAINER_NAME || true
docker rm $DOCKER_CONTAINER_NAME || true
docker run -d --platform linux/arm64 --name $DOCKER_CONTAINER_NAME -v /var/logs/ody-prod-logs:/ody-prod-logs -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod -e JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod
export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
export DOCKERHUB_REPOSITORY=$DOCKERHUB_REPOSITORY
export PROD_MYSQL_PASSWORD=${{ secrets.PROD_MYSQL_PASSWORD }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }}
export TAG=${{ github.sha }}-prod_v2
docker compose up -d

- name: Health Check with Retry
uses: nick-invision/retry@v2
Expand All @@ -122,15 +118,22 @@ jobs:
- name: Rollback if Health Check fails
if: failure()
run: |
docker stop $DOCKER_CONTAINER_NAME || true
docker rm $DOCKER_CONTAINER_NAME || true
docker run -d --platform linux/arm64 --name $DOCKER_CONTAINER_NAME -v /var/logs/ody-prod-logs:/ody-prod-logs -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod -e JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }} ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous
docker compose down || true
docker rmi $(docker images -q) -f || true
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-previous

export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
export DOCKERHUB_REPOSITORY=$DOCKERHUB_REPOSITORY
export PROD_MYSQL_PASSWORD=${{ secrets.PROD_MYSQL_PASSWORD }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }}
export TAG=prod-previous
docker compose up -d

- name: Tag successful deployment as latest
if: success()
run: |
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-prod_v2 ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:prod-latest

- name: Check Docker Process
if: always()
Expand Down
3 changes: 2 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM openjdk:17-jdk

ARG JAR_FILE=./build/libs/ody-backend-0.0.1-SNAPSHOT.jar

ENV SPRING_PROFILES_ACTIVE=dev
ENV SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE}

ENV JASYPT_ENCRYPTOR_PASSWORD=${JASYPT_ENCRYPTOR_PASSWORD}

COPY ${JAR_FILE} ody-backend.jar
Expand Down
14 changes: 7 additions & 7 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ spring:
datasource:
write:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: ENC(/pYkPoMxvv/cFWWYEEIJ1/7gtMSjD9WKhM4oSIUEiK3WGRrcXZGELR8s+ozO5hmimO3U99cs3yMKf5B/5uh7RLk4wC3W1IuZ3F0oX3hSEJeq16YkxR1okYZokeE3mZXTxcOHBb3krNg=)
username: ENC(Pjtz7HDMEVm9ScNAjaDEqA==)
password: ENC(9yYOk4n/7co8h0HDfOA20tduvw3htCFa)
jdbc-url: ENC(+p2DZSe9yNp28DPB+6F7aERvNdgI9vTRrbpU5C5RmKIkrZaNNBTThg/Kb2xg1M1qrtOhYVLtbFnLLbiXUKIfmRPls9py5cf1eO/sAER4Rqpx9oxMQ8m75pOqw/L3CKzAMlL7/TvoXDB0HIvkeYejqg==)
username: ENC(YZ2LkaSgfkD9S8m67TunJw==)
password: ENC(yjnFbKAlVVvzzQuWNECa7DcpWubhBn0Q)
read:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: ENC(Ho2WMst/QlNJngqZ1nnRiQIaytH55rjohNVg/5W14vhcd88rAhk16y1yO3zzorKNGSsYV95t/lChWuFA5ivEz4Rh5NaZHjxddhvs1T7HHltl/eC5V1bhPh3+ck98Lk2iil0+RljDy+I=)
username: ENC(Pjtz7HDMEVm9ScNAjaDEqA==)
password: ENC(9yYOk4n/7co8h0HDfOA20tduvw3htCFa)
jdbc-url: ENC(+p2DZSe9yNp28DPB+6F7aERvNdgI9vTRrbpU5C5RmKIkrZaNNBTThg/Kb2xg1M1qrtOhYVLtbFnLLbiXUKIfmRPls9py5cf1eO/sAER4Rqpx9oxMQ8m75pOqw/L3CKzAMlL7/TvoXDB0HIvkeYejqg==)
username: ENC(YZ2LkaSgfkD9S8m67TunJw==)
password: ENC(yjnFbKAlVVvzzQuWNECa7DcpWubhBn0Q)
flyway:
baseline-version: 2

log:
file:
path: /ody-prod-logs
Loading