ci: deploy web build to nginx on merge
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
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: Verify
|
||||||
|
if: steps.guard.outputs.skip != 'true'
|
||||||
|
run: |
|
||||||
|
echo "--- 产物一致性检查 ---"
|
||||||
|
local_index=$(node -p "require('fs').readFileSync('dist/index.html','utf8').match(/index-[A-Za-z0-9_-]+\\.js/)?.[0] || ''")
|
||||||
|
remote_index=$(grep -oE "index-[A-Za-z0-9_-]+\.js" /mnt/nginx-html/gao/index.html | head -1)
|
||||||
|
echo "本地入口:$local_index"
|
||||||
|
echo "线上入口:$remote_index"
|
||||||
|
if [ "$local_index" != "$remote_index" ]; then
|
||||||
|
echo "FAIL: 线上 index.html 与本次产物不一致"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "OK: 线上已是最新构建"
|
||||||
Reference in New Issue
Block a user