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