build(jenkins): 保留最近10组镜像,超过自动清除旧镜像

This commit is contained in:
sophie committed 2026-06-09 14:08:13 +08:00
1 parent ff7c227cc2
commit c98754e634
2 files changed
+64 -1

No files matched your search

Vendored
+4 -1
View File
@@ -201,6 +201,9 @@ pipeline {
// PushPlus 群组编码(一对多推送到个人微信);Token 仍用 Jenkins 凭据 PUSHPLUS_DEPLOY_TOKEN
PUSHPLUS_TOPIC = 'hxops666'
// 生产机保留的可回滚镜像 tag 数量(deploy_compose.sh 按 ${ENV_NAME}-<commit> 清理旧 tag)
IMAGE_TAG_RETAIN_COUNT = '10'
}
stages {
@@ -527,7 +530,7 @@ pipeline {
git checkout -B ${BRANCH_NAME} origin/${BRANCH_NAME}
git reset --hard "origin/${BRANCH_NAME}"
chmod +x scripts/deploy_compose.sh scripts/healthcheck.sh
GIT_COMMIT=${env.GIT_COMMIT} ./scripts/deploy_compose.sh prod "" "" ${FRONTEND_CHANGED} ${BACKEND_CHANGED} ${DEPLOY_CONFIG_CHANGED}
GIT_COMMIT=${env.GIT_COMMIT} IMAGE_TAG_RETAIN_COUNT=${env.IMAGE_TAG_RETAIN_COUNT} ./scripts/deploy_compose.sh prod "" "" ${FRONTEND_CHANGED} ${BACKEND_CHANGED} ${DEPLOY_CONFIG_CHANGED}
'
"""
}
+60
View File
@@ -118,6 +118,65 @@ tag_built_images() {
fi
}
# 保留最近 N 个 ${ENV_NAME}-<commit> 版本 tag,删除更旧的 tag 并清理悬空镜像层。
# 数量由 IMAGE_TAG_RETAIN_COUNT 控制(默认 10);设为 0 可关闭自动清理。
prune_old_version_tags() {
local keep_count="${IMAGE_TAG_RETAIN_COUNT:-10}"
if [[ "${keep_count}" -le 0 ]]; then
echo "IMAGE_TAG_RETAIN_COUNT=${keep_count}, skipping version tag prune."
return 0
fi
local version_prefix="${ENV_NAME}-"
declare -A tag_created=()
local repo tag created
for repo in hxops-frontend hxops-backend; do
while IFS= read -r tag; do
[[ -z "${tag}" ]] && continue
[[ "${tag}" == "local" ]] && continue
[[ "${tag}" == "${version_prefix}"* ]] || continue
created="$(docker image inspect --format '{{.Created}}' "${repo}:${tag}" 2>/dev/null || true)"
[[ -z "${created}" ]] && continue
if [[ -z "${tag_created[$tag]+x}" ]] || [[ "${created}" > "${tag_created[$tag]}" ]]; then
tag_created["${tag}"]="${created}"
fi
done < <(docker images "${repo}" --format '{{.Tag}}' 2>/dev/null || true)
done
local tag_count="${#tag_created[@]}"
if [[ "${tag_count}" -le "${keep_count}" ]]; then
echo "Version tags for '${version_prefix}*': ${tag_count} (retain ${keep_count}), no prune needed."
return 0
fi
mapfile -t sorted_tags < <(
for tag in "${!tag_created[@]}"; do
printf '%s\t%s\n' "${tag_created[$tag]}" "${tag}"
done | sort -t $'\t' -k1,1r | cut -f2
)
local idx removed=0
for (( idx=keep_count; idx<${#sorted_tags[@]}; idx++ )); do
local old_tag="${sorted_tags[$idx]}"
for repo in hxops-frontend hxops-backend; do
if docker image inspect "${repo}:${old_tag}" >/dev/null 2>&1; then
echo "Removing old version tag: ${repo}:${old_tag}"
if docker rmi "${repo}:${old_tag}" >/dev/null 2>&1; then
removed=$((removed + 1))
else
echo " (kept ${repo}:${old_tag}: still referenced by a container or other tag)"
fi
fi
done
done
if [[ "${removed}" -gt 0 ]]; then
docker image prune -f >/dev/null 2>&1 || true
fi
echo "Version tag prune done: kept latest ${keep_count} '${version_prefix}*' tag(s), removed ${removed} tag reference(s)."
}
# 基础文件检查:compose 文件必须存在
if [[ ! -f "${COMPOSE_FILE}" ]]; then
echo "Missing compose file: ${COMPOSE_FILE}"
@@ -223,6 +282,7 @@ else
export COMPOSE_DOCKER_CLI_BUILD=1
"${DC_CMD[@]}" "${compose_args[@]}" up -d --build "${services[@]}"
tag_built_images
prune_old_version_tags
fi
# Keep DB schema aligned with backend code after deployment.