ci: backend 改为在 job 内启动 PostgreSQL
ci / frontend (push) Successful in 53s
ci / backend (push) Failing after 2m6s

这台 runner 的 services 不可用(服务名解析不了、端口也没发布到 job),
改为装 PostgreSQL 并 initdb 一个临时集群:

- apt 安装 postgresql,顺带提供 psql 客户端,去掉单独的安装步骤
- root 下切 postgres 用户 initdb --auth=trust,pg_ctl 起在 127.0.0.1:55432
- 把 PG* 与 ARTEX_PG_DSN 写入 GITHUB_ENV,后续步骤与测试直接使用
- 已在 debian:bookworm-slim 容器内实跑验证整段脚本
This commit is contained in:
wangdefa committed 2026-10-20 22:03:30 -07:00
1 parent ba2f4b4b06
commit d26dcd7621
1 file changed
+43 -43
+43 -43
View File
@@ -7,6 +7,9 @@
# 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:
@@ -20,17 +23,6 @@ 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
@@ -40,40 +32,48 @@ jobs:
# setup-go 的 cache 是布尔输入,false 表示不做缓存(依赖 Runner 的缓存服务)。
cache: false
- name: 安装 psql 客户端
- name: 启动 PostgreSQL
run: |
command -v psql >/dev/null 2>&1 && exit 0
apt-get update -qq
apt-get install -y -qq postgresql-client
set -euo pipefail
- 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
# 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: |