Skip to content

Commit 5497e70

Browse files
authored
chore(ci): add cloudflare preview (#1065)
1 parent ee31445 commit 5497e70

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Cleanup Cloudflare Preview
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup-preview:
9+
runs-on: ubuntu-latest
10+
if: github.repository == 'doocs/md'
11+
permissions:
12+
pull-requests: write
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v5
16+
17+
- name: Set up node
18+
uses: actions/setup-node@v6
19+
with:
20+
node-version: 22
21+
22+
- name: Setup pnpm
23+
uses: pnpm/action-setup@v4
24+
with:
25+
version: 10
26+
27+
- name: Install wrangler globally
28+
run: pnpm add -g wrangler
29+
30+
- name: Delete preview deployment
31+
id: delete
32+
continue-on-error: true
33+
run: |
34+
echo "Attempting to delete md-pr-${{ github.event.pull_request.number }}"
35+
wrangler delete md-pr-${{ github.event.pull_request.number }} --force
36+
env:
37+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
38+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
39+
40+
- name: Comment on PR
41+
if: steps.delete.outcome == 'success'
42+
uses: actions/github-script@v7
43+
with:
44+
github-token: ${{ secrets.GITHUB_TOKEN }}
45+
script: |
46+
const prNumber = context.issue.number;
47+
48+
await github.rest.issues.createComment({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
issue_number: prNumber,
52+
body: '🗑️ Cloudflare Workers preview deployment has been cleaned up.'
53+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: Cloudflare Workers Preview
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
concurrency:
8+
group: cloudflare-preview-${{ github.event.pull_request.number }}
9+
cancel-in-progress: true
10+
11+
jobs:
12+
deploy-preview:
13+
runs-on: ubuntu-latest
14+
if: github.repository == 'doocs/md'
15+
permissions:
16+
contents: read
17+
deployments: write
18+
pull-requests: write
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v5
22+
with:
23+
ref: ${{ github.event.pull_request.head.sha }}
24+
25+
- name: Set up node
26+
uses: actions/setup-node@v6
27+
with:
28+
node-version: 22
29+
30+
- name: Setup pnpm
31+
uses: pnpm/action-setup@v4
32+
with:
33+
version: 10
34+
35+
- name: Install dependencies
36+
run: pnpm install
37+
38+
- name: Build for Cloudflare Workers
39+
run: pnpm web build:h5-netlify
40+
env:
41+
CF_PAGES: 1
42+
43+
- name: Deploy to Cloudflare Workers
44+
id: deploy
45+
uses: cloudflare/wrangler-action@v3
46+
with:
47+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
48+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
49+
command: deploy --name md-pr-${{ github.event.pull_request.number }}
50+
workingDirectory: apps/web
51+
env:
52+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
53+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
54+
55+
- name: Get deployment URL
56+
id: deployment-url
57+
run: |
58+
PREVIEW_URL="https://md-pr-${{ github.event.pull_request.number }}.doocs.workers.dev"
59+
echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT
60+
echo "Preview URL: $PREVIEW_URL"
61+
62+
- name: Comment PR with preview link
63+
uses: actions/github-script@v7
64+
with:
65+
github-token: ${{ secrets.GITHUB_TOKEN }}
66+
script: |
67+
const previewUrl = '${{ steps.deployment-url.outputs.url }}';
68+
const prNumber = context.issue.number;
69+
70+
// Find existing comment
71+
const comments = await github.rest.issues.listComments({
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
issue_number: prNumber,
75+
});
76+
77+
const botComment = comments.data.find(comment =>
78+
comment.user.type === 'Bot' &&
79+
comment.body.includes('Cloudflare Workers Preview')
80+
);
81+
82+
const commentBody = `## 🚀 Cloudflare Workers Preview
83+
84+
Your preview is ready!
85+
86+
**Preview URL:** ${previewUrl}
87+
88+
<sub>Built with commit ${context.sha}</sub>`;
89+
90+
if (botComment) {
91+
// Update existing comment
92+
await github.rest.issues.updateComment({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
comment_id: botComment.id,
96+
body: commentBody
97+
});
98+
} else {
99+
// Create new comment
100+
await github.rest.issues.createComment({
101+
owner: context.repo.owner,
102+
repo: context.repo.repo,
103+
issue_number: prNumber,
104+
body: commentBody
105+
});
106+
}

0 commit comments

Comments
 (0)