build(jenkins): 支持jenkins镜像回退到稳定版本

This commit is contained in:
sophie committed 2026-06-09 09:23:42 +08:00
1 parent 14caee4df6
commit 3837a8669e
2 files changed
+108 -24

No files matched your search

+50 -2
View File
@@ -8,12 +8,14 @@ set -euo pipefail
# 4: 前端是否有变更(true/false)
# 5: 后端是否有变更(true/false)
# 6: 部署配置是否有变更(true/false)
# 7: 是否跳过 flask db upgrade(true/false;镜像回滚时应为 true)
ENV_NAME="${1:-default}"
FRONTEND_IMAGE="${2:-}"
BACKEND_IMAGE="${3:-}"
FRONTEND_CHANGED="${4:-false}"
BACKEND_CHANGED="${5:-false}"
DEPLOY_CONFIG_CHANGED="${6:-false}"
SKIP_DB_MIGRATE="${7:-false}"
# 统一使用仓库根目录下的 compose 和环境文件
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -82,6 +84,40 @@ check_backend_build_inputs() {
fi
}
verify_prebuilt_image() {
local image="$1"
if [[ -z "${image}" ]]; then
echo "Prebuilt mode requires an image reference for the service being deployed."
exit 1
fi
if ! docker image inspect "${image}" >/dev/null 2>&1; then
echo "Prebuilt image not found: ${image}"
echo "List available tags on this host: docker images 'hxops-*' --format '{{.Repository}}:{{.Tag}}'"
exit 1
fi
}
tag_built_images() {
local commit_sha="${GIT_COMMIT:-}"
if [[ -z "${commit_sha}" ]]; then
echo "GIT_COMMIT is empty, skipping version image tags."
return 0
fi
local short_sha="${commit_sha:0:8}"
local version_tag="${ENV_NAME}-${short_sha}"
# 对当前 :local 镜像打版本 tag(含增量部署),便于回滚时前后端 tag 成对存在
if docker image inspect hxops-frontend:local >/dev/null 2>&1; then
docker tag hxops-frontend:local "hxops-frontend:${version_tag}"
echo "Tagged hxops-frontend:${version_tag}"
fi
if docker image inspect hxops-backend:local >/dev/null 2>&1; then
docker tag hxops-backend:local "hxops-backend:${version_tag}"
echo "Tagged hxops-backend:${version_tag}"
fi
}
# 基础文件检查:compose 文件必须存在
if [[ ! -f "${COMPOSE_FILE}" ]]; then
echo "Missing compose file: ${COMPOSE_FILE}"
@@ -159,6 +195,7 @@ fi
echo "Deploying environment: ${ENV_NAME}"
echo "Services: ${services[*]}"
echo "Prebuilt image mode: ${PREBUILT_MODE}"
echo "Skip DB migrate: ${SKIP_DB_MIGRATE}"
if printf '%s\n' "${services[@]}" | grep -qx "hxops-backend"; then
echo "Checking Oracle Instant Client directory..."
@@ -173,18 +210,29 @@ fi
# 预构建镜像模式:只启动,不构建
# 非预构建镜像模式:在目标机本地构建并启动
if [[ "${PREBUILT_MODE}" == "true" ]]; then
if printf '%s\n' "${services[@]}" | grep -qx "hxops-frontend"; then
verify_prebuilt_image "${FRONTEND_IMAGE}"
fi
if printf '%s\n' "${services[@]}" | grep -qx "hxops-backend"; then
verify_prebuilt_image "${BACKEND_IMAGE}"
fi
"${DC_CMD[@]}" "${compose_args[@]}" up -d --no-build "${services[@]}"
else
# Dockerfile 使用 apt/pip cache mount,需启用 BuildKit
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
"${DC_CMD[@]}" "${compose_args[@]}" up -d --build "${services[@]}"
tag_built_images
fi
# Keep DB schema aligned with backend code after deployment.
if printf '%s\n' "${services[@]}" | grep -qx "hxops-backend"; then
echo "Running database migrations (flask db upgrade)..."
"${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-backend flask --app manage.py db upgrade
if [[ "${SKIP_DB_MIGRATE}" == "true" ]]; then
echo "SKIP_DB_MIGRATE=true, skipping flask db upgrade."
else
echo "Running database migrations (flask db upgrade)..."
"${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-backend flask --app manage.py db upgrade
fi
fi
"${ROOT_DIR}/scripts/healthcheck.sh" "${ENV_NAME}" "${COMPOSE_FILE}" "${ENV_FILE}"