Repository navigation
Add password-gated hosting guide for Georgie's Game Nights #466
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: PR preview | |
| on: | |
| pull_request: | |
| types: | |
| - opened | |
| - reopened | |
| - synchronize | |
| - closed | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| # Shares a per-PR group with the PR screenshots workflow, since both write to | |
| # previews/pr-<number>/. Production and other PRs use separate groups and rely | |
| # on non-force pushes with retries. | |
| concurrency: | |
| group: gh-pages-pr-${{ github.event.pull_request.number }} | |
| cancel-in-progress: false | |
| jobs: | |
| deploy: | |
| if: >- | |
| github.event.action != 'closed' && | |
| github.event.pull_request.head.repo.full_name == github.repository | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v5 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v5 | |
| with: | |
| node-version: 20 | |
| cache: npm | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build preview | |
| env: | |
| PREVIEW_PATH: /previews/pr-${{ github.event.pull_request.number }}/ | |
| run: | | |
| rm -rf docs | |
| npx vite build --base="$PREVIEW_PATH" | |
| cp docs/index.html docs/404.html | |
| # The deploy action never deletes files named CNAME or .nojekyll, | |
| # so shipping them into a preview folder would make close-cleanup | |
| # leave a permanent stale directory behind. They belong only at | |
| # the gh-pages root, which the production deploy provides. | |
| rm -f docs/CNAME docs/.nojekyll | |
| - name: Deploy preview | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| folder: docs | |
| branch: gh-pages | |
| target-folder: previews/pr-${{ github.event.pull_request.number }} | |
| clean: true | |
| # The PR screenshots workflow publishes into screenshots/ inside this | |
| # preview folder; preview redeploys must not delete it. The cleanup | |
| # job below intentionally has no exclude so PR close removes it. | |
| clean-exclude: screenshots/** | |
| force: false | |
| attempt-limit: 10 | |
| - name: Add preview link to pull request | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const marker = '<!-- pr-preview -->'; | |
| const previewUrl = `https://leoncheng.dev/previews/pr-${context.issue.number}/`; | |
| const shortSha = context.payload.pull_request.head.sha.slice(0, 7); | |
| const now = new Date(); | |
| const updatedUtc = now.toISOString().replace('T', ' ').slice(0, 16); | |
| const easternParts = new Intl.DateTimeFormat('en-CA', { | |
| timeZone: 'America/New_York', | |
| year: 'numeric', month: '2-digit', day: '2-digit', | |
| hour: '2-digit', minute: '2-digit', hour12: false, | |
| timeZoneName: 'short', | |
| }).formatToParts(now); | |
| const part = (type) => easternParts.find((p) => p.type === type).value; | |
| const updatedEastern = `${part('year')}-${part('month')}-${part('day')} ${part('hour')}:${part('minute')} ${part('timeZoneName')}`; | |
| const body = [ | |
| marker, | |
| `PR preview: ${previewUrl}`, | |
| '', | |
| `_Deployed at ${shortSha}, updated ${updatedEastern} / ${updatedUtc} UTC. This preview refreshes on every push and is removed when the pull request closes._`, | |
| ].join('\n'); | |
| const comments = await github.paginate(github.rest.issues.listComments, { | |
| ...context.repo, | |
| issue_number: context.issue.number, | |
| per_page: 100, | |
| }); | |
| const existing = comments.find((comment) => comment.body?.includes(marker)); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| ...context.repo, | |
| comment_id: existing.id, | |
| body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| ...context.repo, | |
| issue_number: context.issue.number, | |
| body, | |
| }); | |
| } | |
| cleanup: | |
| if: >- | |
| github.event.action == 'closed' && | |
| github.event.pull_request.head.repo.full_name == github.repository | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v5 | |
| - name: Create empty deployment directory | |
| run: mkdir preview-cleanup | |
| - name: Remove preview | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| folder: preview-cleanup | |
| branch: gh-pages | |
| target-folder: previews/pr-${{ github.event.pull_request.number }} | |
| clean: true | |
| force: false | |
| attempt-limit: 10 |