|
| 1 | +name: Deploy to Netlify |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + pull-requests: write |
| 13 | + steps: |
| 14 | + - uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - uses: oven-sh/setup-bun@v2 |
| 17 | + with: |
| 18 | + bun-version: latest |
| 19 | + |
| 20 | + - run: bun install --frozen-lockfile |
| 21 | + |
| 22 | + - run: bun run build |
| 23 | + |
| 24 | + - name: Deploy to Netlify (Production) |
| 25 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 26 | + run: npx netlify-cli deploy --dir=public --prod |
| 27 | + env: |
| 28 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 29 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
| 30 | + |
| 31 | + - name: Deploy to Netlify (Preview) |
| 32 | + if: github.event_name == 'pull_request' |
| 33 | + id: preview |
| 34 | + run: | |
| 35 | + OUTPUT=$(npx netlify-cli deploy --dir=public --json) |
| 36 | + DEPLOY_URL=$(echo "$OUTPUT" | jq -r '.deploy_url') |
| 37 | + echo "deploy_url=$DEPLOY_URL" >> "$GITHUB_OUTPUT" |
| 38 | + env: |
| 39 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 40 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
| 41 | + |
| 42 | + - name: Update PR description with preview URL |
| 43 | + if: github.event_name == 'pull_request' |
| 44 | + uses: actions/github-script@v7 |
| 45 | + with: |
| 46 | + script: | |
| 47 | + const url = '${{ steps.preview.outputs.deploy_url }}'; |
| 48 | + const marker = '<!-- netlify-preview -->'; |
| 49 | + const previewBlock = `${marker}\n---\n**Deploy Preview:** ${url}`; |
| 50 | +
|
| 51 | + const { data: pr } = await github.rest.pulls.get({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + pull_number: context.issue.number, |
| 55 | + }); |
| 56 | +
|
| 57 | + let body = pr.body || ''; |
| 58 | +
|
| 59 | + if (body.includes(marker)) { |
| 60 | + // Replace existing preview block |
| 61 | + body = body.replace( |
| 62 | + new RegExp(`${marker}[\\s\\S]*$`), |
| 63 | + previewBlock |
| 64 | + ); |
| 65 | + } else { |
| 66 | + // Append preview block |
| 67 | + body = body.trimEnd() + '\n\n' + previewBlock; |
| 68 | + } |
| 69 | +
|
| 70 | + await github.rest.pulls.update({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + pull_number: context.issue.number, |
| 74 | + body, |
| 75 | + }); |
0 commit comments