|
| 1 | +name: Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master, dev] |
| 6 | + pull_request: |
| 7 | + branches: [master, dev] |
| 8 | + |
| 9 | +# Allow concurrent deployments for different environments |
| 10 | +concurrency: |
| 11 | + group: 'pages-${{ github.ref }}' |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Setup Node.js |
| 23 | + uses: actions/setup-node@v4 |
| 24 | + with: |
| 25 | + node-version: '20' |
| 26 | + cache: 'npm' |
| 27 | + |
| 28 | + - name: Install dependencies |
| 29 | + run: npm ci |
| 30 | + |
| 31 | + - name: Run type check |
| 32 | + run: npm run type-check |
| 33 | + |
| 34 | + - name: Run linting |
| 35 | + run: npm run lint |
| 36 | + |
| 37 | + - name: Run tests |
| 38 | + run: npm run test |
| 39 | + |
| 40 | + - name: Build application |
| 41 | + run: | |
| 42 | + echo "Building for branch: ${{ github.ref_name }}" |
| 43 | + echo "NODE_ENV: $NODE_ENV" |
| 44 | + echo "SURGE_PREVIEW: $SURGE_PREVIEW" |
| 45 | + npm run build |
| 46 | + env: |
| 47 | + NEXT_PUBLIC_GA_ID: ${{ secrets.NEXT_PUBLIC_GA_ID }} |
| 48 | + NEXT_PUBLIC_REQUIRE_CONSENT: true |
| 49 | + NEXT_PUBLIC_ANONYMIZE_IP: true |
| 50 | + # Set SURGE_PREVIEW for non-master branches to disable basePath |
| 51 | + SURGE_PREVIEW: ${{ github.ref != 'refs/heads/master' && 'true' || '' }} |
| 52 | + |
| 53 | + - name: Upload Pages Artifact (Production) |
| 54 | + if: github.ref == 'refs/heads/master' |
| 55 | + uses: actions/upload-pages-artifact@v3 |
| 56 | + with: |
| 57 | + path: ./out |
| 58 | + |
| 59 | + - name: Upload Preview Artifact |
| 60 | + if: github.ref != 'refs/heads/master' |
| 61 | + uses: actions/upload-artifact@v4 |
| 62 | + with: |
| 63 | + name: preview-build-${{ github.run_number }}-${{ github.run_attempt }} |
| 64 | + path: ./out |
| 65 | + retention-days: 30 |
| 66 | + |
| 67 | + # Production deployment to main GitHub Pages |
| 68 | + deploy-production: |
| 69 | + needs: build |
| 70 | + runs-on: ubuntu-latest |
| 71 | + |
| 72 | + # Only deploy to production from master branch |
| 73 | + if: github.ref == 'refs/heads/master' |
| 74 | + |
| 75 | + permissions: |
| 76 | + pages: write |
| 77 | + id-token: write |
| 78 | + |
| 79 | + environment: |
| 80 | + name: github-pages |
| 81 | + url: ${{ steps.deployment.outputs.page_url }} |
| 82 | + |
| 83 | + steps: |
| 84 | + - name: Deploy to GitHub Pages |
| 85 | + id: deployment |
| 86 | + uses: actions/deploy-pages@v4 |
| 87 | + |
| 88 | + # Preview deployment for development branches |
| 89 | + deploy-preview: |
| 90 | + needs: build |
| 91 | + runs-on: ubuntu-latest |
| 92 | + |
| 93 | + # Deploy preview for dev branches (not master, only on direct push) |
| 94 | + if: github.ref != 'refs/heads/master' && github.event_name == 'push' |
| 95 | + |
| 96 | + # Add deployment environment to show URL in GitHub UI |
| 97 | + environment: |
| 98 | + name: preview-${{ github.ref_name }} |
| 99 | + |
| 100 | + steps: |
| 101 | + - name: Calculate sanitized branch name |
| 102 | + id: branch |
| 103 | + run: | |
| 104 | + SANITIZED_BRANCH=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9-]/-/g' | tr '[:upper:]' '[:lower:]') |
| 105 | + echo "sanitized=$SANITIZED_BRANCH" >> $GITHUB_OUTPUT |
| 106 | + echo "url=https://gprg-${SANITIZED_BRANCH}.surge.sh" >> $GITHUB_OUTPUT |
| 107 | + - name: Debug deployment conditions |
| 108 | + run: | |
| 109 | + echo "GitHub ref: ${{ github.ref }}" |
| 110 | + echo "GitHub ref name: ${{ github.ref_name }}" |
| 111 | + echo "Event name: ${{ github.event_name }}" |
| 112 | + echo "Is master?: ${{ github.ref == 'refs/heads/master' }}" |
| 113 | + echo "Is push?: ${{ github.event_name == 'push' }}" |
| 114 | + echo "Should deploy?: ${{ github.ref != 'refs/heads/master' && github.event_name == 'push' }}" |
| 115 | +
|
| 116 | + - name: Download Preview Artifact |
| 117 | + uses: actions/download-artifact@v4 |
| 118 | + with: |
| 119 | + name: preview-build-${{ github.run_number }}-${{ github.run_attempt }} |
| 120 | + path: ./preview-out |
| 121 | + |
| 122 | + - name: Deploy to Surge.sh (Preview) |
| 123 | + id: deploy |
| 124 | + run: | |
| 125 | + npm install -g surge |
| 126 | + echo "Deploying preview for branch: ${{ github.ref_name }}" |
| 127 | + echo "Sanitized branch name: ${{ steps.branch.outputs.sanitized }}" |
| 128 | + echo "Preview URL: ${{ steps.branch.outputs.url }}" |
| 129 | + surge ./preview-out ${{ steps.branch.outputs.url }} --token ${{ secrets.SURGE_TOKEN }} |
| 130 | + echo "deployment_url=${{ steps.branch.outputs.url }}" >> $GITHUB_OUTPUT |
| 131 | + env: |
| 132 | + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} |
| 133 | + |
| 134 | + - name: Comment PR with Preview URL |
| 135 | + if: github.event_name == 'pull_request' |
| 136 | + uses: actions/github-script@v7 |
| 137 | + with: |
| 138 | + script: | |
| 139 | + const previewUrl = '${{ steps.deploy.outputs.deployment_url }}'; |
| 140 | +
|
| 141 | + github.rest.issues.createComment({ |
| 142 | + issue_number: context.issue.number, |
| 143 | + owner: context.repo.owner, |
| 144 | + repo: context.repo.repo, |
| 145 | + body: `🚀 **Preview Deployment Ready!**\n\n📱 Preview URL: ${previewUrl}\n\n*This preview will be available for 30 days.*` |
| 146 | + }); |
| 147 | +
|
| 148 | + - name: Create deployment status |
| 149 | + uses: actions/github-script@v7 |
| 150 | + with: |
| 151 | + script: | |
| 152 | + const previewUrl = '${{ steps.deploy.outputs.deployment_url }}'; |
| 153 | +
|
| 154 | + // Create a commit status with the deployment URL |
| 155 | + github.rest.repos.createCommitStatus({ |
| 156 | + owner: context.repo.owner, |
| 157 | + repo: context.repo.repo, |
| 158 | + sha: context.sha, |
| 159 | + state: 'success', |
| 160 | + target_url: previewUrl, |
| 161 | + description: `Preview deployed to ${previewUrl}`, |
| 162 | + context: 'deployment/preview' |
| 163 | + }); |
| 164 | +
|
| 165 | + - name: Update Status Check |
| 166 | + run: | |
| 167 | + echo "🚀 Preview deployed successfully!" |
| 168 | + echo "📱 Preview URL: ${{ steps.deploy.outputs.deployment_url }}" |
| 169 | + echo "" |
| 170 | + echo "You can find this URL in:" |
| 171 | + echo "1. GitHub Actions > Environments tab" |
| 172 | + echo "2. Commit status checks" |
| 173 | + echo "3. This workflow run summary" |
| 174 | +
|
| 175 | + # Add to job summary for easy access |
| 176 | + echo "## 🚀 Preview Deployment Complete!" >> $GITHUB_STEP_SUMMARY |
| 177 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 178 | + echo "**Branch:** \`${{ github.ref_name }}\`" >> $GITHUB_STEP_SUMMARY |
| 179 | + echo "**Preview URL:** [${{ steps.deploy.outputs.deployment_url }}](${{ steps.deploy.outputs.deployment_url }})" >> $GITHUB_STEP_SUMMARY |
| 180 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 181 | + echo "### Where to find this URL:" >> $GITHUB_STEP_SUMMARY |
| 182 | + echo "- **Environments tab:** Go to your repository → Environments → preview-${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY |
| 183 | + echo "- **Commit status:** Check the commit status checks for 'deployment/preview'" >> $GITHUB_STEP_SUMMARY |
| 184 | + echo "- **This summary:** Bookmark this workflow run for easy access" >> $GITHUB_STEP_SUMMARY |
0 commit comments