Skip to content

Commit e2a5443

Browse files
committed
Improve preview building workflows
1 parent 552d1c4 commit e2a5443

File tree

3 files changed

+91
-66
lines changed

3 files changed

+91
-66
lines changed

.github/workflows/cloudflare-preview.yml

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
11
name: Build and Deploy Cloudflare Preview
22

33
on:
4-
workflow_call:
5-
inputs:
6-
pr_number:
7-
description: 'The pull request number'
8-
required: true
9-
type: string
10-
pr_head_sha:
11-
description: 'The SHA of the PR head commit'
12-
required: true
13-
type: string
14-
pr_checkout_repository:
15-
description: 'The repository to checkout (owner/repo)'
16-
required: true
17-
type: string
18-
secrets:
19-
cloudflare_api_token:
20-
description: 'Cloudflare API Token'
21-
required: true
22-
cloudflare_account_id:
23-
description: 'Cloudflare Account ID'
24-
required: true
25-
matzbot_github_token:
26-
description: 'GitHub Token for Matzbot'
27-
required: true
4+
repository_dispatch:
5+
types: [pr-preview-deploy]
286

297
permissions:
308
pull-requests: write # To allow commenting on the PR
@@ -37,8 +15,8 @@ jobs:
3715
- name: Checkout PR Code
3816
uses: actions/checkout@v4
3917
with:
40-
repository: ${{ inputs.pr_checkout_repository }}
41-
ref: ${{ inputs.pr_head_sha }}
18+
repository: ${{ github.event.client_payload.pr_checkout_repository }}
19+
ref: ${{ github.event.client_payload.pr_head_sha }}
4220

4321
- name: Setup Ruby
4422
uses: ruby/setup-ruby@v1
@@ -53,19 +31,19 @@ jobs:
5331
id: deploy
5432
uses: cloudflare/wrangler-action@v3
5533
with:
56-
apiToken: ${{ secrets.cloudflare_api_token }}
57-
accountId: ${{ secrets.cloudflare_account_id }}
58-
command: pages deploy ./_site --project-name=rdoc --branch="${{ inputs.pr_number }}-preview"
34+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
35+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
36+
command: pages deploy ./_site --project-name=rdoc --branch="${{ github.event.client_payload.pr_number }}-preview"
5937

6038
- name: Comment on PR with preview URL
6139
uses: actions/github-script@v7
6240
with:
63-
github-token: ${{ secrets.matzbot_github_token }}
41+
github-token: ${{ secrets.MATZBOT_GITHUB_TOKEN }}
6442
script: |
65-
const prNumber = ${{ inputs.pr_number }};
43+
const prNumber = ${{ github.event.client_payload.pr_number }};
6644
const url = "${{ steps.deploy.outputs.deployment-url }}";
6745
const commentMarker = "🚀 Preview deployment available at:";
68-
const commitSha = '${{ inputs.pr_head_sha }}';
46+
const commitSha = '${{ github.event.client_payload.pr_head_sha }}';
6947
7048
const comments = await github.rest.issues.listComments({
7149
issue_number: prNumber,
@@ -96,4 +74,4 @@ jobs:
9674
body: commentBody
9775
});
9876
console.log("Created new preview comment");
99-
}
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Dispatch Fork PR Preview Deployment
2+
3+
on:
4+
workflow_run:
5+
workflows: ["PR Preview Check"]
6+
types: [completed]
7+
8+
jobs:
9+
deploy-fork:
10+
name: Trigger Preview Build and Deploy (Fork)
11+
runs-on: ubuntu-latest
12+
if: |
13+
github.event.workflow_run.conclusion == 'success' &&
14+
github.event.workflow_run.event == 'pull_request'
15+
steps:
16+
- name: Check and deploy approved fork PR
17+
uses: actions/github-script@v7
18+
with:
19+
script: |
20+
// Get the PR that triggered the workflow
21+
const pr = context.payload.workflow_run.pull_requests[0];
22+
if (!pr) {
23+
core.setFailed('No PR found in workflow run');
24+
return;
25+
}
26+
27+
// Check if this was a fork PR by checking if approve-fork job ran
28+
const jobs = await github.rest.actions.listJobsForWorkflowRun({
29+
owner: context.repo.owner,
30+
repo: context.repo.repo,
31+
run_id: context.payload.workflow_run.id,
32+
});
33+
34+
const approveJob = jobs.data.jobs.find(job => job.name === 'Approve Fork PR');
35+
if (!approveJob || approveJob.conclusion !== 'success') {
36+
core.setFailed('Not a fork PR approval workflow run');
37+
return;
38+
}
39+
40+
console.log(`Deploying approved fork PR #${pr.number}`);
41+
42+
// Trigger deployment via repository dispatch
43+
await github.rest.repos.createDispatchEvent({
44+
owner: context.repo.owner,
45+
repo: context.repo.repo,
46+
event_type: 'pr-preview-deploy',
47+
client_payload: {
48+
pr_number: String(pr.number),
49+
pr_head_sha: pr.head.sha,
50+
pr_checkout_repository: pr.head.repo.full_name,
51+
is_fork: 'true'
52+
}
53+
});
Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
1-
name: PR Preview Check and Trigger
1+
name: PR Preview Check
22

33
on:
44
pull_request:
55

66
jobs:
7-
# For PRs from the main repo - direct call to the shared workflow
8-
trigger-main-repo-preview:
9-
name: Trigger Preview (Main Repo)
10-
uses: ./.github/workflows/cloudflare-preview.yml
7+
# Deploy main repo PRs directly
8+
deploy-for-main:
9+
name: Trigger Preview Build and Deploy (Main Repo)
10+
runs-on: ubuntu-latest
1111
if: github.event.pull_request.head.repo.fork == false
12-
with:
13-
pr_number: ${{ github.event.pull_request.number }}
14-
pr_head_sha: ${{ github.event.pull_request.head.sha }}
15-
pr_checkout_repository: ${{ github.repository }}
16-
secrets:
17-
cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
18-
cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
19-
matzbot_github_token: ${{ secrets.MATZBOT_GITHUB_TOKEN }}
12+
steps:
13+
- name: Trigger preview deployment
14+
uses: actions/github-script@v7
15+
with:
16+
github-token: ${{ secrets.GITHUB_TOKEN }}
17+
script: |
18+
await github.rest.repos.createDispatchEvent({
19+
owner: context.repo.owner,
20+
repo: context.repo.repo,
21+
event_type: 'pr-preview-deploy',
22+
client_payload: {
23+
pr_number: '${{ github.event.pull_request.number }}',
24+
pr_head_sha: '${{ github.event.pull_request.head.sha }}',
25+
pr_checkout_repository: '${{ github.repository }}',
26+
is_fork: 'false'
27+
}
28+
});
29+
console.log('Triggered main repo preview deployment');
2030
21-
# For fork PRs - this job requires approval
22-
wait-for-approval:
23-
name: Wait for Approval (Fork PR)
31+
# Approval gate for fork PRs
32+
approve-for-fork:
33+
name: Approve Fork PR
2434
runs-on: ubuntu-latest
2535
if: github.event.pull_request.head.repo.fork == true
2636
environment: fork-preview-protection
27-
# This job only serves as an approval gate - it doesn't do anything else
2837
steps:
29-
- run: echo "Approval granted. Proceeding with preview deployment for commit ${{ github.event.pull_request.head.sha }}."
30-
31-
# Once approval is granted, call the shared workflow
32-
trigger-fork-preview:
33-
name: Trigger Preview (Fork - After Approval)
34-
needs: wait-for-approval
35-
uses: ./.github/workflows/cloudflare-preview.yml
36-
if: github.event.pull_request.head.repo.fork == true
37-
with:
38-
pr_number: ${{ github.event.pull_request.number }}
39-
pr_head_sha: ${{ github.event.pull_request.head.sha }}
40-
pr_checkout_repository: ${{ github.event.pull_request.head.repo.full_name }}
41-
secrets:
42-
cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }}
43-
cloudflare_account_id: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
44-
matzbot_github_token: ${{ secrets.MATZBOT_GITHUB_TOKEN }}
38+
- run: echo "Fork PR ${{ github.event.pull_request.number }} approved for preview deployment"

0 commit comments

Comments
 (0)