Skip to content

Commit f359730

Browse files
committed
Add a new workflow to deploy page preview
1 parent 8417536 commit f359730

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Cloudflare Pages Preview Deployment
2+
3+
on:
4+
# Runs automatically for PRs from ruby/rdoc
5+
# Fork PRs will be filtered out by the if condition
6+
pull_request:
7+
8+
# Allows manual triggering for fork PRs
9+
workflow_dispatch:
10+
inputs:
11+
pull_request_number:
12+
description: 'Pull Request Number (for fork PRs)'
13+
required: true
14+
type: string
15+
16+
jobs:
17+
deploy-preview:
18+
runs-on: ubuntu-latest
19+
# Skip if PR from fork and NOT manually triggered
20+
if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == 'ruby/rdoc' }}
21+
22+
steps:
23+
- name: Checkout for PR from main repo
24+
if: ${{ github.event_name == 'pull_request' }}
25+
uses: actions/checkout@v4
26+
with:
27+
ref: ${{ github.event.pull_request.head.ref }}
28+
29+
- name: Checkout for manually triggered fork PR
30+
if: ${{ github.event_name == 'workflow_dispatch' }}
31+
uses: actions/checkout@v4
32+
with:
33+
ref: ${{ github.event.pull_request.head.ref }}
34+
repository: ${{ github.event.pull_request.head.repo.full_name }}
35+
36+
- name: Setup Ruby
37+
uses: ruby/setup-ruby@v1
38+
with:
39+
ruby-version: '3.4'
40+
bundler-cache: true
41+
42+
- name: Install dependencies
43+
run: bundle install
44+
45+
- name: Build site
46+
run: bundle exec rake build
47+
48+
- name: Set PR Number
49+
id: pr_number
50+
run: |
51+
if [ "${{ github.event_name }}" == "pull_request" ]; then
52+
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
53+
else
54+
echo "PR_NUMBER=${{ inputs.pull_request_number }}" >> $GITHUB_ENV
55+
fi
56+
57+
# Deploy to Cloudflare Pages using wrangler-action
58+
- name: Deploy to Cloudflare Pages
59+
id: deploy
60+
uses: cloudflare/wrangler-action@v3
61+
with:
62+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
63+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
64+
command: pages deploy ./_site --project-name=rdoc --branch="${{ env.PR_NUMBER }}-preview"
65+
66+
# Comment on PR with preview URL - works for both regular PRs and fork PRs
67+
- name: Comment on PR with preview URL
68+
uses: actions/github-script@v7
69+
with:
70+
script: |
71+
const prNumber = ${{ env.PR_NUMBER }};
72+
const url = "${{ steps.deploy.outputs.deployment-url }}";
73+
74+
await github.rest.issues.createComment({
75+
issue_number: prNumber,
76+
owner: context.repo.owner,
77+
repo: context.repo.repo,
78+
body: `🚀 Preview deployment available at: [${url}](${url})`
79+
});

0 commit comments

Comments
 (0)