fix: restore migration bootstrap secret on deploy failure
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
kdletters
2026-05-01 23:03:50 +08:00
parent d796e7d491
commit fb6fb6e9f5
2 changed files with 37 additions and 2 deletions

View File

@@ -182,6 +182,8 @@ PRESERVED_MIGRATION_EXPORT_TOKEN=""
PRESERVED_MIGRATION_IMPORT_TOKEN=""
PRESERVED_SPACETIME_TOKEN=""
PRESERVED_SPACETIME_MAINCLOUD_TOKEN=""
DEPLOY_COMPLETED="0"
RESTORE_PREVIOUS_MIGRATION_BOOTSTRAP_SECRET_ON_FAILURE="0"
DEPLOY_ITEMS=(
".env"
".env.local"
@@ -307,9 +309,40 @@ save_previous_migration_bootstrap_secret() {
exit 1
}
chmod 600 "${target_file}" 2>/dev/null || true
RESTORE_PREVIOUS_MIGRATION_BOOTSTRAP_SECRET_ON_FAILURE="1"
echo "[jenkins-deploy] 已保存旧模块迁移引导密钥,用于 schema 冲突时导出旧库。"
}
restore_previous_migration_bootstrap_secret_on_failure() {
local exit_code=$?
local source_file=""
local target_file=""
if [[ "${exit_code}" -eq 0 || "${DEPLOY_COMPLETED}" == "1" ]]; then
return 0
fi
if [[ "${RESTORE_PREVIOUS_MIGRATION_BOOTSTRAP_SECRET_ON_FAILURE}" != "1" ]]; then
exit "${exit_code}"
fi
source_file="$(previous_migration_bootstrap_secret_file)"
target_file="${DEPLOY_DIR}/migration-bootstrap-secret.txt"
if [[ ! -f "${source_file}" ]]; then
echo "[jenkins-deploy] 部署失败,但未找到旧迁移引导密钥快照,无法恢复: ${source_file}" >&2
exit "${exit_code}"
fi
if cp "${source_file}" "${target_file}"; then
chmod 600 "${target_file}" 2>/dev/null || true
echo "[jenkins-deploy] 部署失败,已恢复旧迁移引导密钥: ${target_file}" >&2
else
echo "[jenkins-deploy] 部署失败,且恢复旧迁移引导密钥失败: ${target_file}" >&2
fi
exit "${exit_code}"
}
clear_previous_migration_bootstrap_secret() {
local target_file
@@ -358,6 +391,7 @@ fi
SOURCE_DIR="$(cd "${SOURCE_DIR}" && pwd)"
mkdir -p "${DEPLOY_DIR}"
DEPLOY_DIR="$(cd "${DEPLOY_DIR}" && pwd)"
trap restore_previous_migration_bootstrap_secret_on_failure EXIT
if [[ ! -f "${SOURCE_DIR}/start.sh" ]]; then
echo "[jenkins-deploy] 发布目录缺少 start.sh: ${SOURCE_DIR}" >&2
@@ -457,3 +491,4 @@ else
fi
echo "[jenkins-deploy] 完成"
DEPLOY_COMPLETED="1"