Merge pull request 'fix(ci): trap EXIT 回收集成测试 PG/Redis 容器,杜绝取消/失败泄漏' (#1946) from fix/ci-service-container-cleanup-trap into develop
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 2s
CI/CD Pipeline / Check push changed paths (push) Successful in 15s
CI/CD Pipeline / Build Staging API Image (push) Successful in 56s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 29s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 3m10s
CI/CD Pipeline / Integration Tests (push) Successful in 4m14s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 4m5s
CI/CD Pipeline / Validate - Style (push) Successful in 4m48s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 49s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m6s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 7m43s
CI/CD Pipeline / Validate - Security (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / CI Gate (push) Has been cancelled
CI/CD Pipeline / PR Build Web Image (push) Failing after 8h31m19s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 8h25m57s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 8h30m32s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 8h25m58s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 8h30m29s
CI/CD Pipeline / PR Build API Image (push) Failing after 8h30m29s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 8h25m58s
CI/CD Pipeline / Frontend Lint (push) Failing after 8h30m16s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 2s
CI/CD Pipeline / Check push changed paths (push) Successful in 15s
CI/CD Pipeline / Build Staging API Image (push) Successful in 56s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 29s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 3m10s
CI/CD Pipeline / Integration Tests (push) Successful in 4m14s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 4m5s
CI/CD Pipeline / Validate - Style (push) Successful in 4m48s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 49s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m6s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 7m43s
CI/CD Pipeline / Validate - Security (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / CI Gate (push) Has been cancelled
CI/CD Pipeline / PR Build Web Image (push) Failing after 8h31m19s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 8h25m57s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 8h30m32s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 8h25m58s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 8h30m29s
CI/CD Pipeline / PR Build API Image (push) Failing after 8h30m29s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 8h25m58s
CI/CD Pipeline / Frontend Lint (push) Failing after 8h30m16s
This commit was merged in pull request #1946.
This commit is contained in:
@@ -120,6 +120,29 @@ wait_tcp_ready() {
|
||||
return 1
|
||||
}
|
||||
|
||||
# --- 幂等容器清理(无论成功/失败/被取消都回收临时 PG、Redis,杜绝泄漏)---
|
||||
# 背景:服务容器用 docker run -d 起在宿主机上,仅在脚本走到结尾时清理;
|
||||
# job 失败(set -e)或被取消(SIGTERM)时会永久残留,堆积压垮构建机。
|
||||
cleanup_containers() {
|
||||
# 清理过程自身不能再次触发退出,避免掩盖原始退出码
|
||||
set +e
|
||||
if [ -n "${PG_CONTAINER:-}" ]; then
|
||||
docker rm -f "$PG_CONTAINER" >/dev/null 2>&1 && echo "✅ 已清理PG容器: $PG_CONTAINER"
|
||||
fi
|
||||
if [ -n "${REDIS_CONTAINER:-}" ]; then
|
||||
docker rm -f "$REDIS_CONTAINER" >/dev/null 2>&1 && echo "✅ 已清理Redis容器: $REDIS_CONTAINER"
|
||||
fi
|
||||
}
|
||||
on_exit() {
|
||||
local code=$?
|
||||
cleanup_containers
|
||||
exit "$code"
|
||||
}
|
||||
# 必须在启动任何服务容器之前注册;INT/TERM 覆盖 Gitea 取消任务场景
|
||||
trap on_exit EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
# --- 启动 Redis ---
|
||||
echo ""
|
||||
echo "=== 启动 Redis ==="
|
||||
@@ -302,14 +325,10 @@ conn.close()
|
||||
" 2>/dev/null || echo "WARN: 数据库清理失败(可能已被清理)"
|
||||
echo "✅ 共享PG数据库已清理"
|
||||
else
|
||||
# 清理临时PG容器
|
||||
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
|
||||
echo "✅ PG容器已清理"
|
||||
# 临时PG容器由 EXIT trap 的 cleanup_containers 统一回收(失败/取消也保证清理)
|
||||
:
|
||||
fi
|
||||
|
||||
# 清理Redis容器
|
||||
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
|
||||
echo "✅ Redis容器已清理"
|
||||
# 临时Redis容器同样由 EXIT trap 统一回收
|
||||
|
||||
# --- 覆盖率汇总 ---
|
||||
echo ""
|
||||
|
||||
@@ -16,6 +16,17 @@ CONTAINER_NAME="staging-${MODE}-$$"
|
||||
# 强制清理可能残留的同名容器
|
||||
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
||||
|
||||
# 任何退出路径(成功/失败/被取消 SIGTERM)都回收容器,杜绝 staging 测试容器泄漏
|
||||
cleanup_container() {
|
||||
local code=$?
|
||||
set +e
|
||||
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 && echo "✅ 已清理容器: $CONTAINER_NAME"
|
||||
exit "$code"
|
||||
}
|
||||
trap cleanup_container EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
if [ "$MODE" = "e2e" ]; then
|
||||
docker create --name "$CONTAINER_NAME" --ipc=host \
|
||||
-e E2E_BASE_URL=https://staging.xiaoxiajianji.com \
|
||||
@@ -44,7 +55,5 @@ docker cp apps "$CONTAINER_NAME:/workspace/"
|
||||
docker start -a "$CONTAINER_NAME"
|
||||
EXIT_CODE=$(docker wait "$CONTAINER_NAME")
|
||||
|
||||
# 清理容器
|
||||
docker rm "$CONTAINER_NAME" 2>/dev/null || true
|
||||
|
||||
# 容器由 EXIT trap 的 cleanup_container 统一回收(失败/取消也保证清理)
|
||||
exit "$EXIT_CODE"
|
||||
|
||||
Reference in New Issue
Block a user