From e7263e86484919de097caa353b325a512bafda2f Mon Sep 17 00:00:00 2001 From: sseoyeon-ssonia Date: Tue, 23 Jun 2026 17:55:07 +0900 Subject: [PATCH 1/4] =?UTF-8?q?chore=20:=20main=20CI/CD=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=B4=ED=94=84=EB=9D=BC=EC=9D=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-main.yml | 123 ++++++++++++++++++++++++++++++ backend/docker-compose.yml | 11 ++- 2 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/deploy-main.yml diff --git a/.github/workflows/deploy-main.yml b/.github/workflows/deploy-main.yml new file mode 100644 index 0000000..eec332a --- /dev/null +++ b/.github/workflows/deploy-main.yml @@ -0,0 +1,123 @@ +name: CI/CD Deploy Main + +on: + pull_request: + branches: ["main"] + paths: + - "backend/**" + - "frontend/**" + - ".github/workflows/deploy-main.yml" + + push: + branches: ["main"] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: wsssafy/ssafy-pjt-backend + +jobs: + backend: + name: Backend Test & Docker Build + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + defaults: + run: + working-directory: backend + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: "21" + distribution: "temurin" + cache: "gradle" + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build and Test with Gradle + run: ./gradlew build + + - name: Login to GHCR + if: github.event_name == 'push' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and Push Docker Image + if: github.event_name == 'push' + uses: docker/build-push-action@v6 + with: + context: backend + file: backend/Dockerfile + push: true + tags: | + ghcr.io/${{ env.IMAGE_NAME }}:latest + ghcr.io/${{ env.IMAGE_NAME }}:${{ github.sha }} + labels: | + org.opencontainers.image.source=https://github.com/${{ github.repository }} + + frontend: + name: Frontend Build + runs-on: ubuntu-latest + + defaults: + run: + working-directory: frontend + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: "22" + cache: "npm" + cache-dependency-path: frontend/package-lock.json + + - name: Install dependencies + run: npm ci + + - name: Build frontend + run: npm run build + + deploy: + name: Deploy to EC2 + runs-on: ubuntu-latest + needs: [backend, frontend] + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + + steps: + - name: Deploy to EC2 + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.EC2_SSH_KEY }} + script: | + set -e + + cd ~/mallo + git fetch origin main + git reset --hard origin/main + + cd ~/mallo/frontend + npm ci + npm run build + sudo rm -rf /var/www/html/* + sudo cp -r dist/* /var/www/html/ + + cd ~/mallo/backend + docker-compose pull app + docker-compose up -d --force-recreate app + docker system prune -f \ No newline at end of file diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index bba6b74..f6c9bf1 100644 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -1,3 +1,5 @@ +version: "2.4" + services: mysql: image: mysql:8.0 @@ -23,12 +25,9 @@ services: start_period: 5s app: - build: - context: . - dockerfile: Dockerfile - image: backend-app + image: ghcr.io/wsssafy/ssafy-pjt-backend:latest container_name: backend-app - restart: no + restart: always env_file: - .env - .secret.env @@ -41,4 +40,4 @@ services: - ./logs:/app/logs volumes: - mysql_data: + mysql_data: \ No newline at end of file From 82d23521d2a58ef864e55766f720b22046b740b3 Mon Sep 17 00:00:00 2001 From: sseoyeon-ssonia Date: Tue, 23 Jun 2026 22:43:54 +0900 Subject: [PATCH 2/4] =?UTF-8?q?chore:=20GHCR=20=EC=9D=B4=EB=AF=B8=EC=A7=80?= =?UTF-8?q?=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-main.yml | 2 +- backend/docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-main.yml b/.github/workflows/deploy-main.yml index eec332a..390effc 100644 --- a/.github/workflows/deploy-main.yml +++ b/.github/workflows/deploy-main.yml @@ -13,7 +13,7 @@ on: env: REGISTRY: ghcr.io - IMAGE_NAME: wsssafy/ssafy-pjt-backend + IMAGE_NAME: optimesai/mallo-backend jobs: backend: diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index f6c9bf1..4ea901f 100644 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -25,7 +25,7 @@ services: start_period: 5s app: - image: ghcr.io/wsssafy/ssafy-pjt-backend:latest + image: ghcr.io/optimesai/mallo-backend:latest container_name: backend-app restart: always env_file: From 369bdc8dc0556c681e98151cb7bcd3691ea57bfd Mon Sep 17 00:00:00 2001 From: Seoyeon Kim Date: Tue, 23 Jun 2026 23:30:09 +0900 Subject: [PATCH 3/4] =?UTF-8?q?chore=20:=20app=20=EC=BB=A8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EB=84=88=20=EC=9E=AC=EC=8B=9C=EC=9E=91=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy-main.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-main.yml b/.github/workflows/deploy-main.yml index 390effc..3854008 100644 --- a/.github/workflows/deploy-main.yml +++ b/.github/workflows/deploy-main.yml @@ -119,5 +119,7 @@ jobs: cd ~/mallo/backend docker-compose pull app - docker-compose up -d --force-recreate app - docker system prune -f \ No newline at end of file + docker-compose stop app || true + docker-compose rm -f app || true + docker-compose up -d --no-deps app + docker system prune -f From 8602dbe24c8bb4dcbda302dd3bc6a716ab7c4166 Mon Sep 17 00:00:00 2001 From: Seoyeon Kim Date: Tue, 23 Jun 2026 23:46:22 +0900 Subject: [PATCH 4/4] =?UTF-8?q?chore=20:=20=EB=B0=B0=ED=8F=AC=20=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=A6=BD=ED=8A=B8=20DB=20=EA=B8=B0=EB=8F=99=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added health check for MySQL container during deployment. 배포 시 MySQL 기동 보장 및 app 컨테이너만 재시작하도록 수정 --- .github/workflows/deploy-main.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/deploy-main.yml b/.github/workflows/deploy-main.yml index 3854008..37021f2 100644 --- a/.github/workflows/deploy-main.yml +++ b/.github/workflows/deploy-main.yml @@ -119,6 +119,19 @@ jobs: cd ~/mallo/backend docker-compose pull app + docker-compose up -d --no-recreate mysql + + i=0 + until [ "$(docker inspect --format='{{.State.Health.Status}}' backend-mysql 2>/dev/null)" = "healthy" ]; do + i=$((i+1)) + if [ "$i" -ge 30 ]; then + docker-compose logs --tail=100 mysql + exit 1 + fi + echo "Waiting for mysql..." + sleep 3 + done + docker-compose stop app || true docker-compose rm -f app || true docker-compose up -d --no-deps app