# 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 下工具链 name: ci on: push: branches: [main] permissions: contents: read jobs: backend: runs-on: ubuntu-latest-amd64 timeout-minutes: 40 services: postgres: image: postgres:16-alpine env: POSTGRES_USER: artex POSTGRES_PASSWORD: artex POSTGRES_DB: artex # 同时声明端口映射:runner 是 host 网络时服务名解析不了, # 只能靠发布到 127.0.0.1:55432 连进去。用高位端口避免撞宿主机的 postgres。 ports: - "55432:5432" steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: "1.26" # setup-go 的 cache 是布尔输入,false 表示不做缓存(依赖 Runner 的缓存服务)。 cache: false - name: 安装 psql 客户端 run: | command -v psql >/dev/null 2>&1 && exit 0 apt-get update -qq apt-get install -y -qq postgresql-client - name: 探测 postgres 并确定连接串 run: | # 两种网络模式都要兼容: # bridge 模式 → 服务名 postgres 可解析 # host 模式 → 服务名不解析,走发布到宿主机的 127.0.0.1:55432 for hp in "postgres:5432" "127.0.0.1:55432"; do host="${hp%:*}" port="${hp#*:}" echo "== 探测 ${host}:${port}" for _ in $(seq 1 15); do if pg_isready -h "$host" -p "$port" -U artex >/dev/null 2>&1; then echo "已连上 ${host}:${port}" { echo "PGHOST=${host}" echo "PGPORT=${port}" echo "PGUSER=artex" echo "PGDATABASE=artex" echo "PGPASSWORD=artex" echo "ARTEX_PG_DSN=postgres://artex:artex@${host}:${port}/artex?sslmode=disable" } >> "$GITHUB_ENV" exit 0 fi sleep 2 done done echo "postgres 不可达:postgres:5432 与 127.0.0.1:55432 都没连上" >&2 echo "域名解析结果:$(getent hosts postgres 2>/dev/null || echo '未解析')" >&2 exit 1 - 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:各包共用一个库,串行执行避免跨包数据竞争。 # -skip:TestGraphOverviewExpandsAssociatedCompanyScope 在当前代码上 # 已失败(coverage["scope"] 为 nil),与本流程无关,修好后请删掉。 go test -p 1 ./... -count=1 -skip 'TestGraphOverviewExpandsAssociatedCompanyScope' 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