Merge pull request #34 from duynd0909/dev #13
Workflow file for this run
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: Build & Deploy | |
| on: | |
| push: | |
| tags: | |
| - 'v*' | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_PREFIX: ghcr.io/${{ github.repository_owner }} | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: npm | |
| - run: npm ci | |
| - run: npx prisma generate --schema=apps/api/prisma/schema.prisma | |
| - run: npm run typecheck | |
| - run: npm run lint | |
| - run: npm run test | |
| build-and-push: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GHCR_TOKEN }} | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.IMAGE_PREFIX }}/stackdify-api,${{ env.IMAGE_PREFIX }}/stackdify-web | |
| tags: | | |
| type=sha,prefix=sha- | |
| type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} | |
| - name: Build & push API image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: apps/api/Dockerfile | |
| push: true | |
| tags: ${{ env.IMAGE_PREFIX }}/stackdify-api:latest,${{ env.IMAGE_PREFIX }}/stackdify-api:sha-${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build & push Web image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: apps/web/Dockerfile | |
| push: true | |
| tags: ${{ env.IMAGE_PREFIX }}/stackdify-web:latest,${{ env.IMAGE_PREFIX }}/stackdify-web:sha-${{ github.sha }} | |
| build-args: NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Connect to Tailscale | |
| uses: tailscale/github-action@v2 | |
| with: | |
| authkey: ${{ secrets.TAILSCALE_AUTHKEY }} | |
| - name: Setup SSH key | |
| env: | |
| SSH_PASS: ${{ secrets.DEPLOY_SSH_PASSPHRASE }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| # Create askpass script so ssh can provide the passphrase non-interactively | |
| cat > ~/.ssh/askpass.sh << 'ASKPASS' | |
| #!/bin/sh | |
| echo "$SSH_PASS" | |
| ASKPASS | |
| chmod +x ~/.ssh/askpass.sh | |
| - name: Deploy via SSH | |
| env: | |
| SSH_ASKPASS: ~/.ssh/askpass.sh | |
| SSH_ASKPASS_REQUIRE: force | |
| run: | | |
| ssh -o StrictHostKeyChecking=no -i ~/.ssh/deploy_key -p ${{ secrets.DEPLOY_PORT }} ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} << 'DEPLOY' | |
| cd ${{ secrets.DEPLOY_PATH }} | |
| # Pull latest images | |
| docker compose -f docker-compose.prod.yml pull | |
| # Run DB migrations (zero-downtime: run before swap) | |
| docker compose -f docker-compose.prod.yml run --rm api \ | |
| sh -c "npx prisma migrate deploy" | |
| # Rolling restart (no downtime for stateless services) | |
| docker compose -f docker-compose.prod.yml up -d --remove-orphans | |
| # Health check | |
| sleep 10 | |
| curl -f http://localhost:3001/api/v1/health || exit 1 | |
| # Clean up old images | |
| docker image prune -f | |
| DEPLOY |