diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..01dfb038 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,71 @@ +name: Deploy to EC2 + +on: + pull_request: + branches: [deploy] + types: [closed] + +jobs: + deploy: + # PR이 머지된 경우에만 실행 (닫기만 한 건 무시) + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + + steps: + # 1. 코드 체크아웃 + - name: Checkout code + uses: actions/checkout@v4 + + # 2. Docker Buildx 설정 (멀티 아키텍처 빌드용) + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # 3. ghcr.io 로그인 + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # 4. Docker 이미지 빌드 + push + - name: Build and Push Docker Image + uses: docker/build-push-action@v5 + with: + context: . + push: true + platforms: linux/amd64 + tags: | + ghcr.io/team-timo/timo-server:latest + ghcr.io/team-timo/timo-server:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + # 5. EC2에 SSH 접속해서 배포 + - name: Deploy to EC2 + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.EC2_HOST }} + username: ubuntu + key: ${{ secrets.EC2_SSH_KEY }} + script: | + # ghcr.io 로그인 + echo ${{ secrets.GHCR_TOKEN }} | sudo docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin + + # 최신 이미지 pull + sudo docker pull ghcr.io/team-timo/timo-server:latest + + # 배포 디렉토리 이동 + cd ~/timo-deploy + + # 컨테이너 재시작 + sudo docker compose down + sudo docker compose up -d + + # 오래된 이미지 정리 + sudo docker image prune -f + + # 헬스체크 (30초 대기) + echo "Waiting for app to start..." + sleep 30 + curl -f http://localhost:8080/swagger-ui.html || echo "Health check failed, but container is running"