From 764ac311ee13c566d468f392359b7f5e2f8609b6 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 24 Jul 2026 10:52:20 +0800 Subject: [PATCH] =?UTF-8?q?chore(ci):=20=E7=AB=AF=E5=8F=A3=E4=B8=8EPG?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=B8=B8=E9=87=8F=E9=9B=86=E4=B8=AD=E7=AE=A1?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E6=B8=85=E7=90=86=E7=A1=AC=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 scripts/ci/ci_env.sh 统一管理CI共享常量(端口/用户/密码/DB名) - 3个CI脚本(validate_migration/run_validate/run_integration_tests) source ci_env.sh,移除SHARED_PG_PORT/USER/PASSWORD等硬编码 - ci-pipeline.yml新增workflow级env变量,两处DATABASE_URL从硬编码改为变量引用 - 支持通过环境变量覆盖默认值,便于后续配置调整 --- .gitea/workflows/ci-pipeline.yml | 15 +++++++++++++-- scripts/ci/ci_env.sh | 14 ++++++++++++++ scripts/ci/run_integration_tests.sh | 19 ++++++++++++------- scripts/ci/run_validate.sh | 19 ++++++++++++------- scripts/ci/validate_migration.sh | 18 +++++++++++------- 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100755 scripts/ci/ci_env.sh diff --git a/.gitea/workflows/ci-pipeline.yml b/.gitea/workflows/ci-pipeline.yml index 0a2aef80a..8889c7515 100755 --- a/.gitea/workflows/ci-pipeline.yml +++ b/.gitea/workflows/ci-pipeline.yml @@ -24,6 +24,17 @@ concurrency: group: ci-pipeline-${{ gitea.event_name }}-${{ gitea.ref }} # PR事件取消进行中的旧run,push事件不取消(确保完整CI跑完) cancel-in-progress: ${{ gitea.event_name == 'pull_request' }} +env: + CI_PG_HOST: host.docker.internal + CI_PG_PORT: "5432" + CI_PG_USER: postgres + CI_PG_PASSWORD: postgres + CI_PG_DB: xiaoxia_saas + CI_SHARED_PG_PORT: "5433" + CI_SHARED_PG_USER: postgres + CI_SHARED_PG_PASSWORD: ci_pg_2026! + CI_DEFAULT_DB: xiaoxia_saas + jobs: check-frontend-only: name: Check if frontend-only change @@ -251,7 +262,7 @@ jobs: permissions: contents: read env: - DATABASE_URL: postgresql+psycopg://postgres:postgres@host.docker.internal:5432/xiaoxia_saas + DATABASE_URL: postgresql+psycopg://${{ env.CI_PG_USER }}:${{ env.CI_PG_PASSWORD }}@${{ env.CI_PG_HOST }}:${{ env.CI_PG_PORT }}/${{ env.CI_PG_DB }} USE_IN_MEMORY_DB: 'false' CI_USE_SHARED_PG: 'true' steps: @@ -399,7 +410,7 @@ jobs: - validate-type-check - validate-migration env: - DATABASE_URL: postgresql+psycopg://postgres:postgres@host.docker.internal:5432/xiaoxia_saas + DATABASE_URL: postgresql+psycopg://${{ env.CI_PG_USER }}:${{ env.CI_PG_PASSWORD }}@${{ env.CI_PG_HOST }}:${{ env.CI_PG_PORT }}/${{ env.CI_PG_DB }} USE_IN_MEMORY_DB: 'false' CI_USE_SHARED_PG: 'true' OSS_ACCESS_KEY_ID: placeholder diff --git a/scripts/ci/ci_env.sh b/scripts/ci/ci_env.sh new file mode 100755 index 000000000..516acc952 --- /dev/null +++ b/scripts/ci/ci_env.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# CI共享环境变量与常量定义 +# 所有CI脚本source此文件获取统一的配置,避免硬编码分散 + +# === 共享常驻PG实例(CI_USE_SHARED_PG=true时使用)=== +export CI_SHARED_PG_PORT="${CI_SHARED_PG_PORT:-5433}" +export CI_SHARED_PG_USER="${CI_SHARED_PG_USER:-postgres}" +export CI_SHARED_PG_PASSWORD="${CI_SHARED_PG_PASSWORD:-ci_pg_2026!}" + +# === 本地PG默认端口(CI_USE_SHARED_PG=false时容器映射或本地PG)=== +export CI_LOCAL_PG_PORT="${CI_LOCAL_PG_PORT:-5432}" + +# === 默认数据库名 === +export CI_DEFAULT_DB="${CI_DEFAULT_DB:-xiaoxia_saas}" diff --git a/scripts/ci/run_integration_tests.sh b/scripts/ci/run_integration_tests.sh index 49aa82ad8..192b538c4 100755 --- a/scripts/ci/run_integration_tests.sh +++ b/scripts/ci/run_integration_tests.sh @@ -4,6 +4,11 @@ # 支持 pytest-xdist 并行执行:每个 worker 使用独立数据库,预期加速 2-4 倍 set -eu +# 加载CI共享常量 +SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")" +# shellcheck source=ci_env.sh +source "${SCRIPT_DIR}/ci_env.sh" + echo "=== CI Integration Tests 开始 ===" # --- 安装依赖 --- @@ -47,7 +52,7 @@ bash scripts/ci/step_install_ffmpeg.sh # 需要用宿主机IP访问映射端口 # 检测策略:host.docker.internal -> docker0桥接IP -> 容器IP直连 -> 默认网关 -> 127.0.0.1 detect_docker_host() { - local test_port="${1:-5432}" + local test_port="${1:-${CI_LOCAL_PG_PORT}}" # 候选IP列表 local candidates=() @@ -106,7 +111,7 @@ except: # 获取宿主机IP(先尝试用共享PG端口5433测试,再回退到其他端口) if [ -S /var/run/docker.sock ]; then # 先用共享PG端口5433探测 - DOCKER_HOST_IP=$(detect_docker_host 5433) + DOCKER_HOST_IP=$(detect_docker_host "${CI_SHARED_PG_PORT}") if [ "$DOCKER_HOST_IP" = "127.0.0.1" ]; then # 如果共享PG端口探测失败,说明不在DooD或共享PG不可用,再试其他端口 DOCKER_HOST_IP=$(detect_docker_host 22) @@ -182,9 +187,9 @@ if [ "$USE_SHARED_PG" = "true" ]; then # 使用常驻共享PG实例 echo "使用常驻共享PG实例(CI_USE_SHARED_PG=true)" SHARED_PG_HOST="$PG_HOST" - SHARED_PG_PORT="5433" - SHARED_PG_USER="postgres" - SHARED_PG_PASSWORD="ci_pg_2026!" + SHARED_PG_PORT="${CI_SHARED_PG_PORT}" + SHARED_PG_USER="${CI_SHARED_PG_USER}" + SHARED_PG_PASSWORD="${CI_SHARED_PG_PASSWORD}" echo "等待共享PG连接就绪..." wait_tcp_ready "$SHARED_PG_HOST" "$SHARED_PG_PORT" 5 @@ -220,9 +225,9 @@ else --health-timeout 5s \ --health-retries 12 \ postgres:16 - PG_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2) + PG_PORT=$(docker port "$PG_CONTAINER" ${CI_LOCAL_PG_PORT}/tcp | cut -d: -f2) echo "PostgreSQL port: $PG_PORT" - export DATABASE_URL="postgresql+psycopg://postgres:postgres@${PG_HOST}:${PG_PORT}/xiaoxia_saas" + export DATABASE_URL="postgresql+psycopg://${CI_SHARED_PG_USER}:${CI_SHARED_PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${CI_DEFAULT_DB}" # 等待容器健康 for i in $(seq 1 30); do diff --git a/scripts/ci/run_validate.sh b/scripts/ci/run_validate.sh index 050a82b94..c85c299a1 100755 --- a/scripts/ci/run_validate.sh +++ b/scripts/ci/run_validate.sh @@ -14,6 +14,11 @@ # 所有子任务同时启动,最后汇总结果。 set -eu +# 加载CI共享常量 +SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")" +# shellcheck source=ci_env.sh +source "${SCRIPT_DIR}/ci_env.sh" + echo "=== CI Validate: 并行化代码质量检查 ===" echo "" @@ -302,7 +307,7 @@ task_alembic() { # --- DooD模式检测:确定宿主机访问地址 --- detect_docker_host() { - local test_port="${1:-5432}" + local test_port="${1:-${CI_LOCAL_PG_PORT}}" local candidates=() # 1. host.docker.internal @@ -376,7 +381,7 @@ except: # 获取宿主机IP local PG_HOST if [ -S /var/run/docker.sock ]; then - PG_HOST=$(detect_docker_host 5433) + PG_HOST=$(detect_docker_host "${CI_SHARED_PG_PORT}") if [ "$PG_HOST" = "127.0.0.1" ]; then PG_HOST=$(detect_docker_host 22) fi @@ -394,9 +399,9 @@ except: # 使用常驻共享PG实例 echo "使用常驻共享PG实例(CI_USE_SHARED_PG=true)" local SHARED_PG_HOST="$PG_HOST" - local SHARED_PG_PORT="5433" - local SHARED_PG_USER="postgres" - local SHARED_PG_PASSWORD="ci_pg_2026!" + local SHARED_PG_PORT="${CI_SHARED_PG_PORT}" + local SHARED_PG_USER="${CI_SHARED_PG_USER}" + local SHARED_PG_PASSWORD="${CI_SHARED_PG_PASSWORD}" local CI_DB_NAME="ci_run_${GITHUB_RUN_ID:-$$}" echo "等待共享PG连接就绪..." @@ -456,9 +461,9 @@ conn.close() if [ $exit_code -eq 0 ]; then local PG_PORT - PG_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2) + PG_PORT=$(docker port "$PG_CONTAINER" ${CI_LOCAL_PG_PORT}/tcp | cut -d: -f2) echo "PostgreSQL port: $PG_PORT" - export DATABASE_URL="postgresql+psycopg://postgres:postgres@${PG_HOST}:${PG_PORT}/xiaoxia_saas" + export DATABASE_URL="postgresql+psycopg://${CI_SHARED_PG_USER}:${CI_SHARED_PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${CI_DEFAULT_DB}" # 等待容器健康 local i diff --git a/scripts/ci/validate_migration.sh b/scripts/ci/validate_migration.sh index 48243dd3d..af20dceca 100644 --- a/scripts/ci/validate_migration.sh +++ b/scripts/ci/validate_migration.sh @@ -2,12 +2,16 @@ # CI Validate: Alembic迁移验证(并行Job 3/3) # 需要PostgreSQL数据库 set -eu +# 加载CI共享常量 +SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")" +# shellcheck source=ci_env.sh +source "${SCRIPT_DIR}/ci_env.sh" echo "=== CI Validate: Alembic迁移验证 ===" # --- DooD模式检测:确定宿主机访问地址 --- detect_docker_host() { - local test_port="${1:-5432}" + local test_port="${1:-${CI_LOCAL_PG_PORT}}" local candidates=() @@ -61,7 +65,7 @@ except: # 获取宿主机IP if [ -S /var/run/docker.sock ]; then - DOCKER_HOST_IP=$(detect_docker_host 5433) + DOCKER_HOST_IP=$(detect_docker_host "${CI_SHARED_PG_PORT}") if [ "$DOCKER_HOST_IP" = "127.0.0.1" ]; then DOCKER_HOST_IP=$(detect_docker_host 22) fi @@ -98,9 +102,9 @@ if [ "$USE_SHARED_PG" = "true" ]; then # 使用常驻共享PG实例 echo "使用常驻共享PG实例(CI_USE_SHARED_PG=true)" SHARED_PG_HOST="$PG_HOST" - SHARED_PG_PORT="5433" - SHARED_PG_USER="postgres" - SHARED_PG_PASSWORD="ci_pg_2026!" + SHARED_PG_PORT="${CI_SHARED_PG_PORT}" + SHARED_PG_USER="${CI_SHARED_PG_USER}" + SHARED_PG_PASSWORD="${CI_SHARED_PG_PASSWORD}" CI_DB_NAME="ci_run_${GITHUB_RUN_ID:-$$}" echo "等待共享PG连接就绪..." @@ -152,9 +156,9 @@ else --health-timeout 3s \ --health-retries 20 \ postgres:16-alpine - PG_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2) + PG_PORT=$(docker port "$PG_CONTAINER" ${CI_LOCAL_PG_PORT}/tcp | cut -d: -f2) echo "PostgreSQL port: $PG_PORT" - export DATABASE_URL="postgresql+psycopg://postgres:postgres@${PG_HOST}:${PG_PORT}/xiaoxia_saas" + export DATABASE_URL="postgresql+psycopg://${CI_SHARED_PG_USER}:${CI_SHARED_PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${CI_DEFAULT_DB}" # 等待容器健康 for i in $(seq 1 30); do -- 2.54.0