This repository has been archived on 2026-09-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

100 lines
5.2 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: deploy-userbase-front
# 前端部署(方案 B:不打包镜像,直接部署静态文件 + 软链接版本控制)
# 触发:push 到 main(已启用仓库 Actions,push 自动部署)/ 手动 Run workflow
# 核心:vite build --mode prod -> dist/prod
# -> 拷贝到 /data/www/userbase/releases/<version>/
# -> 原子切换 /data/www/userbase/front 软链接指向新版本
# -> 保留最近 5 个版本,更早自动清理(秒级回滚)
# 不再构建/推送 nginx 镜像;Caddy 已改为 file_server(root=/srv/userbase=/data/www/userbase)。
# 依赖:160 宿主的 docker daemon(runner 已挂 docker.sock),且 runner valid_volumes 已放行 /data/www/**。无需 NEXUS secret。
on:
push:
branches: [main]
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
# docker:24 自带 docker CLI,复用宿主 docker daemon(act_runner v0.6.1 已挂载 docker.sock)。
# 切勿再声明 volumes: docker.sock(会报 Duplicate mount point)。
container:
image: docker:24
steps:
- name: Install git (alpine)
run: |
# 统一走 aliyun 源:dl-cdn 时段性被拦会卡满 60s×2(2026-10-20 事故)
sed -i 's#dl-cdn.alpinelinux.org#mirrors.aliyun.com#g' /etc/apk/repositories
apk add --no-cache git
# 内网 Gitea 无法访问 github.com 拉取 actions/checkout,改为直接 clone 本仓库
- name: Checkout (manual, internal Gitea)
run: |
git clone --depth 1 --branch main http://192.168.10.160:3000/${GITHUB_REPOSITORY}.git /tmp/src
cp -a /tmp/src/. .
rm -rf /tmp/src
# 构建 + 部署:在 node:20 容器里完成(源码经 docker cp 传入,并挂载宿主 /data/www/userbase)
- name: Build & Deploy (symlink versioning)
run: |
set -e
# 构建容器名必须全局唯一:runner 挂的是宿主 docker.sock,容器名在整个 daemon 上共享
# 命名空间。固定名会在多仓/多条 workflow 并发时互相 docker rm -f 掉对方正在构建的
# 容器,表现为无编译报错的 exit 137(SIGKILL,2026-10-21 实测复现)。
# GITHUB_RUN_ID 实例级唯一,再拼时间戳兜底。
BUILD_CONTAINER="frontbuild-${GITHUB_RUN_ID:-0}-$(date +%s)"
echo "BUILD_CONTAINER=${BUILD_CONTAINER}"
# 版本号:short sha + 时间戳(可改为 git tag 优先,见下)
VERSION="$(echo ${GITHUB_SHA} | cut -c1-7)-$(date +%Y%m%d-%H%M)"
echo "Deploying version: $VERSION"
docker rm -f "$BUILD_CONTAINER" 2>/dev/null || true
docker run -d --name "$BUILD_CONTAINER" \
-e VERSION="$VERSION" \
-v /data/www/userbase:/data/www/userbase \
-w /build node:20 sleep 3600
# 源码拷入构建容器(绕过 act 工作区非宿主路径的坑,与 service 的 mavenbuild 一致)
docker cp "${PWD}/." "$BUILD_CONTAINER":/build/
docker exec "$BUILD_CONTAINER" sh -c '
set -e
cd /build
# 去掉 Windows 专用 sass 二进制路径(Linux 容器内该路径无效,否则 build 报错)
# .npmrc 被 gitignore、CI 克隆下来没有它,故仅在存在时才删(缺失不报错)
if [ -f .npmrc ]; then sed -i "/SASS_BINARY_PATH/d" .npmrc; fi
# 国内镜像源:内网直连 registry.npmjs.org 跨境极慢(实测 npm install 12 分 46 秒 / 754 包)
npm config set registry https://registry.npmmirror.com
npm ci
npm run build:prod
'
# 部署:首次把真实目录 front 迁入 releases/initial 并建软链;之后均为软链替换
docker exec "$BUILD_CONTAINER" sh -c '
set -e
cd /build
# ---- 首次初始化:front 当前是真实目录,无法直接 ln -sfn 覆盖 ----
if [ ! -L /data/www/userbase/front ]; then
mkdir -p /data/www/userbase/releases
mv /data/www/userbase/front /data/www/userbase/releases/initial
# 相对软链:Caddy 容器内 /data/www/userbase 挂在 /srv/userbase,
# 绝对路径在容器内解析不到,必须用相对路径(相对 front 所在目录)
ln -sfn releases/initial /data/www/userbase/front
echo "INIT: front -> releases/initial (保留上线前状态用于回滚)"
fi
mkdir -p /data/www/userbase/releases/$VERSION
cp -r /build/dist/prod/. /data/www/userbase/releases/$VERSION/
# 原子切换:front 此时已是软链接,ln -sfn 直接替换指向(必须用相对路径)
ln -sfn releases/$VERSION /data/www/userbase/front
echo "current symlink ->"; ls -la /data/www/userbase/front
# 清理旧版本,保留最近 5 个(initial 也会被计入,最多保留 5 个含 initial)
cd /data/www/userbase/releases
ls -1t | tail -n +6 | while read d; do echo "rm old: $d"; rm -rf "$d"; done
echo "remaining versions:"; ls -1t
'
docker rm -f "$BUILD_CONTAINER"
echo "DEPLOY OK: $VERSION"