-
Notifications
You must be signed in to change notification settings - Fork 8
121 lines (107 loc) · 4.63 KB
/
Copy pathpr-preview.yml
File metadata and controls
121 lines (107 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: PR Preview
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
deploy-cloudflare-preview:
runs-on: ubuntu-latest
if: ${{ !github.event.pull_request.head.repo.fork && vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' }}
permissions:
contents: read
deployments: write
outputs:
preview_url: ${{ steps.cf.outputs.pages-deployment-alias-url }}
deployment_url: ${{ steps.cf.outputs.deployment-url }}
has_token: ${{ steps.token_check.outputs.has_token }}
steps:
- name: Check Cloudflare token
id: token_check
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: |
if [ -n "$CLOUDFLARE_API_TOKEN" ]; then
echo "has_token=true" >> "$GITHUB_OUTPUT"
else
echo "has_token=false" >> "$GITHUB_OUTPUT"
fi
- name: Checkout
if: ${{ steps.token_check.outputs.has_token == 'true' }}
uses: actions/checkout@v6
- name: Set up Ruby
if: ${{ steps.token_check.outputs.has_token == 'true' }}
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- name: Build site
if: ${{ steps.token_check.outputs.has_token == 'true' }}
run: bundle exec jekyll build
- name: Publish to Cloudflare Pages
id: cf
if: ${{ steps.token_check.outputs.has_token == 'true' }}
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy _site --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT }} --branch=pr-${{ github.event.number }}
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
wranglerVersion: "3"
comment-preview:
runs-on: ubuntu-latest
needs: deploy-cloudflare-preview
if: ${{ always() && !github.event.pull_request.head.repo.fork && (needs.deploy-cloudflare-preview.result == 'success' || needs.deploy-cloudflare-preview.result == 'skipped' || needs.deploy-cloudflare-preview.result == 'failure') }}
steps:
- name: Add or update PR preview comment
uses: actions/github-script@v7
env:
RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
CLOUDFLARE_URL: ${{ needs.deploy-cloudflare-preview.outputs.preview_url }}
CLOUDFLARE_DEPLOYMENT_URL: ${{ needs.deploy-cloudflare-preview.outputs.deployment_url }}
CLOUDFLARE_ENABLED: ${{ vars.CLOUDFLARE_PAGES_PROJECT != '' && vars.CLOUDFLARE_ACCOUNT_ID != '' && needs.deploy-cloudflare-preview.outputs.has_token == 'true' }}
CLOUDFLARE_RESULT: ${{ needs.deploy-cloudflare-preview.result }}
with:
script: |
const marker = "<!-- pr-preview-comment -->";
const previewUrl = process.env.CLOUDFLARE_URL || process.env.CLOUDFLARE_DEPLOYMENT_URL;
const cloudflareLine = process.env.CLOUDFLARE_ENABLED === "true"
? (process.env.CLOUDFLARE_RESULT === "success"
? (previewUrl
? `- Hosted preview (Cloudflare Pages): [Open preview](${previewUrl})`
: "- Hosted preview (Cloudflare Pages): deployed, but URL output was empty. Check deployment details.")
: `- Hosted preview (Cloudflare Pages): deployment did not succeed; check [workflow logs](${process.env.RUN_URL}).`)
: "- Hosted preview (Cloudflare Pages): not configured (set repository vars/secrets; see README).";
const body = `${marker}
## PR Preview Ready
${cloudflareLine}
`;
const { owner, repo } = context.repo;
const issue_number = context.issue.number;
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.data.find((comment) => comment.body && comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}