Files
que01 447c224fce
CI / Static checks (pull_request) Successful in 1m1s
CI / API integration (pull_request) Successful in 1m26s
CI / Production build (pull_request) Successful in 1m45s
CI / Browser smoke (pull_request) Successful in 50s
ci: 预热 Prisma 并移除运行时 Node 下载
2026-10-23 17:37:48 +08:00

119 lines
7.1 KiB
Markdown
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.
# CI(Gitea Actions)
远端仓库是 Gitea,CI 由 `.gitea/workflows/ci.yml` 定义,运行在本地工作站 `Elitebook` 上的 self-hosted runner(x86_64、Docker、双并发)。旧的 `oracle_arm` runner 已停止并禁用,避免两个 runner 争抢 `ubuntu-latest` 标签。
## Runner
安装与运维步骤见 `docs/runner-setup.md`(`deploy/runner/install-runner.sh` 可复现整套配置)。
- 程序:`gitea-runner` v5.2.0(linux/amd64),位于 `~/gitea-runner`
- 服务:systemd user unit `gitea-runner.service`
- 标签:`ubuntu-latest`、`local-x64`
- 执行模式:Docker,双并发,任务容器限制 `--cpus=4 --memory=8g`
- 任务镜像:`personal-brand-ci:node24`(预装 pnpm 与 Playwright 依赖,见 `deploy/runner/Dockerfile`)
- 作用域:仅 `que01/persionalBrand` 仓库
查看状态:
```bash
systemctl --user status gitea-runner.service
journalctl --user -u gitea-runner -n 100 --no-pager
```
## 当前门禁
- `static`:格式、lint、typecheck、模板检查、路由与模板 preview 覆盖
- `build`:生产构建、Admin preview 构建、模板 release 校验
- `api`:PostgreSQL service container 上的 API integration
- `browser-smoke`:PR 和手动触发时执行会话恢复、创建品牌与页面方案 smoke;只在 PR 和 `workflow_dispatch` 运行,main 合并后不重复执行
完整浏览器回归仍作为发布前本地门禁执行,避免在共享开发机上运行过重的 Playwright 套件。PR smoke 只跑两条关键流程,并使用已构建的 Admin 静态产物与 mocked API。
## 注意
Runner 与本地开发工作共用 `Elitebook` 主机,不再与生产 API 共用资源。任务容器限制 4 CPU / 8 GB、并发 2;本地机器关机或休眠时 CI 会排队,required checks 不会自动完成。
## 工具链与外部下载
任务镜像 `personal-brand-ci:node24` 已内置 Node 24 和 pnpm 9.15.0;workflow 不再调用 `actions/setup-node` 或 `pnpm/action-setup`,而是在每个 job 开头执行 `Verify preinstalled toolchain`。这样不会因为 nodejs.org 下载缓慢或中断而把 job 卡到超时。
Prisma engines 在构建镜像时预热到 `/root/.cache/prisma`,job 内 `pnpm install` 触发的 `prisma generate` 直接命中缓存,不再访问 `binaries.prisma.sh`。workflow 仍用 `actions/cache` 缓存该目录,作为更换镜像或镜像未预热时的兜底。升级 Prisma 后必须同步 `deploy/runner/Dockerfile` 的 `PRISMA_VERSION` 并重建镜像。
## 缓存
Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=~/gitea-runner/cache-data`),供 `actions/cache` 使用(Turbo 缓存等)。
pnpm 依赖不再走 actions/cache:宿主机 `~/gitea-runner/pnpm-store` 被 bind mount 到任务容器的 `/workspace/.pnpm-store`,workflow 的 `npm_config_store_dir` 指向该路径。同一轮 CI 里的 static、build、api、browser-smoke 共享一份 store,`pnpm install` 只做链接/校验,不再重复下载 1600+ 个包。
如果 runner 主机启用了 `ufw`,任务容器到 cache server 的流量会被 `INPUT DROP` 拦截,需要显式放行 Docker 使用的三个私网网段;本地工作站未启用 `ufw`,无需配置:
```bash
sudo ufw allow from 172.16.0.0/12 to any port 8193 proto tcp comment "gitea actions cache"
sudo ufw allow from 192.168.0.0/16 to any port 8193 proto tcp comment "gitea actions cache"
sudo ufw allow from 10.0.0.0/8 to any port 8193 proto tcp comment "gitea actions cache"
```
注意 Gitea Actions 会为每个 job 建立自己的 bridge 网络(实测网关为 `192.168.80.1`),不只是默认的 `172.17.0.1`;只放行 `172.16.0.0/12` 仍然会超时。
未放行时表现为 `Failed to GetCacheEntryDownloadURL ... ETIMEDOUT`,每个 job 会额外空等数分钟,且缓存永远不会命中。验证方式是在 job 容器内直接请求缓存端口:
```bash
docker exec <job-container> node -e 'fetch("http://172.17.0.1:8193/").then(r=>console.log(r.status))'
```
返回 `404` 表示连通(该端口不提供根路径),超时表示仍被拦截。
Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Success - Main ...` 与告警,排障时只能依赖 Gitea 页面。
## 迁移前基线(`oracle_arm`)
| 项 | 值 |
| ---------------- | ----------------------------- |
| run | `21`(`run_number=21`) |
| commit | `c33c9cb` |
| Static checks | 05:03:54 → 05:15:42 UTC,通过 |
| Production build | 05:15:47 → 05:27:27 UTC,通过 |
| API integration | 05:27:32 → 05:37:16 UTC,通过 |
单并发下整轮约 33 分钟;三个 job 串行执行,主要耗时在 `pnpm install` 与构建。
该轮次全部告警均来自缓存后端不可达(`ETIMEDOUT`),已按上一节修复。
修复后的对照数据(run `22`,commit `261a33a`;run `23` 的 static 为 5.7 分钟):
| job | 修复前耗时 | 修复后耗时 |
| ---------------- | ---------- | --------------------------------------------------------- |
| Static checks | 11.8 min | 11.7 min(该 job 在放行规则生效前启动,仍走缓存超时路径) |
| Production build | 11.7 min | 7.6 min |
| API integration | 9.7 min | 3.8 min |
缓存内容为 pnpm store,首次落盘约 1.1 GB,位于 `~/gitea-runner/pnpm-store`(共享 bind mount)。
## 触发范围
- `push` 到 `main` 时忽略纯文档改动(`**.md`),避免为文档提交跑完整 CI。
- `pull_request` 不设置 `paths-ignore`。branch protection 要求的 status check 必须每次都上报,否则纯文档 PR 会一直停在「等待检查」而无法合并。
- 需要给纯文档提交补一次状态时,用 `workflow_dispatch` 手动触发。
模板完成证据的源码哈希已同步收敛,文档、CI 配置和部署脚本改动不再使证据失效;模板、renderer、Admin 模板编辑器、品牌内容 API 等会影响交付的源码改动仍会更新哈希。`pre-commit` 会按 Git index 重算并暂存证据,不再需要单独的 `chore: 更新模板完成证据源码哈希` 提交。注意:`pre-commit` 只在该仓库执行过 `pnpm install`(触发 `prepare` 设置 `core.hooksPath`)后生效。
## 分支保护
`main` 由 Gitea branch protection 保护,规则为:
- 禁止直接 push 和 force push,所有改动走分支 + Pull Request。
- 必须通过 `CI / *` 状态检查(覆盖 `Static checks`、`Production build`、`API integration` 及其后续新增 job)。
- `required_approvals = 0`:当前是单人维护,不强制他人审批,但 CI 必须全绿。
仓库管理员如需紧急放行,应临时调整保护规则并在事后恢复,不要长期用管理员权限绕过检查。规则可通过 `PATCH /api/v1/repos/que01/persionalBrand/branch_protections/main` 复现:
```json
{
"enable_push": false,
"enable_force_push": false,
"enable_status_check": true,
"status_check_contexts": ["CI / *"],
"required_approvals": 0
}
```