chore: replace peaceiris/actions-gh-pages and dorny/paths-filter with native git/shell #159
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, synchronize, reopened] | |
| # Serialize gh-pages pushes per PR; cancel in-progress runs when a new push arrives. | |
| concurrency: | |
| group: "pages-preview-${{ github.event.number }}" | |
| cancel-in-progress: true | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| preview: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | |
| - uses: ./.github/actions/setup | |
| - name: Build all generated files | |
| run: npm run build | |
| - name: Install Playwright browsers | |
| run: npx playwright install --with-deps chromium | |
| - name: Capture desktop and mobile screenshots | |
| run: npm run screenshots | |
| - name: Deploy PR preview to gh-pages branch | |
| uses: ./.github/actions/gh-pages-deploy | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: ./ | |
| destination_dir: previews/pr-${{ github.event.number }} | |
| commit_message: "chore: deploy preview for PR #${{ github.event.number }}" | |
| exclude_assets: | | |
| .git | |
| .github | |
| .gitignore | |
| node_modules | |
| tests | |
| scripts | |
| package-lock.json | |
| package.json | |
| milestones.yaml | |
| project-stats.yaml | |
| screenshots | |
| - name: Upload screenshots to GitHub | |
| id: upload-screenshots | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const prNumber = context.payload.pull_request.number; | |
| const branch = 'gh-pages'; | |
| const owner = context.repo.owner; | |
| const repo = context.repo.repo; | |
| for (const name of ['desktop', 'mobile']) { | |
| const localPath = `screenshots/${name}.png`; | |
| const ghPath = `previews/pr-${prNumber}/screenshots/${name}.png`; | |
| const content = fs.readFileSync(localPath, { encoding: 'base64' }); | |
| let sha; | |
| try { | |
| const { data } = await github.rest.repos.getContent({ | |
| owner, repo, path: ghPath, ref: branch, | |
| }); | |
| sha = data.sha; | |
| } catch (err) { | |
| // A 404 means the file doesn't exist yet — that's expected on the | |
| // first run. Re-throw anything else (auth errors, network failures). | |
| if (err.status !== 404) throw err; | |
| } | |
| await github.rest.repos.createOrUpdateFileContents({ | |
| owner, repo, | |
| path: ghPath, | |
| message: `chore: update ${name} screenshot for PR #${prNumber}`, | |
| content, | |
| branch, | |
| ...(sha ? { sha } : {}), | |
| }); | |
| core.setOutput(`${name}_url`, | |
| `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${ghPath}`); | |
| } | |
| - name: Post or update preview URL comment | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| env: | |
| DESKTOP_URL: ${{ steps.upload-screenshots.outputs.desktop_url }} | |
| MOBILE_URL: ${{ steps.upload-screenshots.outputs.mobile_url }} | |
| with: | |
| script: | | |
| const prNumber = context.payload.pull_request.number; | |
| const sha = context.payload.pull_request.head.sha.slice(0, 7); | |
| const url = `https://nitrocode.github.io/token-deathclock/previews/pr-${prNumber}/`; | |
| const desktopUrl = process.env.DESKTOP_URL; | |
| const mobileUrl = process.env.MOBILE_URL; | |
| const body = [ | |
| '## 👁️ PR Preview', | |
| '', | |
| `🚀 **[Open Preview](${url})**`, | |
| '', | |
| `> Deployed from commit \`${sha}\` · Updates on every push to this PR`, | |
| '> _(Preview is removed automatically when the PR is closed.)_', | |
| '', | |
| '### Screenshots', | |
| '', | |
| '| Desktop | Mobile |', | |
| '|:-------:|:------:|', | |
| `|  |  |`, | |
| ].join('\n'); | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const existing = comments.find(c => | |
| c.user.type === 'Bot' && c.body.includes('## 👁️ PR Preview') | |
| ); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existing.id, | |
| body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body, | |
| }); | |
| } |