From f359730bef683301ed62fa8adfcffcc00d091c34 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 11 May 2025 08:58:50 +0100 Subject: [PATCH 1/2] Add a new workflow to deploy page preview --- .github/workflows/cloudflare-preview.yml | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/cloudflare-preview.yml diff --git a/.github/workflows/cloudflare-preview.yml b/.github/workflows/cloudflare-preview.yml new file mode 100644 index 0000000000..0beded916c --- /dev/null +++ b/.github/workflows/cloudflare-preview.yml @@ -0,0 +1,79 @@ +name: Cloudflare Pages Preview Deployment + +on: + # Runs automatically for PRs from ruby/rdoc + # Fork PRs will be filtered out by the if condition + pull_request: + + # Allows manual triggering for fork PRs + workflow_dispatch: + inputs: + pull_request_number: + description: 'Pull Request Number (for fork PRs)' + required: true + type: string + +jobs: + deploy-preview: + runs-on: ubuntu-latest + # Skip if PR from fork and NOT manually triggered + if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == 'ruby/rdoc' }} + + steps: + - name: Checkout for PR from main repo + if: ${{ github.event_name == 'pull_request' }} + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + + - name: Checkout for manually triggered fork PR + if: ${{ github.event_name == 'workflow_dispatch' }} + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.4' + bundler-cache: true + + - name: Install dependencies + run: bundle install + + - name: Build site + run: bundle exec rake build + + - name: Set PR Number + id: pr_number + run: | + if [ "${{ github.event_name }}" == "pull_request" ]; then + echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV + else + echo "PR_NUMBER=${{ inputs.pull_request_number }}" >> $GITHUB_ENV + fi + + # Deploy to Cloudflare Pages using wrangler-action + - name: Deploy to Cloudflare Pages + id: deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: pages deploy ./_site --project-name=rdoc --branch="${{ env.PR_NUMBER }}-preview" + + # Comment on PR with preview URL - works for both regular PRs and fork PRs + - name: Comment on PR with preview URL + uses: actions/github-script@v7 + with: + script: | + const prNumber = ${{ env.PR_NUMBER }}; + const url = "${{ steps.deploy.outputs.deployment-url }}"; + + await github.rest.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: `🚀 Preview deployment available at: [${url}](${url})` + }); From 27fff3da31527379813b28755447f8d2f6c876fa Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 11 May 2025 09:18:21 +0100 Subject: [PATCH 2/2] Add workflow to comment on fork PRs The comment should allow maintainers to trigger a preview deployment for fork PRs more easily. --- .github/workflows/pr-preview-comment.yml | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/pr-preview-comment.yml diff --git a/.github/workflows/pr-preview-comment.yml b/.github/workflows/pr-preview-comment.yml new file mode 100644 index 0000000000..6fe0d3cc6a --- /dev/null +++ b/.github/workflows/pr-preview-comment.yml @@ -0,0 +1,37 @@ +name: Comment on Fork PRs + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + comment-on-fork-pr: + runs-on: ubuntu-latest + # Only run for fork PRs + if: github.event.pull_request.head.repo.fork == true + steps: + - name: Comment on PR with manual deployment instructions + uses: actions/github-script@v7 + with: + script: | + const prNumber = context.payload.pull_request.number; + const workflowUrl = `https://github.com/ruby/rdoc/actions/workflows/cloudflare-preview.yml`; + const branch = context.payload.pull_request.head.ref; + + // Create a direct link that pre-fills the PR number input + const dispatchUrl = `${workflowUrl}/dispatch?ref=main&inputs%5Bpull_request_number%5D=${prNumber}`; + + await github.rest.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: `## Cloudflare Preview Deployment + + ⚠️ This PR is from a fork, so the preview deployment workflow doesn't run automatically for security reasons. + + If you're a maintainer and want to preview this PR: + + [➡️ Click here to run the workflow with PR #${prNumber} pre-filled](${dispatchUrl}) + + This will trigger a Cloudflare Pages preview deployment for this PR.` + });