-
-
Notifications
You must be signed in to change notification settings - Fork 1
173 lines (153 loc) · 6.6 KB
/
Copy pathpreview-cloudflare.yml
File metadata and controls
173 lines (153 loc) · 6.6 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
name: Preview (Cloudflare Workers)
on:
pull_request:
branches: [develop]
types: [opened, synchronize, reopened, closed]
concurrency:
group: cloudflare-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
preview:
name: Upload PR preview
runs-on: ubuntu-latest
if: github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- name: Setup pnpm
uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4
with:
version: 9
- name: Setup Node.js
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: 24.11.1
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run ESLint
run: pnpm lint
- name: Check code formatting
run: pnpm format:check
- name: Run tests with coverage
run: pnpm test:coverage
env:
NODE_OPTIONS: --max-old-space-size=4096
- name: Build project
run: pnpm build
env:
VITE_GIT_BRANCH: ${{ github.head_ref }}
VITE_DEV_SITE_URL: https://dev.bayanflow.com
VITE_SUPABASE_URL: ${{ secrets.VITE_SUPABASE_URL }}
VITE_SUPABASE_ANON_KEY: ${{ secrets.VITE_SUPABASE_ANON_KEY }}
VITE_GOOGLE_WEB_CLIENT_ID: ${{ secrets.VITE_GOOGLE_WEB_CLIENT_ID }}
VITE_TURNSTILE_SITE_KEY: ${{ secrets.VITE_TURNSTILE_SITE_KEY }}
VITE_POSTHOG_API_KEY: ${{ secrets.VITE_POSTHOG_API_KEY }}
VITE_POSTHOG_API_HOST: https://e.dev.bayanflow.com
- name: Upload preview version
id: deploy
uses: cloudflare/wrangler-action@9acf94ace14e7dc412b076f2c5c20b8ce93c79cd # v3
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
packageManager: pnpm
command: >-
versions upload --env staging
--preview-alias pr-${{ github.event.pull_request.number }}
--tag pr-${{ github.event.pull_request.number }}
--message "PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}"
- name: Comment PR with preview URL
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
env:
DEPLOY_OUTPUT: ${{ steps.deploy.outputs['command-output'] }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
with:
script: |
const output = process.env.DEPLOY_OUTPUT || '';
const urlRegex = /Version Preview Alias URL:\s+(https:\/\/[^\s]+)/;
const match = output.match(urlRegex);
const prNumber = context.payload.pull_request.number;
const fallbackUrl = `https://pr-${prNumber}-bayan-flow-staging.workers.dev`;
const previewUrl = match ? match[1] : fallbackUrl;
const runUrl = process.env.RUN_URL;
const headSha = process.env.HEAD_SHA;
const qrUrl = `https://quickchart.io/qr?size=200&text=${encodeURIComponent(previewUrl)}`;
const marker = '<!-- cloudflare-preview:bayan-flow-staging -->';
const body = [
'### Preview for *Bayan Flow Staging* ready!',
'',
'| Name | Link |',
'|:-:|------------------------|',
`| <span aria-hidden="true">🔨</span> Latest commit | ${headSha} |`,
`| <span aria-hidden="true">🔍</span> Latest deploy log | ${runUrl} |`,
`| <span aria-hidden="true">😎</span> Deploy Preview | [${previewUrl}](${previewUrl}) |`,
`| <span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br /><br /><br />_Use your smartphone camera to open QR code link._</details> |`,
'',
'---',
marker,
`_Preview alias \`pr-${prNumber}\` on the staging worker. Updates automatically with new commits._`,
].join('\n');
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
});
const existing = comments.find(
(comment) =>
comment.body?.includes(marker) ||
comment.body?.includes('<!-- cloudflare-preview -->')
);
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: context.issue.number,
body,
});
}
cleanup:
name: Cleanup preview on PR close
if: github.event.action == 'closed' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
persist-credentials: false
- uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4
with:
version: 9
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
with:
node-version: 24.11.1
- run: pnpm install --frozen-lockfile
- name: Delete preview version
run: |
VERSION_ID=$(npx wrangler versions list --env staging 2>/dev/null | \
grep -B5 "Tag:.*pr-${{ github.event.pull_request.number }}" | \
grep "Version ID:" | \
awk '{print $NF}' | \
head -1)
if [ -n "$VERSION_ID" ]; then
npx wrangler versions delete "$VERSION_ID" --env staging
else
echo "No preview version found for PR ${{ github.event.pull_request.number }}"
fi
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}