Files
persionalBrand/docs/runner-setup.md
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

93 lines
6.5 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 Runner 安装与运维
CI 跑在自托管 Gitea Actions runner 上。当前使用本地工作站 `Elitebook`(x86_64、12 核、30 GB,Docker 容器执行),旧的 `oracle_arm` runner 已停止并禁用。本文件记录可复现的安装步骤,避免换机器时重新摸索。
## 一次性准备
1. 在 Gitea 仓库 Settings → Actions → Runners 里创建注册 token(只对单个仓库或整个实例生效)。
2. 目标主机需要:Docker、`curl`、`xz`、systemd;Node 不需要(任务容器自带)。`ufw` 和 `sudo` 仅在需要配置防火墙或 linger 时使用。
## 安装
把仓库里的脚本拷到目标主机后执行:
```bash
GITEA_URL=https://git.example.com \
RUNNER_TOKEN=<注册 token> \
RUNNER_NAME=elitebook-local \
RUNNER_LABELS=ubuntu-latest,local-x64 \
RUNNER_CAPACITY=2 \
RUNNER_CONTAINER_OPTIONS='--cpus=4 --memory=8g' \
RUNNER_IMAGE=personal-brand-ci:node24 \
RUNNER_PNPM_STORE=$HOME/gitea-runner/pnpm-store \
SKIP_LINGER=1 \
./install-runner.sh
```
加 `DRY_RUN=1` 可先看它准备执行哪些命令(token 会被替换成 `***`)。
脚本做的事:
1. 按架构下载 `gitea-runner-<version>-linux-<arch>`,校验 `.sha256` 后解压到 `~/gitea-runner`。
2. 用 `--no-interactive` 注册,写出 `.runner`(已存在则跳过,可重复执行)。
3. 生成 `config.yaml`:按 `RUNNER_CAPACITY` 设置并发,使用 `RUNNER_IMAGE`(默认 `node:24-bookworm`)作为任务容器镜像,内置 cache server 监听 `172.17.0.1:8193`,并按 `RUNNER_CONTAINER_OPTIONS` 限制任务容器资源。
4. 把 `RUNNER_PNPM_STORE`(默认 `~/gitea-runner/pnpm-store`)bind mount 到任务容器的 `/workspace/.pnpm-store`;workflow 通过 `npm_config_store_dir` 指向同一路径,所有 job 复用这份 pnpm store,避免重复下载依赖。
5. 为下载的 runner 二进制补上可执行权限,避免归档解压后出现 `Permission denied`。
6. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。
7. 放行任务容器到 cache 端口的 ufw 规则。
## 专用 CI 镜像
`browser-smoke` 需要 Chromium 及其系统库。为避免每个 job 重复执行 `playwright install --with-deps`,本机使用预构建镜像:
```bash
docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 .
```
镜像包含 Node 24、pnpm 9.15.0、git/curl/jq/unzip,以及 Playwright 1.58.2 的 Chromium、headless shell、FFmpeg 和系统依赖;构建时还会预热 Prisma engines 到 `/root/.cache/prisma`。构建产物是本机 amd64 镜像(约 3 GB),不推送到 registry。
workflow 不再使用 `actions/setup-node` 或 `pnpm/action-setup`,而是直接校验镜像内置的 Node/pnpm 版本。因此替换 `RUNNER_IMAGE` 时必须保证镜像内已有 Node 24 和 pnpm 9.15.0,否则 job 会在 `Verify preinstalled toolchain` 步骤失败。
`apps/taro/package.json` 升级 Playwright 后,同步修改 `deploy/runner/Dockerfile` 的 `PLAYWRIGHT_VERSION`;升级 Prisma 后,同步修改 `PRISMA_VERSION`。然后重建镜像并重启 runner:
```bash
docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 .
systemctl --user restart gitea-runner.service
```
镜像只在当前主机存在;换 runner 主机时需要重新构建。若在 ARM 主机构建,镜像会按该主机架构原生构建,不需要额外参数。
## 为什么 ufw 要放行三个网段
任务容器访问宿主机的 cache server 走 Docker bridge,`ufw` 默认 `INPUT DROP` 会直接丢包。Gitea Actions 每个 job 会新建独立 bridge(实测 `192.168.x`),默认 `docker0` 又是 `172.17.x`,所以需要 `172.16.0.0/12`、`192.168.0.0/16`、`10.0.0.0/8` 三条规则。只放行其中一条的后果是缓存恢复/保存各空等数分钟并最终超时。
## 日常检查
```bash
systemctl --user status gitea-runner.service
journalctl --user -u gitea-runner -n 100 --no-pager
du -sh ~/gitea-runner/cache-data ~/gitea-runner/pnpm-store
```
`pnpm-store` 会持续增长。确认没有 job 运行时,可以执行 `pnpm store prune` 清理未被引用的包(在任意任务容器里对 `/workspace/.pnpm-store` 执行,或直接删除旧目录后让下一次 job 重新填充)。
`pnpm deploy:status` 只检查 VPS 服务、备份和公网路由,不再把 CI runner 当作生产健康条件。runner 已迁到本地工作站,脚本会额外打印非阻断的 `ci-runner-local=`,也可以直接用上面的 `systemctl --user status gitea-runner.service` 检查。
## 排障
| 现象 | 先查什么 |
| ----------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| push 后 CI 一直 queued 不启动 | runner 服务是否 active;Gitea 仓库 Runners 页面是否显示 online |
| 日志出现 `Failed to GetCacheEntryDownloadURL ... ETIMEDOUT` | ufw 是否放行三个私网段;`docker exec <job-container> node -e 'fetch("http://172.17.0.1:8193/")'` 应返回 404 |
| 一轮 CI 突然变慢 | 缓存是否命中;宿主机负载与磁盘余量 |
| 本机开发明显变慢 | 是否同时运行 build/api job;用 `docker stats` 检查任务容器资源 |
| 重启后 runner 没起来 | `loginctl show-user "$USER" -p Linger` 应为 `Linger=yes`;否则执行 `sudo loginctl enable-linger "$USER"` |
## 约束
- 本地 runner 使用 `capacity: 2`,每个任务容器限制 `4 CPU / 8 GB`;按宿主资源调整,不要超过实际可用内存。
- `~/gitea-runner/pnpm-store` 是所有 job 共享的 pnpm store。不要删除正在被 job 使用的 store;换 runner 主机时整目录拷贝或重新预热即可。
- 本地 runner 关机或休眠时,required checks 会保持 queued。需要开机自启时执行 `sudo loginctl enable-linger "$USER"`,否则用户会话退出后服务会停止。
- 日志级别保持 `debug`:`info` 级别不记录各 step 的结果行,排障时等于没有日志。
- 完整浏览器回归不进入定时或 main 推送 CI;PR 只运行轻量 browser smoke(见 `docs/ci.md`),完整套件仍在发布前执行。