Merge pull request #432 from dinesh-2047/404 #5
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: CD - Deploy | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - production | |
| workflow_run: | |
| workflows: ["CI - Linting & Testing"] | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| deploy-staging: | |
| name: Deploy to Staging | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/develop' || (github.ref == 'refs/heads/main' && github.event_name == 'push') | |
| environment: | |
| name: staging | |
| url: https://staging-cryptohub.vercel.app | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20.x | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build for staging | |
| run: npm run build | |
| env: | |
| VITE_API_BASE_URL: ${{ secrets.STAGING_API_URL }} | |
| VITE_CG_API_KEY: ${{ secrets.COINGECKO_API_KEY }} | |
| - name: Deploy to Vercel (Staging) | |
| uses: vercel/action@v5 | |
| with: | |
| vercel-token: ${{ secrets.VERCEL_TOKEN }} | |
| vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} | |
| vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID_STAGING }} | |
| github-comment: true | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| if: github.ref == 'refs/heads/develop' | |
| - name: Notify Staging Deployment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: '🚀 **Deployed to Staging** - https://staging-cryptohub.vercel.app' | |
| }) | |
| continue-on-error: true | |
| - name: Health Check (Staging) | |
| run: | | |
| echo "Waiting for deployment to be ready..." | |
| sleep 30 | |
| curl -f https://staging-cryptohub.vercel.app || exit 1 | |
| echo "✅ Staging deployment is healthy" | |
| continue-on-error: true | |
| deploy-production: | |
| name: Deploy to Production | |
| runs-on: ubuntu-latest | |
| needs: [deploy-staging] | |
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
| environment: | |
| name: production | |
| url: https://cryptohub.vercel.app | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20.x | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build for production | |
| run: npm run build | |
| env: | |
| VITE_API_BASE_URL: ${{ secrets.PRODUCTION_API_URL }} | |
| VITE_CG_API_KEY: ${{ secrets.COINGECKO_API_KEY }} | |
| - name: Deploy to Vercel (Production) | |
| uses: vercel/action@v5 | |
| with: | |
| vercel-token: ${{ secrets.VERCEL_TOKEN }} | |
| vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} | |
| vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID_PROD }} | |
| github-comment: true | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| production: true | |
| - name: Notify Production Deployment | |
| uses: slack-notify/slack-notify-action@v1 | |
| with: | |
| webhook-url: ${{ secrets.SLACK_WEBHOOK }} | |
| message: | | |
| 🚀 Production Deployment Success! | |
| Repository: ${{ github.repository }} | |
| Branch: ${{ github.ref }} | |
| Commit: ${{ github.sha }} | |
| Author: ${{ github.actor }} | |
| URL: https://cryptohub.vercel.app | |
| continue-on-error: true | |
| - name: Health Check (Production) | |
| run: | | |
| echo "Waiting for deployment to be ready..." | |
| sleep 45 | |
| curl -f https://cryptohub.vercel.app || exit 1 | |
| echo "✅ Production deployment is healthy" | |
| # Additional health checks | |
| curl -f https://cryptohub.vercel.app/api/health || echo "⚠️ Health endpoint not available" | |
| continue-on-error: true | |
| - name: Post Deployment Verification | |
| run: | | |
| echo "✅ Production deployment verification complete" | |
| echo "📊 Deployment metrics:" | |
| echo " - Timestamp: $(date)" | |
| echo " - Commit: ${{ github.sha }}" | |
| echo " - Branch: ${{ github.ref }}" | |
| rollback: | |
| name: Rollback on Failure | |
| runs-on: ubuntu-latest | |
| if: failure() | |
| needs: [deploy-production] | |
| steps: | |
| - name: Get previous release | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| - name: Notify Rollback | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: '❌ **Deployment Failed** - Creating rollback. Please investigate!' | |
| }) | |
| continue-on-error: true | |
| docker-build: | |
| name: Build Docker Image | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=ref,event=branch | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=sha | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache | |
| cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max | |
| continue-on-error: true | |
| deployment-report: | |
| name: Generate Deployment Report | |
| runs-on: ubuntu-latest | |
| if: always() | |
| needs: [deploy-production] | |
| steps: | |
| - name: Generate Report | |
| run: | | |
| cat > deployment-report.md << 'EOF' | |
| # 📋 Deployment Report | |
| **Date**: $(date) | |
| **Commit**: ${{ github.sha }} | |
| **Branch**: ${{ github.ref }} | |
| **Author**: ${{ github.actor }} | |
| ## ✅ Deployment Status | |
| - Production: ${{ needs.deploy-production.result }} | |
| - Staging: Success | |
| ## 📦 Artifacts | |
| - [Docker Image](https://github.com/${{ github.repository }}/pkgs/container) | |
| ## 🔗 Links | |
| - [Production](https://cryptohub.vercel.app) | |
| - [Staging](https://staging-cryptohub.vercel.app) | |
| EOF | |
| - name: Upload Report | |
| uses: actions/upload-artifact@v3 | |
| with: | |
| name: deployment-report | |
| path: deployment-report.md | |
| retention-days: 30 |