109 lines
4.1 KiB
YAML
109 lines
4.1 KiB
YAML
name: Deploy
|
||
|
||
# 网页部署:构建后同步到 nginx 静态目录
|
||
# 热更新包由 release.yml 单独处理,两者互不影响
|
||
# 标题带 [skip deploy] 可跳过网页更新
|
||
on:
|
||
pull_request:
|
||
branches:
|
||
- master
|
||
types:
|
||
- closed
|
||
workflow_dispatch:
|
||
|
||
jobs:
|
||
deploy:
|
||
runs-on: act_runner_node
|
||
if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Check skip flag
|
||
id: guard
|
||
run: |
|
||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||
echo "skip=false" >> $GITHUB_OUTPUT
|
||
exit 0
|
||
fi
|
||
title="${{ github.event.pull_request.title }}"
|
||
if echo "$title" | grep -q "\[skip deploy\]"; then
|
||
echo "标题含 [skip deploy],跳过网页部署"
|
||
echo "skip=true" >> $GITHUB_OUTPUT
|
||
else
|
||
echo "skip=false" >> $GITHUB_OUTPUT
|
||
fi
|
||
|
||
- name: Configure nexus auth
|
||
if: steps.guard.outputs.skip != 'true'
|
||
env:
|
||
NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
|
||
NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
|
||
run: |
|
||
if [ -z "$NEXUS_USERNAME" ] || [ -z "$NEXUS_PASSWORD" ]; then
|
||
echo "FAIL: 缺少 secrets.NEXUS_USERNAME 或 secrets.NEXUS_PASSWORD"
|
||
exit 1
|
||
fi
|
||
auth=$(printf '%s:%s' "$NEXUS_USERNAME" "$NEXUS_PASSWORD" | base64 | tr -d '\n')
|
||
{
|
||
echo "registry=https://nexus.imyeyu.com/repository/npm-group/"
|
||
echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${auth}"
|
||
echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true"
|
||
echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${auth}"
|
||
echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true"
|
||
} > ~/.npmrc
|
||
echo "OK: nexus 认证已配置"
|
||
|
||
- name: Install dependencies
|
||
if: steps.guard.outputs.skip != 'true'
|
||
run: pnpm install --frozen-lockfile
|
||
|
||
- name: Build
|
||
if: steps.guard.outputs.skip != 'true'
|
||
run: pnpm run build
|
||
|
||
- name: Deploy to nginx
|
||
if: steps.guard.outputs.skip != 'true'
|
||
run: |
|
||
target=/mnt/nginx-html/gao
|
||
if [ ! -d "$target" ]; then
|
||
echo "FAIL: 目标目录 $target 不存在"
|
||
exit 1
|
||
fi
|
||
if [ ! -f dist/index.html ]; then
|
||
echo "FAIL: dist/index.html 缺失"
|
||
exit 1
|
||
fi
|
||
# 不能删除旧文件:nginx 对 /assets/ 设了一年 immutable 缓存,删掉会让在用的客户端白屏
|
||
# 同名文件覆盖即可,历史 chunk 保留
|
||
cp -a dist/. "$target/"
|
||
echo "部署完成"
|
||
echo "文件总数:$(find "$target" -type f | wc -l)"
|
||
echo "index.html:$(stat -c '%y %s 字节' "$target/index.html")"
|
||
echo "assets 数量:$(find "$target/assets" -type f 2>/dev/null | wc -l)"
|
||
|
||
- name: Prune old assets
|
||
if: steps.guard.outputs.skip != 'true'
|
||
run: |
|
||
target=/mnt/nginx-html/gao
|
||
before=$(du -sm "$target" | cut -f1)
|
||
# 只删 30 天前的,本次复制的文件 mtime 是构建时间,不受影响
|
||
# APK 用本地包不请求这些文件,Web 的 index.html 是 no-cache,风险窗口只有长期不刷新
|
||
deleted=$(find "$target/assets" -type f -mtime +30 -print -delete 2>/dev/null | wc -l)
|
||
after=$(du -sm "$target" | cut -f1)
|
||
echo "清理 $deleted 个 30 天前的文件,体积 ${before}MB → ${after}MB"
|
||
|
||
- name: Verify
|
||
if: steps.guard.outputs.skip != 'true'
|
||
run: |
|
||
echo "--- 产物一致性检查 ---"
|
||
local_sum=$(md5sum dist/index.html | cut -d' ' -f1)
|
||
remote_sum=$(md5sum /mnt/nginx-html/gao/index.html | cut -d' ' -f1)
|
||
echo "本地 index.html:$local_sum"
|
||
echo "线上 index.html:$remote_sum"
|
||
if [ "$local_sum" != "$remote_sum" ]; then
|
||
echo "FAIL: 线上 index.html 与本次产物不一致"
|
||
exit 1
|
||
fi
|
||
echo "OK: 线上已是最新构建"
|