1
- name : Cloudflare Pages Preview Deployment
1
+ name : Build and Deploy Cloudflare Preview
2
2
3
3
on :
4
- # Runs automatically for PRs from ruby/rdoc
5
- # Fork PRs will be filtered out by the if condition
6
- pull_request :
4
+ repository_dispatch :
5
+ types : [pr-preview-deploy]
7
6
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
7
+ permissions :
8
+ pull-requests : write # To allow commenting on the PR
15
9
16
10
jobs :
17
- deploy-preview :
11
+ build-deploy-and-comment :
12
+ name : Build, Deploy, and Comment
18
13
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
14
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
- # For fork PRs that are manually triggered, we need to get the PR details first
30
- - name : Get PR details for fork
31
- if : ${{ github.event_name == 'workflow_dispatch' }}
32
- id : pr_details
33
- uses : actions/github-script@v7
34
- with :
35
- script : |
36
- const prNumber = ${{ inputs.pull_request_number }};
37
-
38
- // Get PR details to find the head SHA
39
- const { data: pr } = await github.rest.pulls.get({
40
- owner: context.repo.owner,
41
- repo: context.repo.repo,
42
- pull_number: prNumber
43
- });
44
-
45
- console.log(`Fork PR head SHA: ${pr.head.sha}`);
46
- console.log(`Fork PR head ref: ${pr.head.ref}`);
47
- console.log(`Fork PR repo: ${pr.head.repo.full_name}`);
48
-
49
- // Set outputs for checkout step
50
- core.setOutput('head_sha', pr.head.sha);
51
- core.setOutput('head_ref', pr.head.ref);
52
- core.setOutput('repo_full_name', pr.head.repo.full_name);
53
-
54
- - name : Checkout for manually triggered fork PR
55
- if : ${{ github.event_name == 'workflow_dispatch' }}
15
+ - name : Checkout PR Code
56
16
uses : actions/checkout@v4
57
17
with :
58
- ref : ${{ steps.pr_details.outputs.head_sha }}
59
- repository : ${{ steps.pr_details.outputs.repo_full_name }}
18
+ repository : ${{ github.event.client_payload.pr_checkout_repository }}
19
+ ref : ${{ github.event.client_payload.pr_head_sha }}
60
20
61
21
- name : Setup Ruby
62
22
uses : ruby/setup-ruby@v1
63
23
with :
64
24
ruby-version : ' 3.4'
65
25
bundler-cache : true
66
26
67
- - name : Install dependencies
68
- run : bundle install
69
-
70
27
- name : Build site
71
28
run : bundle exec rake rdoc
72
29
73
- - name : Set PR Number
74
- id : pr_number
75
- run : |
76
- if [ "${{ github.event_name }}" == "pull_request" ]; then
77
- echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
78
- else
79
- echo "PR_NUMBER=${{ inputs.pull_request_number }}" >> $GITHUB_ENV
80
- fi
81
-
82
- # Deploy to Cloudflare Pages using wrangler-action
83
30
- name : Deploy to Cloudflare Pages
84
31
id : deploy
85
32
uses : cloudflare/wrangler-action@v3
86
33
with :
87
34
apiToken : ${{ secrets.CLOUDFLARE_API_TOKEN }}
88
35
accountId : ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
89
- command : pages deploy ./_site --project-name=rdoc --branch="${{ env.PR_NUMBER }}-preview"
36
+ command : pages deploy ./_site --project-name=rdoc --branch="${{ github.event.client_payload.pr_number }}-preview"
90
37
91
- # Comment on PR with preview URL - works for both regular PRs and fork PRs
92
38
- name : Comment on PR with preview URL
93
39
uses : actions/github-script@v7
94
40
with :
95
41
github-token : ${{ secrets.MATZBOT_GITHUB_TOKEN }}
96
42
script : |
97
- const prNumber = ${{ env.PR_NUMBER }};
43
+ const prNumber = ${{ github.event.client_payload.pr_number }};
98
44
const url = "${{ steps.deploy.outputs.deployment-url }}";
99
45
const commentMarker = "🚀 Preview deployment available at:";
46
+ const commitSha = '${{ github.event.client_payload.pr_head_sha }}';
100
47
101
- // Get commit SHA based on event type
102
- let commitSha;
103
- if ('${{ github.event_name }}' === 'pull_request') {
104
- commitSha = '${{ github.event.pull_request.head.sha }}';
105
- } else {
106
- // For workflow_dispatch, get the SHA from the PR details
107
- commitSha = '${{ steps.pr_details.outputs.head_sha }}';
108
- }
109
-
110
- // Get all comments on the PR
111
48
const comments = await github.rest.issues.listComments({
112
49
issue_number: prNumber,
113
50
owner: context.repo.owner,
114
51
repo: context.repo.repo,
115
52
per_page: 100
116
53
});
117
54
118
- // Look for our previous bot comment
119
55
const existingComment = comments.data.find(comment =>
120
56
comment.body.includes(commentMarker)
121
57
);
122
58
123
59
const commentBody = `${commentMarker} [${url}](${url}) (commit: ${commitSha})`;
124
60
125
61
if (existingComment) {
126
- // Update existing comment
127
62
await github.rest.issues.updateComment({
128
63
comment_id: existingComment.id,
129
64
owner: context.repo.owner,
@@ -132,12 +67,11 @@ jobs:
132
67
});
133
68
console.log("Updated existing preview comment");
134
69
} else {
135
- // Create new comment
136
70
await github.rest.issues.createComment({
137
71
issue_number: prNumber,
138
72
owner: context.repo.owner,
139
73
repo: context.repo.repo,
140
74
body: commentBody
141
75
});
142
76
console.log("Created new preview comment");
143
- }
77
+ }
0 commit comments