Skip to content

Commit 3c19909

Browse files
committed
Use repository_dispatch instead
1 parent 552d1c4 commit 3c19909

File tree

2 files changed

+47
-69
lines changed

2 files changed

+47
-69
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+
}

.github/workflows/pr-preview-check.yml

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,41 @@ 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
11-
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 }}
20-
21-
# For fork PRs - this job requires approval
22-
wait-for-approval:
23-
name: Wait for Approval (Fork PR)
7+
# Single job that handles both main repo and fork PRs
8+
preview-trigger:
9+
name: Trigger Preview Deployment
2410
runs-on: ubuntu-latest
25-
if: github.event.pull_request.head.repo.fork == true
26-
environment: fork-preview-protection
27-
# This job only serves as an approval gate - it doesn't do anything else
11+
# For fork PRs, require approval first
12+
environment: ${{ github.event.pull_request.head.repo.fork == true && 'fork-preview-protection' || '' }}
2813
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 }}
14+
- name: Prepare deployment info
15+
id: prep
16+
run: |
17+
echo "is_fork=${{ github.event.pull_request.head.repo.fork }}" >> $GITHUB_OUTPUT
18+
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
19+
echo "pr_head_sha=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
20+
if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then
21+
echo "pr_checkout_repository=${{ github.event.pull_request.head.repo.full_name }}" >> $GITHUB_OUTPUT
22+
else
23+
echo "pr_checkout_repository=${{ github.repository }}" >> $GITHUB_OUTPUT
24+
fi
25+
26+
- name: Trigger preview deployment
27+
uses: actions/github-script@v7
28+
with:
29+
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
script: |
31+
const is_fork = '${{ steps.prep.outputs.is_fork }}' === 'true';
32+
console.log(`Triggering preview deployment for ${is_fork ? 'fork' : 'main repo'} PR #${{ steps.prep.outputs.pr_number }}`);
33+
34+
await github.rest.repos.createDispatchEvent({
35+
owner: context.repo.owner,
36+
repo: context.repo.repo,
37+
event_type: 'pr-preview-deploy',
38+
client_payload: {
39+
pr_number: '${{ steps.prep.outputs.pr_number }}',
40+
pr_head_sha: '${{ steps.prep.outputs.pr_head_sha }}',
41+
pr_checkout_repository: '${{ steps.prep.outputs.pr_checkout_repository }}',
42+
is_fork: '${{ steps.prep.outputs.is_fork }}'
43+
}
44+
});

0 commit comments

Comments
 (0)