From 29f1c9b69a0d453b7f499b41d7498a285f450717 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Mar 2026 05:59:35 +0000 Subject: [PATCH] ci: add PR preview deployment workflow for Cloudflare Pages Automatically deploys a preview to Cloudflare Pages on every PR open/update, using a unique branch name (pr-) to generate a dedicated preview URL. Posts and updates a comment on the PR with the deployment URL and commit SHA. --- .github/workflows/pr-preview.yml | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 .github/workflows/pr-preview.yml diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 0000000..73ddce4 --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -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('') + ); + + const body = ` + ### 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, + }); + }