|
1 | | -name: Build and Preview Site |
| 1 | +name: PR Preview Deployment (Gatsby + GitHub Pages) |
| 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: |
| 17 | + preview: |
9 | 18 | runs-on: ubuntu-latest |
| 19 | + |
10 | 20 | steps: |
11 | | - - name: Checkout 🛎️ |
12 | | - uses: actions/checkout@master |
| 21 | + |
| 22 | + - name: Checkout PR |
| 23 | + if: github.event.action != 'closed' |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + ref: ${{ github.event.pull_request.head.sha }} |
| 27 | + |
| 28 | + - name: Checkout gh-pages for cleanup |
| 29 | + if: github.event.action == 'closed' |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + ref: gh-pages |
| 33 | + |
| 34 | + - name: Setup Node |
| 35 | + if: github.event.action != 'closed' |
| 36 | + uses: actions/setup-node@v4 |
| 37 | + with: |
| 38 | + node-version: "20" |
| 39 | + |
| 40 | + - name: Install dependencies |
| 41 | + if: github.event.action != 'closed' |
| 42 | + run: npm ci |
| 43 | + |
| 44 | + - name: Set PATH_PREFIX for preview |
| 45 | + if: github.event.action != 'closed' |
| 46 | + run: | |
| 47 | + echo "PATH_PREFIX=pr-preview/pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV |
| 48 | +
|
| 49 | + - name: Build PR preview |
| 50 | + if: github.event.action != 'closed' |
| 51 | + env: |
| 52 | + GATSBY_PREVIEW: "true" |
| 53 | + GATSBY_SITE_URL: https://${{ github.repository_owner }}.github.io |
| 54 | + run: | |
| 55 | + PATH_PREFIX=$PATH_PREFIX npm run build:preview |
| 56 | +
|
| 57 | + # Prevent indexing |
| 58 | + echo -e "User-agent: *\nDisallow: /" > public/robots.txt |
| 59 | +
|
| 60 | + - name: Deploy PR preview |
| 61 | + if: github.event.action != 'closed' |
| 62 | + |
13 | 63 | 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 |
| 64 | + source-dir: ./public |
| 65 | + preview-branch: gh-pages |
| 66 | + umbrella-dir: pr-preview |
| 67 | + action: auto |
| 68 | + comment: false |
| 69 | + |
| 70 | + - name: Comment PR with Preview URL |
| 71 | + if: github.event.action != 'closed' |
| 72 | + uses: marocchino/sticky-pull-request-comment@v2 |
32 | 73 | with: |
33 | | - target: ./public/**/*.html |
| 74 | + header: pr-preview |
| 75 | + message: | |
| 76 | + 🚀 **Preview for PR #${{ github.event.pull_request.number }}** |
34 | 77 |
|
35 | | - - name: Zip Site |
36 | | - run: bash ./script.sh |
| 78 | + 🌐 https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/ |
37 | 79 |
|
38 | | - - name: Upload files |
39 | | - uses: actions/upload-artifact@v6 |
| 80 | + - name: Cleanup PR preview |
| 81 | + if: github.event.action == 'closed' |
| 82 | + |
40 | 83 | 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" |
| 84 | + preview-branch: gh-pages |
| 85 | + umbrella-dir: pr-preview |
| 86 | + action: remove |
0 commit comments