From 965dd10219912de9af7679dea3d4f404872e8d9a Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 23 Oct 2026 15:40:50 +0800 Subject: [PATCH 1/4] =?UTF-8?q?ci:=20=E8=BF=81=E7=A7=BB=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=20runner=20=E5=B9=B6=E4=BC=98=E5=8C=96=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../raw/completion-evidence.json | 4 +-- .gitea/workflows/ci.yml | 22 +++++++++++++--- apps/web/Dockerfile | 7 ++++- deploy/runner/install-runner.sh | 22 +++++++++++----- docs/ci.md | 26 +++++++++---------- docs/runner-setup.md | 23 +++++++++------- 6 files changed, 69 insertions(+), 35 deletions(-) diff --git a/.codex-tasks/template-audit-execution/raw/completion-evidence.json b/.codex-tasks/template-audit-execution/raw/completion-evidence.json index 8264a22..45728b9 100644 --- a/.codex-tasks/template-audit-execution/raw/completion-evidence.json +++ b/.codex-tasks/template-audit-execution/raw/completion-evidence.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "scope": "模板完成证据登记,保留命令、日志与产物哈希", - "generatedAt": "2026-10-23T06:35:59.337Z", + "generatedAt": "2026-10-23T07:40:54.556Z", "templates": { "acheng-campus-food": { "real-manifest-interaction": { @@ -256,5 +256,5 @@ } } }, - "sourceTreeHash": "f39e31a1959e24c07bf60776a0d3c5f28e5745962ca004301ff304daa1430041" + "sourceTreeHash": "f0bb03d734cf303b75be41bbcab6eea15f5479ac197b16c82f78e1faad71b17e" } diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 508468a..6b1cbf4 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: push: branches: [main] - # 纯文档提交不消耗 2 核 runner;需要状态时用 workflow_dispatch 手动触发。 + # 纯文档提交不占用 runner;需要状态时用 workflow_dispatch 手动触发。 paths-ignore: - "**.md" # PR 不设置 paths-ignore:branch protection 的 required checks 必须始终上报, @@ -33,6 +33,14 @@ jobs: with: node-version: ${{ env.NODE_VERSION }} cache: pnpm + - name: Restore Turbo cache + uses: actions/cache@v4 + with: + path: .turbo/cache + key: turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }} + restore-keys: | + turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}- + turbo-${{ runner.os }}-${{ runner.arch }}- - run: pnpm install --frozen-lockfile - run: pnpm check - run: pnpm test:api-routes @@ -56,6 +64,14 @@ jobs: with: node-version: ${{ env.NODE_VERSION }} cache: pnpm + - name: Restore Turbo cache + uses: actions/cache@v4 + with: + path: .turbo/cache + key: turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }} + restore-keys: | + turbo-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }}- + turbo-${{ runner.os }}-${{ runner.arch }}- - run: pnpm install --frozen-lockfile - run: pnpm build - run: pnpm build:admin:preview @@ -67,7 +83,6 @@ jobs: if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest timeout-minutes: 30 - needs: [static] steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 @@ -82,7 +97,7 @@ jobs: uses: actions/cache@v4 with: path: ~/.cache/ms-playwright - key: playwright-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} + key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }} - name: Install Chromium run: pnpm --filter @personal-brand/taro exec playwright install --with-deps chromium - name: Run PR browser smoke @@ -92,7 +107,6 @@ jobs: name: API integration runs-on: ubuntu-latest timeout-minutes: 45 - needs: [static] services: postgres: image: postgres:16-alpine diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index 000f220..59d1893 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -18,7 +18,12 @@ RUN pnpm --filter @personal-brand/config build RUN pnpm --filter @personal-brand/types build RUN pnpm --filter @personal-brand/validation build RUN pnpm --filter @personal-brand/api-client build -RUN pnpm add -w @tarojs/binding-linux-arm64-gnu@4.3.0 +ARG TARGETARCH +RUN case "${TARGETARCH:-$(uname -m)}" in \ + arm64 | aarch64) pnpm add -w @tarojs/binding-linux-arm64-gnu@4.3.0 ;; \ + amd64 | x86_64) pnpm add -w @tarojs/binding-linux-x64-gnu@4.3.0 ;; \ + *) echo "unsupported TARGETARCH: ${TARGETARCH:-$(uname -m)}" >&2; exit 1 ;; \ + esac RUN pnpm build:admin:preview FROM alpine:latest AS runtime diff --git a/deploy/runner/install-runner.sh b/deploy/runner/install-runner.sh index 746a4d9..c956d0b 100755 --- a/deploy/runner/install-runner.sh +++ b/deploy/runner/install-runner.sh @@ -10,22 +10,28 @@ # RUNNER_TOKEN 仓库 Settings → Actions → Runners 里生成的注册 token # 可选: # RUNNER_NAME runner 名称,默认取 hostname -# RUNNER_LABELS 默认 ubuntu-latest,oracle-arm +# RUNNER_LABELS 默认 ubuntu-latest # RUNNER_VERSION 默认 5.2.0 # RUNNER_ROOT 默认 $HOME/gitea-runner +# RUNNER_CAPACITY 默认 1,按 runner 主机资源设置并发数 +# RUNNER_CONTAINER_OPTIONS 默认 "--cpus=1.5 --memory=4g" # CACHE_PORT 默认 8193 # RUNNER_CACHE_HOST 默认 172.17.0.1(Docker 默认 bridge 网关) +# SKIP_LINGER 默认 0;设为 1 时不尝试用 sudo 开启 linger set -euo pipefail GITEA_URL=${GITEA_URL:?需要设置 GITEA_URL} RUNNER_TOKEN=${RUNNER_TOKEN:?需要设置 RUNNER_TOKEN} RUNNER_NAME=${RUNNER_NAME:-$(hostname -s)} -RUNNER_LABELS=${RUNNER_LABELS:-ubuntu-latest,oracle-arm} +RUNNER_LABELS=${RUNNER_LABELS:-ubuntu-latest} RUNNER_VERSION=${RUNNER_VERSION:-5.2.0} RUNNER_ROOT=${RUNNER_ROOT:-$HOME/gitea-runner} +RUNNER_CAPACITY=${RUNNER_CAPACITY:-1} +RUNNER_CONTAINER_OPTIONS=${RUNNER_CONTAINER_OPTIONS:-"--cpus=1.5 --memory=4g"} CACHE_PORT=${CACHE_PORT:-8193} RUNNER_CACHE_HOST=${RUNNER_CACHE_HOST:-172.17.0.1} CACHE_DIR=${CACHE_DIR:-$RUNNER_ROOT/cache-data} +SKIP_LINGER=${SKIP_LINGER:-0} DRY_RUN=${DRY_RUN:-0} if [ "$DRY_RUN" != "1" ]; then @@ -57,6 +63,7 @@ if [ ! -x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" ]; then run env -C "$RUNNER_ROOT" sha256sum -c "$archive.sha256" run xz -d -f "$RUNNER_ROOT/$archive" fi +run chmod +x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" run ln -sf "gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" "$RUNNER_ROOT/gitea-runner" if [ ! -f "$RUNNER_ROOT/.runner" ]; then @@ -76,7 +83,7 @@ log: level: debug runner: file: .runner - capacity: 1 + capacity: ${RUNNER_CAPACITY} timeout: 2h fetch_timeout: 10s fetch_interval: 2s @@ -88,7 +95,7 @@ cache: host: ${RUNNER_CACHE_HOST} port: ${CACHE_PORT} container: - options: "--cpus=1.5 --memory=4g" + options: "${RUNNER_CONTAINER_OPTIONS}" valid_volumes: [] docker_host: "-" CONFIG @@ -111,8 +118,11 @@ WantedBy=default.target UNIT # 用户级 systemd 服务默认在最后一个会话退出后停止;开启 linger 才能在重启后自动拉起 runner。 -if command -v loginctl >/dev/null 2>&1; then - run sudo loginctl enable-linger "$USER" +if [ "$SKIP_LINGER" != "1" ] && command -v loginctl >/dev/null 2>&1; then + linger=$(loginctl show-user "$USER" -p Linger --value 2>/dev/null || true) + if [ "$linger" != "yes" ]; then + run sudo loginctl enable-linger "$USER" + fi fi run systemctl --user daemon-reload run systemctl --user enable --now gitea-runner.service diff --git a/docs/ci.md b/docs/ci.md index 9776528..fec3d30 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -1,22 +1,22 @@ # CI(Gitea Actions) -远端仓库是 Gitea,CI 由 `.gitea/workflows/ci.yml` 定义,运行在 `oracle_arm` 主机上的 self-hosted runner。 +远端仓库是 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/arm64),位于 `/home/ubuntu/gitea-runner` +- 程序:`gitea-runner` v5.2.0(linux/amd64),位于 `~/gitea-runner` - 服务:systemd user unit `gitea-runner.service` -- 标签:`ubuntu-latest`、`oracle-arm` -- 执行模式:Docker,单并发,任务容器限制 `--cpus=1.5 --memory=4g` +- 标签:`ubuntu-latest`、`local-x64` +- 执行模式:Docker,双并发,任务容器限制 `--cpus=4 --memory=8g` - 作用域:仅 `que01/persionalBrand` 仓库 查看状态: ```bash -ssh oracle_arm 'systemctl --user status gitea-runner.service' -ssh oracle_arm 'journalctl --user -u gitea-runner -n 100 --no-pager' +systemctl --user status gitea-runner.service +journalctl --user -u gitea-runner -n 100 --no-pager ``` ## 当前门禁 @@ -26,17 +26,17 @@ ssh oracle_arm 'journalctl --user -u gitea-runner -n 100 --no-pager' - `api`:PostgreSQL service container 上的 API integration - `browser-smoke`:PR 和手动触发时执行会话恢复、创建品牌与页面方案 smoke;只在 PR 和 `workflow_dispatch` 运行,main 合并后不重复执行 -完整浏览器回归仍作为发布前本地门禁执行,避免在 2 核生产主机上运行完整 Playwright 套件抢占资源。PR smoke 只跑两条关键流程,并使用已构建的 Admin 静态产物与 mocked API。 +完整浏览器回归仍作为发布前本地门禁执行,避免在共享开发机上运行过重的 Playwright 套件。PR smoke 只跑两条关键流程,并使用已构建的 Admin 静态产物与 mocked API。 ## 注意 -Runner 与生产 API 共用 `oracle_arm` 主机。任务容器已限制 CPU 与内存;如果 CI 期间生产延迟明显,应暂停 runner 或迁移到独立主机。 +Runner 与本地开发工作共用 `Elitebook` 主机,不再与生产 API 共用资源。任务容器限制 4 CPU / 8 GB、并发 2;本地机器关机或休眠时 CI 会排队,required checks 不会自动完成。 ## 缓存 -Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=/home/ubuntu/gitea-runner/cache-data`),供 `actions/setup-node` 的 pnpm 缓存使用。 +Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=~/gitea-runner/cache-data`),供 `actions/setup-node` 的 pnpm 缓存使用。 -任务容器与主机之间被 `ufw` 的 `INPUT DROP` 默认策略拦截,需要显式放行 Docker 使用的三个私网网段: +如果 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" @@ -56,7 +56,7 @@ docker exec node -e 'fetch("http://172.17.0.1:8193/").then(r=>co Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Success - Main ...` 与告警,排障时只能依赖 Gitea 页面。 -## 首次完整通过 +## 迁移前基线(`oracle_arm`) | 项 | 值 | | ---------------- | ----------------------------- | @@ -78,11 +78,11 @@ Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Succe | Production build | 11.7 min | 7.6 min | | API integration | 9.7 min | 3.8 min | -缓存内容为 pnpm store,首次落盘约 268 MB,位于 `/home/ubuntu/gitea-runner/cache-data`。 +缓存内容为 pnpm store,首次落盘约 268 MB,位于 `~/gitea-runner/cache-data`。 ## 触发范围 -- `push` 到 `main` 时忽略纯文档改动(`**.md`),避免 2 核主机为文档提交跑完整 CI。 +- `push` 到 `main` 时忽略纯文档改动(`**.md`),避免为文档提交跑完整 CI。 - `pull_request` 不设置 `paths-ignore`。branch protection 要求的 status check 必须每次都上报,否则纯文档 PR 会一直停在「等待检查」而无法合并。 - 需要给纯文档提交补一次状态时,用 `workflow_dispatch` 手动触发。 diff --git a/docs/runner-setup.md b/docs/runner-setup.md index 71eedf8..9e996d2 100644 --- a/docs/runner-setup.md +++ b/docs/runner-setup.md @@ -1,11 +1,11 @@ # CI Runner 安装与运维 -CI 跑在自托管 Gitea Actions runner 上(当前主机 `oracle_arm`,与生产 API 共用 2 核)。本文件记录可复现的安装步骤,避免换机器时重新摸索。 +CI 跑在自托管 Gitea Actions runner 上。当前使用本地工作站 `Elitebook`(x86_64、12 核、30 GB,Docker 容器执行),旧的 `oracle_arm` runner 已停止并禁用。本文件记录可复现的安装步骤,避免换机器时重新摸索。 ## 一次性准备 1. 在 Gitea 仓库 Settings → Actions → Runners 里创建注册 token(只对单个仓库或整个实例生效)。 -2. 目标主机需要:Docker、`curl`、`xz`、`ufw`、systemd、可用 sudo、Node 不需要(任务容器自带)。 +2. 目标主机需要:Docker、`curl`、`xz`、systemd;Node 不需要(任务容器自带)。`ufw` 和 `sudo` 仅在需要配置防火墙或 linger 时使用。 ## 安装 @@ -14,8 +14,11 @@ CI 跑在自托管 Gitea Actions runner 上(当前主机 `oracle_arm`,与生 ```bash GITEA_URL=https://git.example.com \ RUNNER_TOKEN=<注册 token> \ -RUNNER_NAME=oracle-arm \ -RUNNER_LABELS=ubuntu-latest,oracle-arm \ +RUNNER_NAME=elitebook-local \ +RUNNER_LABELS=ubuntu-latest,local-x64 \ +RUNNER_CAPACITY=2 \ +RUNNER_CONTAINER_OPTIONS='--cpus=4 --memory=8g' \ +SKIP_LINGER=1 \ ./install-runner.sh ``` @@ -25,9 +28,10 @@ RUNNER_LABELS=ubuntu-latest,oracle-arm \ 1. 按架构下载 `gitea-runner--linux-`,校验 `.sha256` 后解压到 `~/gitea-runner`。 2. 用 `--no-interactive` 注册,写出 `.runner`(已存在则跳过,可重复执行)。 -3. 生成 `config.yaml`:单并发、`docker://node:24-bookworm` 标签、内置 cache server 监听 `172.17.0.1:8193`、任务容器限制 `--cpus=1.5 --memory=4g`。 -4. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。 -5. 放行任务容器到 cache 端口的 ufw 规则。 +3. 生成 `config.yaml`:按 `RUNNER_CAPACITY` 设置并发,使用 `docker://node:24-bookworm` 标签,内置 cache server 监听 `172.17.0.1:8193`,并按 `RUNNER_CONTAINER_OPTIONS` 限制任务容器资源。 +4. 为下载的 runner 二进制补上可执行权限,避免归档解压后出现 `Permission denied`。 +5. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。 +6. 放行任务容器到 cache 端口的 ufw 规则。 ## 为什么 ufw 要放行三个网段 @@ -50,11 +54,12 @@ du -sh ~/gitea-runner/cache-data | push 后 CI 一直 queued 不启动 | runner 服务是否 active;Gitea 仓库 Runners 页面是否显示 online | | 日志出现 `Failed to GetCacheEntryDownloadURL ... ETIMEDOUT` | ufw 是否放行三个私网段;`docker exec node -e 'fetch("http://172.17.0.1:8193/")'` 应返回 404 | | 一轮 CI 突然变慢 | 缓存是否命中;宿主机负载与磁盘余量 | -| 生产 API 变慢 | CI 与生产共用主机,检查是否在跑 build/api job;必要时暂停 runner | +| 本机开发明显变慢 | 是否同时运行 build/api job;用 `docker stats` 检查任务容器资源 | | 重启后 runner 没起来 | `loginctl show-user "$USER" -p Linger` 应为 `Linger=yes`;否则执行 `sudo loginctl enable-linger "$USER"` | ## 约束 -- `capacity: 1`:不要提高并发,2 核主机上并发只会让生产和 CI 一起变慢。 +- 本地 runner 使用 `capacity: 2`,每个任务容器限制 `4 CPU / 8 GB`;按宿主资源调整,不要超过实际可用内存。 +- 本地 runner 关机或休眠时,required checks 会保持 queued。需要开机自启时执行 `sudo loginctl enable-linger "$USER"`,否则用户会话退出后服务会停止。 - 日志级别保持 `debug`:`info` 级别不记录各 step 的结果行,排障时等于没有日志。 - 完整浏览器回归不进入定时或 main 推送 CI;PR 只运行轻量 browser smoke(见 `docs/ci.md`),完整套件仍在发布前执行。 -- 2.54.0 From b43b6f27855f55a48a517355fbf61ded8a14206d Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 23 Oct 2026 15:52:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?ci:=20=E9=A2=84=E6=9E=84=E5=BB=BA=20runner?= =?UTF-8?q?=20=E9=95=9C=E5=83=8F=E5=B9=B6=E9=A2=84=E8=A3=85=20Playwright?= =?UTF-8?q?=20=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yml | 3 ++- deploy/runner/Dockerfile | 25 +++++++++++++++++++++++++ deploy/runner/install-runner.sh | 10 ++++++++-- docs/ci.md | 1 + docs/runner-setup.md | 22 +++++++++++++++++++++- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 deploy/runner/Dockerfile diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 6b1cbf4..b349cd1 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -99,7 +99,8 @@ jobs: path: ~/.cache/ms-playwright key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('pnpm-lock.yaml') }} - name: Install Chromium - run: pnpm --filter @personal-brand/taro exec playwright install --with-deps chromium + # 系统依赖与同版本 Chromium 已预装在 personal-brand-ci:node24 中;这里只做版本兜底。 + run: pnpm --filter @personal-brand/taro exec playwright install chromium - name: Run PR browser smoke run: pnpm test:browser:smoke diff --git a/deploy/runner/Dockerfile b/deploy/runner/Dockerfile new file mode 100644 index 0000000..c5f41de --- /dev/null +++ b/deploy/runner/Dockerfile @@ -0,0 +1,25 @@ +# 本地 CI 任务容器基础镜像:Node 24 + pnpm + Playwright 运行依赖。 +# 构建(在仓库根目录执行): +# docker build -f deploy/runner/Dockerfile -t personal-brand-ci:node24 . +# Playwright 版本需与 apps/taro/package.json 保持一致;升级 Playwright 后重建本镜像。 +FROM node:24-bookworm + +ARG PNPM_VERSION=9.15.0 +ARG PLAYWRIGHT_VERSION=1.58.2 + +ENV PNPM_HOME=/usr/local/share/pnpm +ENV PATH=${PNPM_HOME}:${PATH} + +RUN apt-get update \ + && apt-get install -y --no-install-recommends jq unzip \ + && rm -rf /var/lib/apt/lists/* + +RUN corepack enable \ + && corepack prepare pnpm@${PNPM_VERSION} --activate \ + && pnpm --version + +# 预装 Chromium 的系统依赖和浏览器本体,避免每个 job 重复执行 apt 与下载。 +RUN npx --yes playwright@${PLAYWRIGHT_VERSION} install --with-deps chromium \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /workspace diff --git a/deploy/runner/install-runner.sh b/deploy/runner/install-runner.sh index c956d0b..c3657ca 100755 --- a/deploy/runner/install-runner.sh +++ b/deploy/runner/install-runner.sh @@ -15,6 +15,7 @@ # RUNNER_ROOT 默认 $HOME/gitea-runner # RUNNER_CAPACITY 默认 1,按 runner 主机资源设置并发数 # RUNNER_CONTAINER_OPTIONS 默认 "--cpus=1.5 --memory=4g" +# RUNNER_IMAGE 任务容器镜像,默认 node:24-bookworm;本地预构建镜像可传 personal-brand-ci:node24 # CACHE_PORT 默认 8193 # RUNNER_CACHE_HOST 默认 172.17.0.1(Docker 默认 bridge 网关) # SKIP_LINGER 默认 0;设为 1 时不尝试用 sudo 开启 linger @@ -28,6 +29,7 @@ RUNNER_VERSION=${RUNNER_VERSION:-5.2.0} RUNNER_ROOT=${RUNNER_ROOT:-$HOME/gitea-runner} RUNNER_CAPACITY=${RUNNER_CAPACITY:-1} RUNNER_CONTAINER_OPTIONS=${RUNNER_CONTAINER_OPTIONS:-"--cpus=1.5 --memory=4g"} +RUNNER_IMAGE=${RUNNER_IMAGE:-node:24-bookworm} CACHE_PORT=${CACHE_PORT:-8193} RUNNER_CACHE_HOST=${RUNNER_CACHE_HOST:-172.17.0.1} CACHE_DIR=${CACHE_DIR:-$RUNNER_ROOT/cache-data} @@ -66,13 +68,17 @@ fi run chmod +x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" run ln -sf "gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" "$RUNNER_ROOT/gitea-runner" +label_specs() { + printf '%s\n' "$RUNNER_LABELS" | tr ',' '\n' | sed "s|^\(.*\)$|\1:docker://${RUNNER_IMAGE}|" +} + if [ ! -f "$RUNNER_ROOT/.runner" ]; then run env -C "$RUNNER_ROOT" ./gitea-runner register \ --no-interactive \ --instance "$GITEA_URL" \ --token "$RUNNER_TOKEN" \ --name "$RUNNER_NAME" \ - --labels "$RUNNER_LABELS" + --labels "$(label_specs | paste -sd, -)" fi if [ -f "$RUNNER_ROOT/.runner" ]; then run chmod 600 "$RUNNER_ROOT/.runner" @@ -88,7 +94,7 @@ runner: fetch_timeout: 10s fetch_interval: 2s labels: -$(printf '%s\n' "$RUNNER_LABELS" | tr ',' '\n' | sed 's|^\(.*\)$| - "\1:docker://node:24-bookworm"|') +$(label_specs | sed 's|^| - "|; s|$|"|') cache: enabled: true dir: ${CACHE_DIR} diff --git a/docs/ci.md b/docs/ci.md index fec3d30..a2dc3c5 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -10,6 +10,7 @@ - 服务: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` 仓库 查看状态: diff --git a/docs/runner-setup.md b/docs/runner-setup.md index 9e996d2..ab32100 100644 --- a/docs/runner-setup.md +++ b/docs/runner-setup.md @@ -18,6 +18,7 @@ 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 \ SKIP_LINGER=1 \ ./install-runner.sh ``` @@ -28,11 +29,30 @@ SKIP_LINGER=1 \ 1. 按架构下载 `gitea-runner--linux-`,校验 `.sha256` 后解压到 `~/gitea-runner`。 2. 用 `--no-interactive` 注册,写出 `.runner`(已存在则跳过,可重复执行)。 -3. 生成 `config.yaml`:按 `RUNNER_CAPACITY` 设置并发,使用 `docker://node:24-bookworm` 标签,内置 cache server 监听 `172.17.0.1:8193`,并按 `RUNNER_CONTAINER_OPTIONS` 限制任务容器资源。 +3. 生成 `config.yaml`:按 `RUNNER_CAPACITY` 设置并发,使用 `RUNNER_IMAGE`(默认 `node:24-bookworm`)作为任务容器镜像,内置 cache server 监听 `172.17.0.1:8193`,并按 `RUNNER_CONTAINER_OPTIONS` 限制任务容器资源。 4. 为下载的 runner 二进制补上可执行权限,避免归档解压后出现 `Permission denied`。 5. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。 6. 放行任务容器到 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 和系统依赖。构建产物是本机 amd64 镜像(约 3 GB),不推送到 registry。 + +`apps/taro/package.json` 升级 Playwright 后,同步修改 `deploy/runner/Dockerfile` 的 `PLAYWRIGHT_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` 三条规则。只放行其中一条的后果是缓存恢复/保存各空等数分钟并最终超时。 -- 2.54.0 From b82aa128754210e1746d1051506013da9c158c45 Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 23 Oct 2026 16:09:25 +0800 Subject: [PATCH 3/4] =?UTF-8?q?ci:=20=E5=85=B1=E4=BA=AB=20pnpm=20store=20?= =?UTF-8?q?=E5=8A=A0=E9=80=9F=E6=9C=AC=E5=9C=B0=20runner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci.yml | 6 ++---- deploy/runner/install-runner.sh | 10 +++++++++- docs/ci.md | 6 ++++-- docs/runner-setup.md | 13 +++++++++---- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index b349cd1..cd5dda3 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -18,6 +18,8 @@ concurrency: env: NODE_VERSION: "24" PNPM_VERSION: "9.15.0" + # runner 任务容器把该路径 bind mount 到宿主机共享 store,避免每个 job 重复下载依赖。 + npm_config_store_dir: /workspace/.pnpm-store jobs: static: @@ -32,7 +34,6 @@ jobs: - uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - cache: pnpm - name: Restore Turbo cache uses: actions/cache@v4 with: @@ -63,7 +64,6 @@ jobs: - uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - cache: pnpm - name: Restore Turbo cache uses: actions/cache@v4 with: @@ -91,7 +91,6 @@ jobs: - uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - cache: pnpm - run: pnpm install --frozen-lockfile - name: Cache Playwright browsers uses: actions/cache@v4 @@ -133,7 +132,6 @@ jobs: - uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - cache: pnpm - run: pnpm install --frozen-lockfile - name: Prepare API test env working-directory: apps/api diff --git a/deploy/runner/install-runner.sh b/deploy/runner/install-runner.sh index c3657ca..24ff67b 100755 --- a/deploy/runner/install-runner.sh +++ b/deploy/runner/install-runner.sh @@ -15,6 +15,7 @@ # RUNNER_ROOT 默认 $HOME/gitea-runner # RUNNER_CAPACITY 默认 1,按 runner 主机资源设置并发数 # RUNNER_CONTAINER_OPTIONS 默认 "--cpus=1.5 --memory=4g" +# RUNNER_PNPM_STORE 宿主机 pnpm store 目录,默认 $RUNNER_ROOT/pnpm-store # RUNNER_IMAGE 任务容器镜像,默认 node:24-bookworm;本地预构建镜像可传 personal-brand-ci:node24 # CACHE_PORT 默认 8193 # RUNNER_CACHE_HOST 默认 172.17.0.1(Docker 默认 bridge 网关) @@ -29,6 +30,7 @@ RUNNER_VERSION=${RUNNER_VERSION:-5.2.0} RUNNER_ROOT=${RUNNER_ROOT:-$HOME/gitea-runner} RUNNER_CAPACITY=${RUNNER_CAPACITY:-1} RUNNER_CONTAINER_OPTIONS=${RUNNER_CONTAINER_OPTIONS:-"--cpus=1.5 --memory=4g"} +RUNNER_PNPM_STORE=${RUNNER_PNPM_STORE:-$RUNNER_ROOT/pnpm-store} RUNNER_IMAGE=${RUNNER_IMAGE:-node:24-bookworm} CACHE_PORT=${CACHE_PORT:-8193} RUNNER_CACHE_HOST=${RUNNER_CACHE_HOST:-172.17.0.1} @@ -58,7 +60,7 @@ run() { archive="gitea-runner-${RUNNER_VERSION}-linux-${ARCH}.xz" base="https://dl.gitea.com/gitea-runner/${RUNNER_VERSION}" -run mkdir -p "$RUNNER_ROOT" "$CACHE_DIR" +run mkdir -p "$RUNNER_ROOT" "$CACHE_DIR" "$RUNNER_PNPM_STORE" if [ ! -x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" ]; then run curl -fsSL -o "$RUNNER_ROOT/$archive" "$base/$archive" run curl -fsSL -o "$RUNNER_ROOT/$archive.sha256" "$base/$archive.sha256" @@ -66,6 +68,12 @@ if [ ! -x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" ]; then run xz -d -f "$RUNNER_ROOT/$archive" fi run chmod +x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" + +# 把 pnpm store 挂到所有任务容器的固定路径;workflow 通过 npm_config_store_dir 指向它。 +case " $RUNNER_CONTAINER_OPTIONS " in + *"/workspace/.pnpm-store"*) ;; + *) RUNNER_CONTAINER_OPTIONS="$RUNNER_CONTAINER_OPTIONS --volume=${RUNNER_PNPM_STORE}:/workspace/.pnpm-store" ;; +esac run ln -sf "gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" "$RUNNER_ROOT/gitea-runner" label_specs() { diff --git a/docs/ci.md b/docs/ci.md index a2dc3c5..2156b2f 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -35,7 +35,9 @@ Runner 与本地开发工作共用 `Elitebook` 主机,不再与生产 API 共 ## 缓存 -Runner 内置 cache server(`cache.host=172.17.0.1`、`cache.port=8193`、`cache.dir=~/gitea-runner/cache-data`),供 `actions/setup-node` 的 pnpm 缓存使用。 +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`,无需配置: @@ -79,7 +81,7 @@ Runner 日志级别保持 `debug`:`info` 级别不会记录各 step 的 `Succe | Production build | 11.7 min | 7.6 min | | API integration | 9.7 min | 3.8 min | -缓存内容为 pnpm store,首次落盘约 268 MB,位于 `~/gitea-runner/cache-data`。 +缓存内容为 pnpm store,首次落盘约 1.1 GB,位于 `~/gitea-runner/pnpm-store`(共享 bind mount)。 ## 触发范围 diff --git a/docs/runner-setup.md b/docs/runner-setup.md index ab32100..348f6ec 100644 --- a/docs/runner-setup.md +++ b/docs/runner-setup.md @@ -19,6 +19,7 @@ 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 ``` @@ -30,9 +31,10 @@ SKIP_LINGER=1 \ 1. 按架构下载 `gitea-runner--linux-`,校验 `.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 二进制补上可执行权限,避免归档解压后出现 `Permission denied`。 -5. 装 systemd user unit `gitea-runner.service`,执行 `loginctl enable-linger` 后 `enable --now`。 -6. 放行任务容器到 cache 端口的 ufw 规则。 +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 镜像 @@ -62,9 +64,11 @@ systemctl --user restart gitea-runner.service ```bash systemctl --user status gitea-runner.service journalctl --user -u gitea-runner -n 100 --no-pager -du -sh ~/gitea-runner/cache-data +du -sh ~/gitea-runner/cache-data ~/gitea-runner/pnpm-store ``` +`pnpm-store` 会持续增长。确认没有 job 运行时,可以执行 `pnpm store prune` 清理未被引用的包(在任意任务容器里对 `/workspace/.pnpm-store` 执行,或直接删除旧目录后让下一次 job 重新填充)。 + 仓库侧 `pnpm deploy:status` 也会打印 `ci-runner=` 一行。 ## 排障 @@ -80,6 +84,7 @@ du -sh ~/gitea-runner/cache-data ## 约束 - 本地 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`),完整套件仍在发布前执行。 -- 2.54.0 From e7ef0b3ce5032771f47e4f9bfe91be5681071922 Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 23 Oct 2026 16:10:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?ci:=20=E5=85=81=E8=AE=B8=20runner=20?= =?UTF-8?q?=E6=8C=82=E8=BD=BD=E5=85=B1=E4=BA=AB=20pnpm=20store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/runner/install-runner.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy/runner/install-runner.sh b/deploy/runner/install-runner.sh index 24ff67b..ecd8d08 100755 --- a/deploy/runner/install-runner.sh +++ b/deploy/runner/install-runner.sh @@ -110,7 +110,8 @@ cache: port: ${CACHE_PORT} container: options: "${RUNNER_CONTAINER_OPTIONS}" - valid_volumes: [] + valid_volumes: + - ${RUNNER_PNPM_STORE} docker_host: "-" CONFIG -- 2.54.0