Skip to content

Merge pull request #1 from nicepkg/dev #2

Merge pull request #1 from nicepkg/dev

Merge pull request #1 from nicepkg/dev #2

name: Deploy Website to Cloudflare Pages
on:
push:
branches:
- main
paths:
# NOTE: Update this path to match your WEBSITE_DIR (e.g., 'website/**')
- 'website/**'
- '.github/workflows/deploy-website.yml'
pull_request:
branches:
- main
paths:
# NOTE: Update this path to match your WEBSITE_DIR (e.g., 'website/**')
- 'website/**'
workflow_dispatch:
concurrency:
group: deploy-website-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
# Project Configuration - Update these values for your project
PROJECT_NAME: 'vsync'
SITE_DOMAIN: 'vsync.xiaominglab.com'
WEBSITE_DIR: 'website'
BUILD_OUTPUT_DIR: 'website/out'
BUILD_COMMAND: 'pnpm build:website'
# Comment marker (used to update existing PR comment instead of spamming)
PREVIEW_COMMENT_MARKER: '<!-- CF_PAGES_PREVIEW_COMMENT -->'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
- name: Build website
run: ${{ env.BUILD_COMMAND }}
env:
NEXT_PUBLIC_SITE_URL: https://${{ env.SITE_DOMAIN }}
NEXT_PUBLIC_GIT_SHA: ${{ github.sha }}
SKIP_ENV_VALIDATION: true
- name: Upload build artifact
uses: actions/upload-artifact@v6
with:
name: website-build
path: ${{ env.BUILD_OUTPUT_DIR }}
retention-days: 1
deploy:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
permissions:
contents: read
deployments: write
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
with:
install-dependencies: 'false'
setup-cache: 'false'
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: website-build
path: ${{ env.BUILD_OUTPUT_DIR }}
- name: Install wrangler
run: pnpm install -g wrangler
- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v3
with:
packageManager: pnpm
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy ${{ env.BUILD_OUTPUT_DIR }} --project-name=${{ env.PROJECT_NAME }} --commit-dirty=true
preview:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'pull_request'
permissions:
contents: read
deployments: write
pull-requests: write
issues: write
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Node.js and pnpm
uses: ./.github/actions/setup-node-pnpm
with:
install-dependencies: 'false'
setup-cache: 'false'
- name: Download build artifact
uses: actions/download-artifact@v7
with:
name: website-build
path: ${{ env.BUILD_OUTPUT_DIR }}
- name: Install wrangler
run: pnpm install -g wrangler
- name: Deploy Preview to Cloudflare Pages
id: deploy
uses: cloudflare/wrangler-action@v3
with:
packageManager: pnpm
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy ${{ env.BUILD_OUTPUT_DIR }} --project-name=${{ env.PROJECT_NAME }} --branch=${{ github.head_ref }} --commit-dirty=true
- name: Upsert PR comment with preview URL (no spam)
uses: actions/github-script@v8
env:
MARKER: ${{ env.PREVIEW_COMMENT_MARKER }}
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
# If your wrangler-action provides it, you can switch to this:
# ALIAS_URL: ${{ steps.deploy.outputs.pages-deployment-alias-url }}
with:
script: |
const marker = process.env.MARKER;
const url = process.env.DEPLOYMENT_URL;
const body = `${marker}
## 🚀 Preview Deployment Ready!
Preview URL: ${url}
- Branch: \`${context.payload.pull_request.head.ref}\`
- Commit: \`${context.sha}\`
(This comment will be updated on new pushes.)
`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
// List recent comments and find the one with our marker
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find(c => (c.body || '').includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}