06a43f3 已有意删除 coverage.scope(范围根资产+company 富化)以精简规划上
下文,但该测试仍断言此字段。去掉相关断言、改名为 TestAssociatedCompany
AssetsEnterTaskContext(仍校验公司资产进入任务上下文),并移除 CI 里的 -skip。
152 lines
5.5 KiB
YAML
152 lines
5.5 KiB
YAML
# Gitea Actions:push 到 main 时跑后端测试与前端构建。
|
||
# 刻意不接 pull_request:solo 开发时同一提交会跑两遍,浪费 CI 时间。
|
||
#
|
||
# 前置条件:
|
||
# 1. 仓库 Settings → Actions 勾选 “Enable Repository Actions”
|
||
# 2. 已注册带 ubuntu-latest-amd64 标签的 Gitea Runner
|
||
# runner-amd64 → ubuntu-latest-amd64 / ubuntu-22.04-amd64 / ubuntu-20.04-amd64
|
||
# runner-arm64 → ubuntu-latest-arm64 / ubuntu-22.04-arm64 / ubuntu-20.04-arm64
|
||
# 3. Runner 能出网:actions/checkout 从 github.com 拉 action,setup-go 从 go.dev 下工具链
|
||
#
|
||
# 数据库不用 services:这台 runner 上服务容器的服务名解析不了、端口也没发布到 job,
|
||
# 所以改为在 job 内装 PostgreSQL 并 initdb 一个临时集群(见「启动 PostgreSQL」)。
|
||
name: ci
|
||
|
||
on:
|
||
push:
|
||
branches: [main]
|
||
|
||
permissions:
|
||
contents: read
|
||
|
||
jobs:
|
||
backend:
|
||
runs-on: ubuntu-latest-amd64
|
||
timeout-minutes: 40
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- uses: actions/setup-go@v5
|
||
with:
|
||
go-version: "1.26"
|
||
# setup-go 的 cache 是布尔输入,false 表示不做缓存(依赖 Runner 的缓存服务)。
|
||
cache: false
|
||
|
||
- name: 启动 PostgreSQL
|
||
run: |
|
||
set -euo pipefail
|
||
|
||
# Debian/Ubuntu 把服务器端装在 /usr/lib/postgresql/<ver>/bin。装包自动创建的
|
||
# 集群要靠 systemd 启动,容器里起不来,所以这里自己 initdb 一个临时集群。
|
||
if ! ls /usr/lib/postgresql/*/bin/initdb >/dev/null 2>&1; then
|
||
apt-get update -qq
|
||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq postgresql
|
||
fi
|
||
BIN="$(ls -d /usr/lib/postgresql/*/bin | sort -V | tail -1)"
|
||
echo "使用 $BIN"
|
||
|
||
PGDATA=/tmp/pgdata
|
||
rm -rf "$PGDATA"
|
||
mkdir -p "$PGDATA"
|
||
|
||
# initdb 不允许以 root 运行:root 下切到 postgres 用户,否则用当前用户。
|
||
pg_as() {
|
||
if [ "$(id -u)" = 0 ]; then
|
||
su postgres -s /bin/sh -c "cd /tmp && $*"
|
||
else
|
||
sh -c "cd /tmp && $*"
|
||
fi
|
||
}
|
||
if [ "$(id -u)" = 0 ]; then chown postgres:postgres "$PGDATA"; fi
|
||
|
||
pg_as "$BIN/initdb -D $PGDATA -U artex --auth=trust --encoding=UTF8" > /tmp/initdb.log 2>&1 \
|
||
|| { echo "initdb 失败:"; cat /tmp/initdb.log; exit 1; }
|
||
pg_as "$BIN/pg_ctl -D $PGDATA -l /tmp/pg.log -o '-p 55432 -k /tmp' -w start" \
|
||
|| { echo "pg_ctl 启动失败:"; cat /tmp/pg.log; exit 1; }
|
||
pg_as "$BIN/psql -h /tmp -p 55432 -U artex -d postgres -c 'CREATE DATABASE artex'"
|
||
|
||
{
|
||
echo "PGHOST=127.0.0.1"
|
||
echo "PGPORT=55432"
|
||
echo "PGUSER=artex"
|
||
echo "PGDATABASE=artex"
|
||
echo "ARTEX_PG_DSN=postgres://artex@127.0.0.1:55432/artex?sslmode=disable"
|
||
} >> "$GITHUB_ENV"
|
||
|
||
psql -h 127.0.0.1 -p 55432 -U artex -d artex -tAc "select version()"
|
||
|
||
- name: 初始化数据库 schema
|
||
run: |
|
||
psql -v ON_ERROR_STOP=1 -q -f db/schema.sql
|
||
echo "schema 应用完成,表数:$(psql -tAc "select count(*) from information_schema.tables where table_schema='public'")"
|
||
|
||
- name: 初始化运行时表(llm_usage)
|
||
run: |
|
||
# llm_usage 不在 schema.sql 里,由应用启动时的 db.EnsureLLMUsageTable 建表。
|
||
# 有测试直接读写这张表,所以这里显式补一次,避免依赖包执行顺序。
|
||
cat > /tmp/bootstrap.go <<'GO'
|
||
package main
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
|
||
"github.com/Autumn-27/artex/db"
|
||
)
|
||
|
||
func main() {
|
||
d, err := db.Open(os.Getenv("ARTEX_PG_DSN"))
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
defer d.Close()
|
||
if err := d.EnsureLLMUsageTable(); err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
}
|
||
GO
|
||
go run /tmp/bootstrap.go
|
||
|
||
- name: 依赖完整性
|
||
run: go mod verify
|
||
|
||
- name: 静态检查
|
||
run: go vet ./...
|
||
|
||
- name: 测试
|
||
run: |
|
||
# -p 1:各包共用一个库,串行执行避免跨包数据竞争。
|
||
run_tests() {
|
||
go test -p 1 ./... -count=1
|
||
}
|
||
# 已知偶发:server 包的后台 worker 会在测试结束后继续写 t.TempDir(),
|
||
# 触发 "TempDir RemoveAll cleanup: directory not empty"。根因是
|
||
# Manager.Close() 只关库、不等 task goroutine 退出,属于既有问题。
|
||
# 先重试一次兜住;真实失败会在两次里都失败,不会被掩盖。
|
||
if run_tests; then
|
||
exit 0
|
||
fi
|
||
echo "::: 第一次失败,重试一次(可能是已知的 TempDir 清理竞态)" >&2
|
||
run_tests
|
||
|
||
frontend:
|
||
runs-on: ubuntu-latest-amd64
|
||
timeout-minutes: 20
|
||
steps:
|
||
- uses: actions/checkout@v4
|
||
|
||
- uses: actions/setup-node@v4
|
||
with:
|
||
node-version: "22"
|
||
# 注意:这里不能写 cache: false —— setup-node 的 cache 只接受
|
||
# npm / yarn / pnpm,传布尔值会报 "Caching for 'false' is not supported"。
|
||
# 需要缓存时改成 cache: npm(要求 Runner 开了缓存服务)。
|
||
|
||
- name: 安装依赖
|
||
working-directory: web
|
||
run: npm ci --no-audit --no-fund
|
||
|
||
- name: 构建静态站点
|
||
working-directory: web
|
||
run: npm run build:static
|