Merge pull request #78 from Sysone-Final/feature/SYSONE-73-monitoring #132
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Backend CI/CD to GCP | |
| on: | |
| push: | |
| branches: | |
| - "main" | |
| - "refactor/SYSONE-88-(53+63)-리팩토링" | |
| # - "ci/SYSONE-115-모니터링" | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| # PostgreSQL 서비스 컨테이너 추가 | |
| services: | |
| postgres: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_DB: testdb | |
| POSTGRES_USER: testuser | |
| POSTGRES_PASSWORD: testpass | |
| ports: | |
| - 5432:5432 | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| cache: 'gradle' | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| - name: Test with Gradle | |
| env: | |
| DB_URL: jdbc:postgresql://localhost:5432/testdb | |
| DB_USERNAME: testuser | |
| DB_PASSWORD: testpass | |
| JWT_SECRET: test-jwt-secret-key-for-github-actions-minimum-32-characters-long-required | |
| run: ./gradlew test | |
| deploy: | |
| needs: test # test 작업이 성공해야만 실행됩니다. | |
| runs-on: ubuntu-latest | |
| # if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/refactor/SYSONE-88-(53+63)-리팩토링' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/final-be:latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Deploy to GCP VM | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.GCP_HOST }} | |
| username: ${{ secrets.GCP_USER }} | |
| key: ${{ secrets.GCP_PRIVATE_KEY }} | |
| script: | | |
| # 1. app 폴더로 이동 | |
| cd /home/sana2d2v/app | |
| # 2. 'root' 사용자가 이 디렉토리를 신뢰하도록 설정 | |
| sudo git config --global --add safe.directory /home/sana2d2v/app | |
| # 3. 'sudo' (root) 권한으로 로컬 변경사항을 강제 리셋 | |
| sudo git reset --hard HEAD | |
| # 4. 'sudo' (root) 권한으로 최신 코드를 pull | |
| sudo git pull | |
| # 5. 'app'과 'nginx' 컨테이너 모두 강제 삭제 | |
| sudo docker stop final-be-app || true | |
| sudo docker rm final-be-app || true | |
| sudo docker stop final-be-nginx || true | |
| sudo docker rm final-be-nginx || true | |
| # 6. --volumes 플래그를 추가하여 Docker의 잘못된 캐시(볼륨)를 삭제 | |
| sudo docker-compose down --volumes | |
| # 7. 'sudo'로 docker 명령어 실행 | |
| sudo docker-compose pull | |
| sudo docker-compose up -d | |
| # 8. 5초 대기 후 로그 확인 | |
| sleep 5 | |
| sudo docker-compose logs app | |
| sudo docker image prune -f | |
| echo "✅ Backend Deploy Success!" |