Deploy to Production #101
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: FieldTrack Backend CI/CD | |
| on: | |
| push: | |
| branches: | |
| - master | |
| pull_request: | |
| branches: | |
| - master | |
| workflow_dispatch: | |
| inputs: | |
| mode: | |
| description: "Pipeline mode" | |
| required: true | |
| default: "deploy-and-test" | |
| type: choice | |
| options: | |
| - deploy | |
| - smoke-test | |
| - deploy-and-test | |
| jobs: | |
| test: | |
| name: Test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v5 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v5 | |
| with: | |
| node-version: "24" | |
| cache: npm | |
| cache-dependency-path: apps/api/package-lock.json | |
| - name: Install dependencies | |
| run: npm install | |
| - name: TypeScript compilation check | |
| run: npm run typecheck | |
| - name: Run tests | |
| working-directory: apps/api | |
| run: npm run test | |
| build-and-deploy: | |
| name: Build and Deploy | |
| runs-on: ubuntu-latest | |
| needs: test | |
| if: | | |
| github.event_name == 'push' || | |
| github.event.inputs.mode == 'deploy' || | |
| github.event.inputs.mode == 'deploy-and-test' | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v5 | |
| - name: Extract commit SHA | |
| id: sha | |
| run: echo "sha_short=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./apps/api/Dockerfile | |
| push: true | |
| tags: | | |
| ghcr.io/rajashish147/fieldtrack-backend:${{ steps.sha.outputs.sha_short }} | |
| ghcr.io/rajashish147/fieldtrack-backend:latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Deploy to VPS (Blue-Green) | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.DO_HOST }} | |
| username: ${{ secrets.DO_USER }} | |
| key: ${{ secrets.DO_SSH_KEY }} | |
| script: | | |
| cd /home/ashish/FieldTrack-2.0 | |
| git fetch origin | |
| git reset --hard origin/master | |
| chmod +x apps/api/scripts/deploy-bluegreen.sh | |
| ./apps/api/scripts/deploy-bluegreen.sh "${{ steps.sha.outputs.sha_short }}" | |
| smoke-tests: | |
| name: API Smoke Tests | |
| runs-on: ubuntu-latest | |
| needs: build-and-deploy | |
| if: | | |
| github.event.inputs.mode == 'smoke-test' || | |
| github.event.inputs.mode == 'deploy-and-test' || | |
| github.event_name == 'push' | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v5 | |
| - name: Wait for API health | |
| run: | | |
| echo "Waiting for API..." | |
| for i in {1..30}; do | |
| STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://fieldtrack.meowsician.tech/health) | |
| if [ "$STATUS" = "200" ]; then | |
| echo "API is healthy" | |
| exit 0 | |
| fi | |
| sleep 2 | |
| done | |
| echo "API did not become healthy" | |
| exit 1 | |
| - name: Install jq (needed for JSON parsing) | |
| run: sudo apt-get update && sudo apt-get install -y jq | |
| - name: Run API Smoke Tests | |
| env: | |
| FT_API_BASE_URL: ${{ secrets.FT_API_BASE_URL }} | |
| FT_EMP_EMAIL: ${{ secrets.FT_EMP_EMAIL }} | |
| FT_EMP_PASSWORD: ${{ secrets.FT_EMP_PASSWORD }} | |
| FT_ADMIN_EMAIL: ${{ secrets.FT_ADMIN_EMAIL }} | |
| FT_ADMIN_PASSWORD: ${{ secrets.FT_ADMIN_PASSWORD }} | |
| SUPABASE_URL: ${{ secrets.SUPABASE_URL }} | |
| SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }} | |
| run: | | |
| chmod +x apps/api/scripts/smoke-test.sh | |
| ./apps/api/scripts/smoke-test.sh | |
| - name: Upload smoke test report | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: smoke-test-report | |
| path: smoke-report.json | |
| rollback: | |
| name: Rollback Deployment | |
| runs-on: ubuntu-latest | |
| needs: smoke-tests | |
| if: failure() | |
| steps: | |
| - name: Rollback on VPS | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.DO_HOST }} | |
| username: ${{ secrets.DO_USER }} | |
| key: ${{ secrets.DO_SSH_KEY }} | |
| script: | | |
| cd /home/ashish/FieldTrack-2.0 | |
| chmod +x apps/api/scripts/rollback.sh | |
| ./apps/api/scripts/rollback.sh --auto |