Skip to content

Commit 369aaa1

Browse files
committed
ci(pwa): 手机端 PWA 发版工作流(mobile-release.yml + /pwa/ 子路径部署配套)
1 parent 38b745c commit 369aaa1

4 files changed

Lines changed: 134 additions & 3 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Release PWA (Mobile)
2+
3+
# 由 semantic-release 创建的 v* tag 触发:构建 PWA 移动端产物并部署到
4+
# ECS 服务器(https://entropydecrease.com/pwa/)。与 release.yml(Electron
5+
# 桌面端)共用同一 v* tag——双端同版本发布、互不干扰;PR 质量验证复用
6+
# pr-check.yml(client job 已含 vite build 产出 PWA)。
7+
on:
8+
push:
9+
tags: ['v*']
10+
# 兜底触发:semantic-release 以内置 GITHUB_TOKEN 推 tag 时 GitHub 防递归
11+
# 机制不派发 push 事件(同 release.yml 注释),Release 发布事件补跑
12+
release:
13+
types: [published]
14+
# 手动兜底:在 UI 选择对应 tag 运行,用于补发历史版本
15+
workflow_dispatch:
16+
17+
permissions:
18+
contents: read
19+
20+
# 同一 tag 的多个触发源串行执行,避免重复部署竞争
21+
concurrency:
22+
group: release-pwa-${{ github.ref_name }}
23+
cancel-in-progress: false
24+
25+
jobs:
26+
build:
27+
runs-on: ubuntu-latest
28+
timeout-minutes: 30
29+
steps:
30+
# lfs: true 必需:pwa-192x192.png 等由 Git LFS 托管,不拉取则为指针文件
31+
- uses: actions/checkout@v4
32+
with:
33+
lfs: true
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: 20
37+
cache: 'npm'
38+
cache-dependency-path: client/package-lock.json
39+
- run: cd client && npm ci
40+
- run: cd client && npm run lint
41+
- run: cd client && npm run test -- --run
42+
# PWA 构建(子路径 /pwa/ 部署):.env.production 已入库(git 跟踪),
43+
# 生产环境变量自动注入;VITE_PWA_BASE 控制构建 base 与 manifest
44+
# start_url/scope(vite.config.ts),保证子路径安装入口正确
45+
- name: Build PWA (base=/pwa/)
46+
env:
47+
VITE_PWA_BASE: /pwa
48+
run: cd client && npx vite build
49+
- name: Verify PWA artifacts
50+
run: |
51+
cd client
52+
test -f dist/index.html && echo "index.html OK"
53+
test -f dist/manifest.webmanifest && echo "manifest.webmanifest OK"
54+
test -f dist/sw.js && echo "sw.js OK"
55+
- uses: actions/upload-artifact@v4
56+
with:
57+
name: pwa-dist
58+
path: client/dist/
59+
retention-days: 14
60+
61+
deploy:
62+
needs: build
63+
runs-on: ubuntu-latest
64+
timeout-minutes: 15
65+
steps:
66+
- uses: actions/download-artifact@v4
67+
with:
68+
name: pwa-dist
69+
path: pwa-dist
70+
- name: Ensure PWA directory on server
71+
uses: appleboy/ssh-action@v1
72+
with:
73+
host: ${{ secrets.SERVER_HOST }}
74+
username: ${{ secrets.SERVER_USER }}
75+
key: ${{ secrets.SSH_PRIVATE_KEY }}
76+
script: mkdir -p /opt/Entropydecrease/pwa
77+
- name: Upload PWA to server (clean replace)
78+
uses: appleboy/scp-action@v0.1.7
79+
with:
80+
host: ${{ secrets.SERVER_HOST }}
81+
username: ${{ secrets.SERVER_USER }}
82+
key: ${{ secrets.SSH_PRIVATE_KEY }}
83+
source: pwa-dist/*
84+
target: /opt/Entropydecrease/pwa
85+
strip_components: 1
86+
rm: true
87+
# scp rm:true 重建目录导致 bind mount 失效,须重启 nginx 容器恢复
88+
# (同 deploy-website.yml 模式);顺带写入版本标记 + 验证部署
89+
- name: Restart Nginx, write version and verify
90+
uses: appleboy/ssh-action@v1
91+
with:
92+
host: ${{ secrets.SERVER_HOST }}
93+
username: ${{ secrets.SERVER_USER }}
94+
key: ${{ secrets.SSH_PRIVATE_KEY }}
95+
script: |
96+
echo "${{ github.ref_name }}" > /opt/Entropydecrease/pwa/version.txt
97+
docker restart entropy-decrease-nginx
98+
sleep 5
99+
curl -sf https://entropydecrease.com/pwa/ | grep -q "熵减" || exit 1
100+
echo "PWA deployed: ${{ github.ref_name }}"

‎client/vite.config.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { writeFileSync, mkdirSync } from 'fs'
66

77
// 检测 Electron 桌面端构建模式
88
const isElectronBuild = !!process.env.ELECTRON_BUILD;
9+
// PWA 部署子路径(CI 注入 VITE_PWA_BASE=/pwa;本地默认根路径 '/')。
10+
// 同时控制构建 base 与 manifest start_url/scope,保证子路径部署(/pwa/)时
11+
// 资源路径与安装入口均指向正确位置
12+
const pwaBase = (process.env.VITE_PWA_BASE || '').replace(/\/+$/, '');
13+
const pwaBasePath = pwaBase ? `${pwaBase}/` : '/';
914

1015
/**
1116
* Vite 插件:Electron 构建时将主进程需要的 VITE_* 环境变量写入 build-config.json
@@ -53,7 +58,7 @@ export default defineConfig(({ command }) => ({
5358
// dev 模式必须保持 '/'——否则 Vite 预构建依赖 URL 解析异常,
5459
// 全部 optimizeDeps 产物持续 504,lazy 页面动态导入失败
5560
// (曾因 ELECTRON_BUILD=1 vite --mode test 启动 dev server 触发)
56-
base: isElectronBuild && command === 'build' ? './' : '/',
61+
base: isElectronBuild && command === 'build' ? './' : pwaBasePath,
5762
plugins: [
5863
react(),
5964
// Electron 构建时生成 build-config.json,供主进程运行时读取环境变量
@@ -70,8 +75,8 @@ export default defineConfig(({ command }) => ({
7075
background_color: '#111827',
7176
display: 'standalone',
7277
orientation: 'portrait-primary',
73-
scope: '/',
74-
start_url: '/',
78+
scope: pwaBasePath,
79+
start_url: pwaBasePath,
7580
icons: [
7681
{
7782
src: 'pwa-192x192.png',

‎server/docker-compose.prod.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ services:
170170
- /opt/Entropydecrease/website:/usr/share/nginx/html:ro
171171
# 安装包下载目录:独立于 website(其部署会 rm 清空目录),由 release.yml 同步
172172
- /opt/Entropydecrease/downloads:/usr/share/nginx/downloads:ro
173+
# PWA 移动端目录:由 mobile-release.yml 同步至 /opt/Entropydecrease/pwa
174+
- /opt/Entropydecrease/pwa:/usr/share/nginx/pwa:ro
173175
depends_on:
174176
- ai-gateway
175177
- sync-service

‎server/nginx/nginx.conf‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,30 @@ http {
262262
}
263263
}
264264

265+
# -----------------------------------------------------------------
266+
# PWA 移动端(由 mobile-release.yml 同步至 /opt/Entropydecrease/pwa,
267+
# 挂载为 /usr/share/nginx/pwa)。SPA 路由回退到 index.html;
268+
# 带 content hash 的资源长期缓存,manifest/sw.js 不缓存(更新即时生效)
269+
# -----------------------------------------------------------------
270+
location ^~ /pwa/ {
271+
root /usr/share/nginx;
272+
try_files $uri $uri/ /pwa/index.html;
273+
}
274+
location ^~ /pwa/assets/ {
275+
root /usr/share/nginx;
276+
expires 365d;
277+
add_header Cache-Control "public, immutable";
278+
access_log off;
279+
}
280+
location = /pwa/manifest.webmanifest {
281+
root /usr/share/nginx;
282+
add_header Cache-Control "no-cache" always;
283+
}
284+
location = /pwa/sw.js {
285+
root /usr/share/nginx;
286+
add_header Cache-Control "no-cache" always;
287+
}
288+
265289
# -----------------------------------------------------------------
266290
# 官网静态站点(Next.js 静态导出,由 CI 同步至 /opt/Entropydecrease/website)
267291
# -----------------------------------------------------------------

0 commit comments

Comments
 (0)