Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: PR Preview Deployment

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
preview:
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
pull-requests: write

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build
env:
VITE_CONVEX_URL: ${{ secrets.VITE_CONVEX_URL }}

- name: Deploy preview to Cloudflare Pages
id: deploy
run: |
BRANCH_NAME="pr-${{ github.event.number }}"
OUTPUT=$(npx wrangler pages deploy ./dist \
--project-name spacecraft \
--branch "$BRANCH_NAME" \
2>&1)
echo "$OUTPUT"
DEPLOY_URL=$(echo "$OUTPUT" | grep -oP 'https://[^\s]+\.pages\.dev' | tail -1)
echo "url=$DEPLOY_URL" >> $GITHUB_OUTPUT
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

- name: Comment PR with preview URL
uses: actions/github-script@v7
with:
script: |
const url = '${{ steps.deploy.outputs.url }}';
const prNumber = context.payload.pull_request.number;
const sha = context.payload.pull_request.head.sha.substring(0, 7);

// Buscar si ya existe un comentario de preview para actualizar
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});

const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('<!-- cf-preview -->')
);

const body = `<!-- cf-preview -->
### Preview deployment

| | |
|---|---|
| **URL** | ${url} |
| **Commit** | \`${sha}\` |
| **Status** | Ready |

> Deployment updated on every push to this PR.`;

if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}