-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
102 lines (91 loc) · 3.35 KB
/
cloudflare-preview.yml
File metadata and controls
102 lines (91 loc) · 3.35 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
name: Cloudflare Workers Preview
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:
concurrency:
group: cloudflare-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
deploy-preview:
runs-on: ubuntu-latest
if: github.repository == 'doocs/md' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
permissions:
contents: read
deployments: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Set up node
uses: actions/setup-node@v6
with:
node-version: 22
- name: Setup pnpm
uses: pnpm/action-setup@v6
with:
version: 11
- name: Install dependencies
run: pnpm install
- name: Build for Cloudflare Workers
run: pnpm web build:h5-netlify
env:
CF_WORKERS: 1
- name: Deploy to Cloudflare Workers
id: deploy
uses: cloudflare/wrangler-action@v4
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --name md-pr-${{ github.event.pull_request.number }}
workingDirectory: apps/web
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Get deployment URL
id: deployment-url
run: |
PREVIEW_URL="https://md-pr-${{ github.event.pull_request.number }}.doocs.workers.dev"
echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT
echo "Preview URL: $PREVIEW_URL"
- name: Comment PR with preview link
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = ${{ github.event.pull_request.number }};
const deployUrl = '${{ steps.deployment-url.outputs.url }}';
const commitSha = '${{ github.event.pull_request.head.sha }}';
const body = [
'🚀 Cloudflare Workers Preview has been successfully deployed!',
'',
`**Preview URL:** ${deployUrl}`,
'',
`<sub>Built with commit ${commitSha}</sub>`,
'',
'<!-- Cloudflare Preview Comment -->',
].join('\n');
// Upsert: find existing comment and update, or create new
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(c => c.body.includes('<!-- Cloudflare Preview Comment -->'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}