From c98754e634b29580d0736134f0ead4a0c61cb9fe Mon Sep 17 00:00:00 2001 From: sophie <16087569+sophie_liang@user.noreply.gitee.com> Date: Tue, 9 Jun 2026 14:08:13 +0800 Subject: [PATCH] =?UTF-8?q?build(jenkins):=20=E4=BF=9D=E7=95=99=E6=9C=80?= =?UTF-8?q?=E8=BF=9110=E7=BB=84=E9=95=9C=E5=83=8F=EF=BC=8C=E8=B6=85?= =?UTF-8?q?=E8=BF=87=E8=87=AA=E5=8A=A8=E6=B8=85=E9=99=A4=E6=97=A7=E9=95=9C?= =?UTF-8?q?=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkinsfile | 5 +++- scripts/deploy_compose.sh | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index a9bdd551..e2ee2eab 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -201,6 +201,9 @@ pipeline { // PushPlus 群组编码(一对多推送到个人微信);Token 仍用 Jenkins 凭据 PUSHPLUS_DEPLOY_TOKEN PUSHPLUS_TOPIC = 'hxops666' + + // 生产机保留的可回滚镜像 tag 数量(deploy_compose.sh 按 ${ENV_NAME}- 清理旧 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} ' """ } diff --git a/scripts/deploy_compose.sh b/scripts/deploy_compose.sh index 7f148a7f..e2b193eb 100644 --- a/scripts/deploy_compose.sh +++ b/scripts/deploy_compose.sh @@ -118,6 +118,65 @@ tag_built_images() { fi } +# 保留最近 N 个 ${ENV_NAME}- 版本 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.