|
| 1 | +name: Build and Deploy |
| 2 | + |
| 3 | +# Run on push to main/master branch only |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - master |
| 9 | + workflow_dispatch: # Allow manual triggering |
| 10 | + |
| 11 | +# Grant necessary permissions |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pages: write |
| 15 | + id-token: write |
| 16 | + issues: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + # Job 1: Build with Vite |
| 21 | + build: |
| 22 | + name: Build with Vite |
| 23 | + needs: [] |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + build_status: ${{ steps.build_check.outputs.status }} |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + ref: ${{ github.ref_name }} |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Setup Node.js |
| 36 | + uses: actions/setup-node@v4 |
| 37 | + with: |
| 38 | + node-version: '20' |
| 39 | + cache: 'npm' |
| 40 | + |
| 41 | + - name: Install dependencies |
| 42 | + run: | |
| 43 | + echo "📦 Installing dependencies..." |
| 44 | + npm ci |
| 45 | +
|
| 46 | + - name: Build with Vite |
| 47 | + id: build_check |
| 48 | + run: | |
| 49 | + echo "🏗️ Building with Vite..." |
| 50 | + npm run build |
| 51 | +
|
| 52 | + # Check if build succeeded |
| 53 | + if [ ! -d "dist" ]; then |
| 54 | + echo "❌ Build failed - dist directory not created!" |
| 55 | + echo "status=failed" >> $GITHUB_OUTPUT |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +
|
| 59 | + # Verify critical files exist |
| 60 | + if [ ! -f "dist/index.html" ]; then |
| 61 | + echo "❌ Build failed - index.html not found in dist!" |
| 62 | + echo "status=failed" >> $GITHUB_OUTPUT |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +
|
| 66 | + echo "✅ Vite build completed successfully" |
| 67 | + echo "📦 Build output:" |
| 68 | + ls -lh dist/ |
| 69 | + echo "" |
| 70 | + echo "📦 Assets:" |
| 71 | + ls -lh dist/assets/ | head -20 |
| 72 | + echo "status=success" >> $GITHUB_OUTPUT |
| 73 | +
|
| 74 | + - name: Copy additional files to dist |
| 75 | + run: | |
| 76 | + echo "📋 Copying additional files using copy-assets.js..." |
| 77 | + # Use the centralized copy-assets.js script for consistency |
| 78 | + # This script maintains the list of all files/directories to copy |
| 79 | + node copy-assets.js |
| 80 | +
|
| 81 | + echo "" |
| 82 | + echo "📦 Final dist contents:" |
| 83 | + find dist -type f | head -50 |
| 84 | + echo "..." |
| 85 | + echo "Total files: $(find dist -type f | wc -l)" |
| 86 | +
|
| 87 | + - name: Upload artifact for deployment |
| 88 | + uses: actions/upload-pages-artifact@v3 |
| 89 | + with: |
| 90 | + path: 'dist' |
| 91 | + |
| 92 | + # Job 4a: Report Build Status |
| 93 | + report-status: |
| 94 | + name: Report Build Status |
| 95 | + needs: build |
| 96 | + runs-on: ubuntu-latest |
| 97 | + if: always() |
| 98 | + |
| 99 | + steps: |
| 100 | + - name: Report success |
| 101 | + if: needs.build.outputs.build_status == 'success' |
| 102 | + run: | |
| 103 | + echo "✅ BUILD SUCCESSFUL" |
| 104 | + echo "================================" |
| 105 | + echo "Built with: Vite" |
| 106 | + echo "Status: SUCCESS" |
| 107 | + echo "Ready for deployment" |
| 108 | + echo "================================" |
| 109 | +
|
| 110 | + - name: Report failure |
| 111 | + if: needs.build.outputs.build_status == 'failed' |
| 112 | + run: | |
| 113 | + echo "❌ BUILD FAILED" |
| 114 | + echo "================================" |
| 115 | + echo "Built with: Vite" |
| 116 | + echo "Status: FAILED" |
| 117 | + echo "Check build logs for details" |
| 118 | + echo "================================" |
| 119 | + exit 1 |
| 120 | +
|
| 121 | + - name: Create status comment (if PR) |
| 122 | + if: github.event_name == 'pull_request' |
| 123 | + uses: actions/github-script@v7 |
| 124 | + with: |
| 125 | + script: | |
| 126 | + const status = '${{ needs.build.outputs.build_status }}'; |
| 127 | + const icon = status === 'success' ? '✅' : '❌'; |
| 128 | + const message = status === 'success' ? 'Build successful!' : 'Build failed!'; |
| 129 | +
|
| 130 | + github.rest.issues.createComment({ |
| 131 | + issue_number: context.issue.number, |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + body: `${icon} **${message}**\n\n**Built with:** Vite\n**Status:** ${status.toUpperCase()}` |
| 135 | + }); |
| 136 | +
|
| 137 | + # Job 4b: Deploy to GitHub Pages |
| 138 | + deploy: |
| 139 | + name: Deploy to GitHub Pages |
| 140 | + needs: build |
| 141 | + runs-on: ubuntu-latest |
| 142 | + if: needs.build.outputs.build_status == 'success' |
| 143 | + |
| 144 | + # Required for GitHub Pages deployment |
| 145 | + environment: |
| 146 | + name: github-pages |
| 147 | + url: ${{ steps.deployment.outputs.page_url }} |
| 148 | + |
| 149 | + steps: |
| 150 | + - name: Deploy to GitHub Pages |
| 151 | + id: deployment |
| 152 | + uses: actions/deploy-pages@v4 |
| 153 | + |
| 154 | + - name: Purge Cloudflare cache |
| 155 | + run: | |
| 156 | + echo "🧹 Purging Cloudflare cache..." |
| 157 | + curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${{ secrets.CLOUDFLARE_ZONE_ID }}/purge_cache" \ |
| 158 | + -H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \ |
| 159 | + -H "Content-Type: application/json" \ |
| 160 | + --data '{"purge_everything":true}' | jq . |
| 161 | + echo "✅ Cache purge requested" |
| 162 | +
|
| 163 | + - name: Report deployment success |
| 164 | + run: | |
| 165 | + echo "🚀 DEPLOYMENT SUCCESSFUL" |
| 166 | + echo "================================" |
| 167 | + echo "Branch: ${{ github.ref_name }}" |
| 168 | + echo "URL: ${{ steps.deployment.outputs.page_url }}" |
| 169 | + echo "Built with: Vite (optimized)" |
| 170 | + echo "Cache: Purged via Cloudflare API" |
| 171 | + echo "================================" |
0 commit comments