Skip to content

Commit dfffac6

Browse files
authored
Merge branch 'master' into fix-templates-location
2 parents 0959242 + fcfab0f commit dfffac6

File tree

5 files changed

+132
-153
lines changed

5 files changed

+132
-153
lines changed
Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,64 @@
1-
name: Cloudflare Pages Preview Deployment
1+
name: Build and Deploy Cloudflare Preview
22

33
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]
76

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
159

1610
jobs:
17-
deploy-preview:
11+
build-deploy-and-comment:
12+
name: Build, Deploy, and Comment
1813
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-
2214
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
5616
uses: actions/checkout@v4
5717
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 }}
6020

6121
- name: Setup Ruby
6222
uses: ruby/setup-ruby@v1
6323
with:
6424
ruby-version: '3.4'
6525
bundler-cache: true
6626

67-
- name: Install dependencies
68-
run: bundle install
69-
7027
- name: Build site
7128
run: bundle exec rake rdoc
7229

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
8330
- name: Deploy to Cloudflare Pages
8431
id: deploy
8532
uses: cloudflare/wrangler-action@v3
8633
with:
8734
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
8835
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"
9037

91-
# Comment on PR with preview URL - works for both regular PRs and fork PRs
9238
- name: Comment on PR with preview URL
9339
uses: actions/github-script@v7
9440
with:
9541
github-token: ${{ secrets.MATZBOT_GITHUB_TOKEN }}
9642
script: |
97-
const prNumber = ${{ env.PR_NUMBER }};
43+
const prNumber = ${{ github.event.client_payload.pr_number }};
9844
const url = "${{ steps.deploy.outputs.deployment-url }}";
9945
const commentMarker = "🚀 Preview deployment available at:";
46+
const commitSha = '${{ github.event.client_payload.pr_head_sha }}';
10047
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
11148
const comments = await github.rest.issues.listComments({
11249
issue_number: prNumber,
11350
owner: context.repo.owner,
11451
repo: context.repo.repo,
11552
per_page: 100
11653
});
11754
118-
// Look for our previous bot comment
11955
const existingComment = comments.data.find(comment =>
12056
comment.body.includes(commentMarker)
12157
);
12258
12359
const commentBody = `${commentMarker} [${url}](${url}) (commit: ${commitSha})`;
12460
12561
if (existingComment) {
126-
// Update existing comment
12762
await github.rest.issues.updateComment({
12863
comment_id: existingComment.id,
12964
owner: context.repo.owner,
@@ -132,12 +67,11 @@ jobs:
13267
});
13368
console.log("Updated existing preview comment");
13469
} else {
135-
// Create new comment
13670
await github.rest.issues.createComment({
13771
issue_number: prNumber,
13872
owner: context.repo.owner,
13973
repo: context.repo.repo,
14074
body: commentBody
14175
});
14276
console.log("Created new preview comment");
143-
}
77+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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: Download PR information
17+
uses: actions/download-artifact@v4
18+
with:
19+
name: pr
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
21+
run-id: ${{ github.event.workflow_run.id }}
22+
23+
- name: Read PR information and trigger deployment
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
const fs = require('fs');
28+
29+
// Check if this was a fork PR by checking if approve-fork job ran
30+
const jobs = await github.rest.actions.listJobsForWorkflowRun({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
run_id: context.payload.workflow_run.id,
34+
});
35+
36+
const approveJob = jobs.data.jobs.find(job => job.name === 'Approve Fork PR');
37+
if (!approveJob || approveJob.conclusion !== 'success') {
38+
core.setFailed('Not a fork PR approval workflow run');
39+
return;
40+
}
41+
42+
// Read PR information from artifacts
43+
let prNumber, prHeadSha, prCheckoutRepo;
44+
try {
45+
prNumber = fs.readFileSync('./pr_number', 'utf8').trim();
46+
prHeadSha = fs.readFileSync('./pr_head_sha', 'utf8').trim();
47+
prCheckoutRepo = fs.readFileSync('./pr_checkout_repository', 'utf8').trim();
48+
} catch (error) {
49+
core.setFailed(`Failed to read PR information: ${error.message}`);
50+
return;
51+
}
52+
53+
console.log(`Deploying approved fork PR #${prNumber}`);
54+
55+
// Trigger deployment via repository dispatch
56+
await github.rest.repos.createDispatchEvent({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
event_type: 'pr-preview-deploy',
60+
client_payload: {
61+
pr_number: prNumber,
62+
pr_head_sha: prHeadSha,
63+
pr_checkout_repository: prCheckoutRepo,
64+
is_fork: 'true'
65+
}
66+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: PR Preview Check
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
# Deploy main repo PRs directly
8+
deploy-for-main:
9+
name: Trigger Preview Build and Deploy (Main Repo)
10+
runs-on: ubuntu-latest
11+
if: github.event.pull_request.head.repo.fork == false
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');
30+
31+
# Approval gate for fork PRs
32+
approve-for-fork:
33+
name: Approve Fork PR
34+
runs-on: ubuntu-latest
35+
if: github.event.pull_request.head.repo.fork == true
36+
environment: fork-preview-protection
37+
steps:
38+
- name: Save PR information
39+
run: |
40+
echo "Fork PR #${{ github.event.pull_request.number }} approved for preview deployment"
41+
mkdir -p ./pr
42+
echo "${{ github.event.pull_request.number }}" > ./pr/pr_number
43+
echo "${{ github.event.pull_request.head.sha }}" > ./pr/pr_head_sha
44+
echo "${{ github.event.pull_request.head.repo.full_name }}" > ./pr/pr_checkout_repository
45+
46+
- name: Upload PR information
47+
uses: actions/upload-artifact@v4
48+
with:
49+
name: pr
50+
path: pr/
51+
retention-days: 1

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

.github/workflows/push_gem.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
steps:
2525
- name: Harden Runner
26-
uses: step-security/harden-runner@002fdce3c6a235733a90a27c80493a3241e56863 # v2.12.1
26+
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 # v2.12.2
2727
with:
2828
egress-policy: audit
2929

0 commit comments

Comments
 (0)