Skip to content

Commit eca0c90

Browse files
committed
feat(ci): added preview wrangler build CI
1 parent bf68492 commit eca0c90

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
name: Cloudflare Workers (Preview) Comment
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- Cloudflare Workers (Preview)
7+
types:
8+
- completed
9+
10+
env:
11+
PR_NUM: 0
12+
BRANCH_NAME: main
13+
14+
jobs:
15+
on-success:
16+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
17+
name: Deploy
18+
runs-on: ubuntu-latest
19+
20+
permissions:
21+
contents: read
22+
pull-requests: write
23+
24+
strategy:
25+
matrix:
26+
include:
27+
- app_name: stage-web
28+
artifact_name: preview-build-stage-web
29+
artifact_path: ./apps/stage-web/dist
30+
deploy_command: version deploy -c ./apps/stage-web/wrangler.toml
31+
32+
steps:
33+
- uses: actions/checkout@v6
34+
# Turborepo
35+
- name: Cache turbo build setup
36+
uses: actions/cache@v4
37+
with:
38+
path: .turbo
39+
key: ${{ runner.os }}-turbo-${{ github.sha }}
40+
restore-keys: |
41+
${{ runner.os }}-turbo-
42+
- uses: pnpm/action-setup@v4
43+
- uses: actions/setup-node@v6
44+
with:
45+
node-version: lts/*
46+
cache: pnpm
47+
48+
# NOTICE:
49+
#
50+
# Here installing wrangler to global is required, or otherwise:
51+
# ERR_PNPM_ADDING_TO_ROOT  Running this command will add the dependency to the workspace root...
52+
# error occurs.
53+
#
54+
# Since https://github.com/cloudflare/wrangler-action/pull/339#issuecomment-2667622947 rejected the -g support
55+
# by saying un-reasonable 'I'm not sure if it's common ... to install packages to the global scope, ... might be introducing some unintended side effects.'
56+
#
57+
# Clearly I think installing with `<packageManager> install` brings more unintended side effects...
58+
#
59+
# As suggested by https://github.com/cloudflare/wrangler-action/issues/181#issuecomment-2127990708, we should pre-install
60+
# with our package manager and then use it in the action.
61+
- run: pnpm i -g wrangler@4
62+
63+
- name: Download artifact - PR
64+
uses: dawidd6/action-download-artifact@v11
65+
with:
66+
workflow_conclusion: success
67+
run_id: ${{ github.event.workflow_run.id }}
68+
name: pr-num
69+
path: pr-num
70+
allow_forks: true
71+
72+
- name: Download artifact - PR
73+
uses: dawidd6/action-download-artifact@v11
74+
with:
75+
workflow_conclusion: success
76+
run_id: ${{ github.event.workflow_run.id }}
77+
name: branch-name
78+
path: branch-name
79+
allow_forks: true
80+
81+
- name: Obtain PR number
82+
id: pr-num
83+
run: |
84+
echo "PR_NUM=$(cat pr-num/pr_num)" >> $GITHUB_ENV
85+
86+
- name: Obtain branch name
87+
id: branch-name
88+
run: |
89+
echo "BRANCH_NAME=$(cat branch-name/branch_name)" >> $GITHUB_ENV
90+
91+
- name: Download artifact
92+
uses: dawidd6/action-download-artifact@v11
93+
with:
94+
workflow_conclusion: success
95+
run_id: ${{ github.event.workflow_run.id }}
96+
name: preview-build-stage-web
97+
path: ./apps/stage-web/dist
98+
allow_forks: true
99+
100+
- name: Upload
101+
id: deploy
102+
uses: cloudflare/wrangler-action@v3
103+
with:
104+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
105+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
106+
command: ${{ matrix.deploy_command }}
107+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
108+
109+
- name: Find Comment
110+
uses: peter-evans/find-comment@v4
111+
id: fc
112+
with:
113+
issue-number: ${{ env.PR_NUM }}
114+
comment-author: 'github-actions[bot]'
115+
body-includes: to Cloudflare Workers (Preview) for *${{ matrix.app_name }}*
116+
117+
- name: Create or update comment
118+
uses: peter-evans/create-or-update-comment@v5
119+
with:
120+
comment-id: ${{ steps.fc.outputs.comment-id }}
121+
issue-number: ${{ env.PR_NUM }}
122+
edit-mode: replace
123+
body: |
124+
## ✅ Deploy to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* ready!
125+
126+
| Name | Link |
127+
|:-----------------------|:---------------------------------------------------------------------------------------------|
128+
| 🔍 Latest deploy log | https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} |
129+
| 😎 Deploy Preview | ${{ steps.deploy.outputs.deployment-url }} |
130+
131+
on-failure:
132+
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
133+
name: Failed to build previewing docs
134+
runs-on: ubuntu-latest
135+
136+
permissions:
137+
pull-requests: write
138+
139+
strategy:
140+
matrix:
141+
include:
142+
- app_name: stage-web
143+
144+
steps:
145+
- name: Download artifact - PR
146+
uses: dawidd6/action-download-artifact@v11
147+
with:
148+
workflow_conclusion: success
149+
run_id: ${{ github.event.workflow_run.id }}
150+
name: pr-num
151+
path: pr-num
152+
allow_forks: true
153+
154+
- name: Obtain PR number
155+
id: pr-num
156+
run: |
157+
echo "PR_NUM=$(cat pr-num/pr_num)" >> $GITHUB_ENV
158+
159+
- name: Find Comment
160+
uses: peter-evans/find-comment@v4
161+
id: fc
162+
with:
163+
issue-number: ${{ env.PR_NUM }}
164+
comment-author: 'github-actions[bot]'
165+
body-includes: to Cloudflare Workers (Preview) for *${{ matrix.app_name }}*
166+
167+
- name: Create or update comment
168+
uses: peter-evans/create-or-update-comment@v5
169+
with:
170+
comment-id: ${{ steps.fc.outputs.comment-id }}
171+
issue-number: ${{ env.PR_NUM }}
172+
edit-mode: replace
173+
body: |
174+
## ❌ Deploy to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* failed.
175+
176+
| Name | Link |
177+
|:-----------------------|:---------------------------------------------------------------------------------------------|
178+
| 🔍 Latest deploy log | https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} |
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Cloudflare Workers (Preview)
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
paths:
8+
- 'docs/**'
9+
10+
jobs:
11+
build:
12+
name: Deploy Preview - ${{ matrix.app_name }}
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
include:
18+
- app_name: stage-web
19+
environment_name: Production
20+
environment_url: https://airi.moeru.ai/docs/
21+
cloudflare_pages_id: moeru-ai-airi
22+
dist_directory: ./apps/stage-web/dist
23+
command: |
24+
pnpm -F @proj-airi/stage-web run build
25+
26+
pnpm -F @proj-airi/docs run build:base
27+
mv ./docs/.vitepress/dist ./apps/stage-web/dist/docs
28+
cp ./apps/stage-web/dist/docs/sitemap.xml ./apps/stage-web/dist/sitemap.xml
29+
30+
pnpm -F @proj-airi/stage-ui run story:build
31+
mv ./packages/stage-ui/.histoire/dist ./apps/stage-web/dist/ui
32+
33+
steps:
34+
# This is quite weird.
35+
# Even though this is the *intended* solution introduces in official blog post here
36+
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/.
37+
# But still, as https://github.com/orgs/community/discussions/25220#discussioncomment-7856118 stated,
38+
# this is vulnerable since there is no source of truth about which PR in the triggered workflow.
39+
- name: Persist PR number
40+
run: |
41+
echo "${{ github.event.number }}" > pr_num
42+
43+
- name: Persist branch name
44+
run: |
45+
echo "${{ github.head_ref }}" > branch_name
46+
47+
- name: Upload PR artifact
48+
uses: actions/upload-artifact@v5
49+
with:
50+
name: pr-num
51+
path: ./pr_num
52+
overwrite: true
53+
54+
- name: Upload PR artifact
55+
uses: actions/upload-artifact@v5
56+
with:
57+
name: branch-name
58+
path: ./branch_name
59+
overwrite: true
60+
61+
- uses: actions/checkout@v6
62+
# Turborepo
63+
- name: Cache turbo build setup
64+
uses: actions/cache@v4
65+
with:
66+
path: .turbo
67+
key: ${{ runner.os }}-turbo-${{ github.sha }}
68+
restore-keys: |
69+
${{ runner.os }}-turbo-
70+
- uses: pnpm/action-setup@v4
71+
- uses: actions/setup-node@v6
72+
with:
73+
node-version: lts/*
74+
cache: pnpm
75+
76+
- run: pnpm install --frozen-lockfile
77+
- run: pnpm run build:packages
78+
- run: ${{ matrix.command }}
79+
env:
80+
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
81+
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
82+
S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }}
83+
S3_REGION: ${{ secrets.S3_REGION }}
84+
WARP_DRIVE_PUBLIC_BASE: ${{ secrets.WARP_DRIVE_PUBLIC_BASE }}
85+
86+
- name: Upload artifact
87+
uses: actions/upload-artifact@v5
88+
with:
89+
name: preview-build-${{ matrix.app_name }}
90+
path: ${{ matrix.dist_directory }}
91+
overwrite: true

0 commit comments

Comments
 (0)