# 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//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:各包共用一个库,串行执行避免跨包数据竞争。 # -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