Skip to content

Project overhaul

Project overhaul #34

Workflow file for this run

name: PR Preview Deploy
on:
pull_request:
types: [opened, synchronize, reopened, closed]
# Allow one concurrent deployment per PR
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
deploy-preview:
name: Deploy PR Preview
runs-on: ubuntu-latest
if: github.event.action != 'closed'
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Install dependencies
run: bun install
- name: Build demo for PR preview
run: bun run build:demo
env:
PUBLIC_URL: /react-gauge-component/pr-${{ github.event.pull_request.number }}
CI: false
- name: Deploy to GitHub Pages subdirectory
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
destination_dir: pr-${{ github.event.pull_request.number }}
keep_files: true
- name: Comment PR with preview URL
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const previewUrl = `https://antoniolago.github.io/react-gauge-component/pr-${prNumber}/`;
// Check if we already commented
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
// Find existing bot comment by login name (more reliable than type)
const botComment = comments.data.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('PR Preview deployed')
);
const body = `🚀 **PR Preview deployed!**\n\n` +
`Preview URL: ${previewUrl}\n\n` +
`_This preview will be updated on each push to this PR._`;
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body,
});
}
# Cleanup preview when PR is closed
cleanup-preview:
name: Cleanup PR Preview
runs-on: ubuntu-latest
if: github.event.action == 'closed'
permissions:
contents: write
steps:
- name: Checkout gh-pages branch
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Remove PR preview directory
run: |
PR_DIR="pr-${{ github.event.pull_request.number }}"
if [ -d "$PR_DIR" ]; then
rm -rf "$PR_DIR"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "Remove preview for PR #${{ github.event.pull_request.number }}" || echo "Nothing to commit"
git push
fi