Merge pull request #381 from kookmin-sw/backend/feat/#380-reference-u… #114
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 | |
| on: | |
| push: | |
| branches: [backend/develop] | |
| paths: | |
| - 'backend/**' | |
| - '.github/workflows/deploy.yml' | |
| workflow_dispatch: | |
| concurrency: | |
| group: deploy-${{ github.ref }} | |
| cancel-in-progress: false | |
| permissions: | |
| contents: read | |
| env: | |
| IMAGE_NAME: flowmeet-api | |
| jobs: | |
| build-and-push: | |
| name: Build & Push | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.sha }} | |
| image: ${{ steps.meta.outputs.image }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Resolve image reference | |
| id: meta | |
| run: | | |
| { | |
| echo "sha=${GITHUB_SHA}" | |
| echo "image=${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Build and push | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./backend | |
| platforms: linux/amd64 | |
| push: true | |
| tags: | | |
| ${{ steps.meta.outputs.image }}:latest | |
| ${{ steps.meta.outputs.image }}:${{ steps.meta.outputs.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| name: Deploy to EC2 | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Copy compose files to EC2 | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: "backend/docker-compose.yml,backend/docker/nginx/**,backend/docker/scripts/**" | |
| target: "/home/ubuntu" | |
| strip_components: 1 | |
| - name: Deploy over SSH | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| set -euo pipefail | |
| cd /home/ubuntu | |
| export IMAGE_TAG=${{ needs.build-and-push.outputs.image_tag }} | |
| sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=${IMAGE_TAG}/" .env | |
| echo "::group::Pull & Up" | |
| docker compose pull app | |
| docker compose up -d app | |
| echo "::endgroup::" | |
| echo "::group::Wait for health" | |
| for i in $(seq 1 24); do | |
| status=$(docker inspect -f '{{.State.Health.Status}}' flowmeet-api 2>/dev/null || echo "starting") | |
| echo "[$i/24] app health: $status" | |
| if [ "$status" = "healthy" ]; then | |
| echo "healthy" | |
| break | |
| fi | |
| if [ "$i" -eq 24 ]; then | |
| echo "::error::app did not become healthy within 2 minutes" | |
| docker compose logs --tail=200 app | |
| exit 1 | |
| fi | |
| sleep 5 | |
| done | |
| echo "::endgroup::" | |
| docker image prune -f |