|
1 | 1 | name: Build and Preview Site |
| 2 | + |
2 | 3 | on: |
3 | | - pull_request: |
| 4 | + pull_request_target: |
4 | 5 | branches: [master] |
5 | | - types: [opened, synchronize, reopened] |
| 6 | + types: [opened, synchronize, reopened, closed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: "preview-${{ github.event.pull_request.number || github.run_id }}" |
| 14 | + cancel-in-progress: true |
6 | 15 |
|
7 | 16 | jobs: |
8 | | - site-preview: |
9 | | - runs-on: ubuntu-latest |
| 17 | + build-and-deploy-preview: |
| 18 | + runs-on: ubuntu-24.04 |
10 | 19 | steps: |
11 | | - - name: Checkout 🛎️ |
12 | | - uses: actions/checkout@master |
| 20 | + - name: Checkout PR |
| 21 | + if: github.event.action != 'closed' |
| 22 | + uses: actions/checkout@v6 |
| 23 | + with: |
| 24 | + ref: ${{ github.event.pull_request.head.sha }} |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Checkout for cleanup |
| 28 | + if: github.event.action == 'closed' |
| 29 | + uses: actions/checkout@v6 |
| 30 | + with: |
| 31 | + ref: gh-pages |
| 32 | + fetch-depth: 0 |
| 33 | + |
| 34 | + - name: Setup Node |
| 35 | + if: github.event.action != 'closed' |
| 36 | + uses: actions/setup-node@v4 |
13 | 37 | with: |
14 | | - persist-credentials: false |
15 | | - fetch-depth: 1 |
16 | | - |
17 | | - - name: Install 🔧 |
18 | | - run: make setup |
19 | | - |
20 | | - - name: Build 🏗️ |
21 | | - # Always run the full production build explicitly. |
22 | | - # Do NOT use `make clean` here — that target is a developer convenience |
23 | | - # command whose recipe has changed multiple times, causing blog posts and |
24 | | - # other collections to be silently excluded from the deployed site when |
25 | | - # it ran a lite/dev build instead of the full production build. |
26 | | - # `npm run build` is the canonical, stable production build command: |
27 | | - # cross-env BUILD_FULL_SITE=true gatsby build |
28 | | - run: npm run build |
29 | | - |
30 | | - - name: Broken Link Check 🔗 |
31 | | - uses: technote-space/broken-link-checker-action@v2 |
| 38 | + node-version: "20" |
| 39 | + |
| 40 | + - name: Install dependencies |
| 41 | + if: github.event.action != 'closed' |
| 42 | + run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" |
| 43 | + |
| 44 | + - name: Build PR preview |
| 45 | + if: github.event.action != 'closed' |
| 46 | + run: | |
| 47 | + npm run build |
| 48 | +
|
| 49 | + # Block all crawlers from indexing PR previews. |
| 50 | + cat > public/robots.txt <<'EOF' |
| 51 | + User-agent: * |
| 52 | + Disallow: / |
| 53 | + EOF |
| 54 | + touch public/.nojekyll |
| 55 | +
|
| 56 | + - name: Deploy PR preview |
| 57 | + if: github.event.action != 'closed' |
| 58 | + id: deploy-preview |
| 59 | + uses: rossjrw/pr-preview-action@v1.6.3 |
32 | 60 | with: |
33 | | - target: ./public/**/*.html |
| 61 | + source-dir: ./public |
| 62 | + preview-branch: gh-pages |
| 63 | + umbrella-dir: pr-preview |
| 64 | + action: auto |
| 65 | + comment: false |
| 66 | + |
| 67 | + - name: Comment PR with Preview URL |
| 68 | + if: github.event.action != 'closed' |
| 69 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 70 | + with: |
| 71 | + header: pr-preview |
| 72 | + message: | |
| 73 | + 🚀 **Preview deployment for PR #${{ github.event.pull_request.number }}** |
| 74 | +
|
| 75 | + 🌐 **Preview URL**: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/ |
34 | 76 |
|
35 | | - - name: Zip Site |
36 | | - run: bash ./script.sh |
| 77 | + _This preview will be updated automatically when you push new commits to this PR._ |
37 | 78 |
|
38 | | - - name: Upload files |
39 | | - uses: actions/upload-artifact@v6 |
| 79 | + - name: Cleanup PR preview on close |
| 80 | + if: github.event.action == 'closed' |
| 81 | + uses: rossjrw/pr-preview-action@v1.6.3 |
40 | 82 | with: |
41 | | - name: public-dir |
42 | | - path: ./public-dir.zip |
43 | | - retention-days: 1 |
44 | | - - name: Trigger Inner workflow |
45 | | - run: echo "triggering inner workflow" |
| 83 | + preview-branch: gh-pages |
| 84 | + umbrella-dir: pr-preview |
| 85 | + action: remove |
0 commit comments