-
Notifications
You must be signed in to change notification settings - Fork 457
143 lines (123 loc) · 5.09 KB
/
cloudflare-preview.yml
File metadata and controls
143 lines (123 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 }}
# For fork PRs that are manually triggered, we need to get the PR details first
- name: Get PR details for fork
if: ${{ github.event_name == 'workflow_dispatch' }}
id: pr_details
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ inputs.pull_request_number }};
// Get PR details to find the head SHA
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
console.log(`Fork PR head SHA: ${pr.head.sha}`);
console.log(`Fork PR head ref: ${pr.head.ref}`);
console.log(`Fork PR repo: ${pr.head.repo.full_name}`);
// Set outputs for checkout step
core.setOutput('head_sha', pr.head.sha);
core.setOutput('head_ref', pr.head.ref);
core.setOutput('repo_full_name', pr.head.repo.full_name);
- name: Checkout for manually triggered fork PR
if: ${{ github.event_name == 'workflow_dispatch' }}
uses: actions/checkout@v4
with:
ref: ${{ steps.pr_details.outputs.head_sha }}
repository: ${{ steps.pr_details.outputs.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 rdoc
- 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:
github-token: ${{ secrets.MATZBOT_GITHUB_TOKEN }}
script: |
const prNumber = ${{ env.PR_NUMBER }};
const url = "${{ steps.deploy.outputs.deployment-url }}";
const commentMarker = "🚀 Preview deployment available at:";
// Get commit SHA based on event type
let commitSha;
if ('${{ github.event_name }}' === 'pull_request') {
commitSha = '${{ github.event.pull_request.head.sha }}';
} else {
// For workflow_dispatch, get the SHA from the PR details
commitSha = '${{ steps.pr_details.outputs.head_sha }}';
}
// Get all comments on the PR
const comments = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
// Look for our previous bot comment
const existingComment = comments.data.find(comment =>
comment.body.includes(commentMarker)
);
const commentBody = `${commentMarker} [${url}](${url}) (commit: ${commitSha})`;
if (existingComment) {
// Update existing comment
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
console.log("Updated existing preview comment");
} else {
// Create new comment
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody
});
console.log("Created new preview comment");
}