forked from longtao.wu/droboticflow
173 lines
6.5 KiB
Bash
Executable File
173 lines
6.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# 分支新鲜度检查(只读,会话开始时运行:bash scripts/local/branch-freshness.sh)
|
||
#
|
||
# 对比当前检出与 origin/dev、Fanyu 线上 API/切片处理器/Worker 的 build commit,
|
||
# 落后或不包含线上版本时提示。只更新远端跟踪引用,不改分支、文件或集群;
|
||
# 任何失败都只降级为提示,始终以 0 退出,不阻断会话。
|
||
#
|
||
# 可选参数:
|
||
# --no-fetch 等同 FRESHNESS_SKIP_FETCH=1(不拉取远端引用)
|
||
# --no-cluster 等同 FRESHNESS_SKIP_CLUSTER=1(不查询线上版本)
|
||
# --base-ref <ref> 等同 FRESHNESS_BASE_REF=<ref>
|
||
#
|
||
# 可选环境变量:
|
||
# FRESHNESS_BASE_REF 对比基线(默认 origin/dev)
|
||
# FRESHNESS_SKIP_FETCH=1 不拉取远端引用
|
||
# FRESHNESS_SKIP_CLUSTER=1 不查询线上版本
|
||
# FRESHNESS_CACHE_TTL 线上版本号缓存秒数(默认 1800)
|
||
# FRESHNESS_CACHE_FILE / FRESHNESS_KUBECONFIG / FRESHNESS_KUBECTL 测试或本机覆盖
|
||
|
||
set -u
|
||
|
||
skip_fetch=${FRESHNESS_SKIP_FETCH:-0}
|
||
skip_cluster=${FRESHNESS_SKIP_CLUSTER:-0}
|
||
cli_base_ref=""
|
||
|
||
while [ $# -gt 0 ]; do
|
||
case "$1" in
|
||
--no-fetch)
|
||
skip_fetch=1
|
||
shift
|
||
;;
|
||
--no-cluster)
|
||
skip_cluster=1
|
||
shift
|
||
;;
|
||
--base-ref)
|
||
if [ $# -lt 2 ] || [ -z "${2:-}" ] || [[ "${2:-}" == -* ]]; then
|
||
echo "错误:--base-ref 需要提供非空引用名(用法:bash scripts/local/branch-freshness.sh [--no-fetch] [--no-cluster] [--base-ref <ref>])" >&2
|
||
exit 2
|
||
fi
|
||
cli_base_ref=$2
|
||
shift 2
|
||
;;
|
||
--base-ref=*)
|
||
cli_base_ref=${1#--base-ref=}
|
||
if [ -z "${cli_base_ref}" ]; then
|
||
echo "错误:--base-ref= 需要提供非空引用名(用法:bash scripts/local/branch-freshness.sh [--no-fetch] [--no-cluster] [--base-ref <ref>])" >&2
|
||
exit 2
|
||
fi
|
||
shift
|
||
;;
|
||
*)
|
||
echo "错误:未知参数 '$1'(支持参数:--no-fetch、--no-cluster、--base-ref <ref>)" >&2
|
||
exit 2
|
||
;;
|
||
esac
|
||
done
|
||
|
||
# macOS 没有 timeout 命令;用 perl alarm 给网络调用设上限。
|
||
with_timeout() {
|
||
local seconds=$1
|
||
shift
|
||
perl -e 'alarm shift; exec @ARGV' "$seconds" "$@"
|
||
}
|
||
|
||
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||
echo "分支新鲜度检查:当前目录不是 git 仓库,跳过。"
|
||
exit 0
|
||
fi
|
||
|
||
common_dir=$(git rev-parse --path-format=absolute --git-common-dir)
|
||
main_root=$(dirname "$common_dir")
|
||
base_ref=${cli_base_ref:-${FRESHNESS_BASE_REF:-origin/dev}}
|
||
cache_file=${FRESHNESS_CACHE_FILE:-$main_root/local-data/.cache/live-build-commits.txt}
|
||
cache_ttl=${FRESHNESS_CACHE_TTL:-1800}
|
||
kubeconfig=${FRESHNESS_KUBECONFIG:-$main_root/local-data/fanyu.conf}
|
||
kubectl_bin=${FRESHNESS_KUBECTL:-kubectl}
|
||
namespace=droboticflow-fanyu
|
||
deployments="droboticflow-api droboticflow-annotation-processor droboticflow-worker"
|
||
|
||
branch=$(git branch --show-current)
|
||
[ -n "${branch}" ] || branch="(detached $(git rev-parse --short HEAD))"
|
||
warnings=()
|
||
notes=()
|
||
fetch_succeeded=0
|
||
base_verified=0
|
||
live_verified=0
|
||
|
||
# 1. 与基线分支对比
|
||
if [ "${skip_fetch}" = "1" ]; then
|
||
notes+=("未拉取远端引用(FRESHNESS_SKIP_FETCH=1 或 --no-fetch)。")
|
||
elif [ "${base_ref}" = "origin/dev" ]; then
|
||
if with_timeout 8 git fetch --quiet origin dev >/dev/null 2>&1; then
|
||
fetch_succeeded=1
|
||
else
|
||
notes+=("未能联网拉取 origin/dev,以下按本地已有引用对比。")
|
||
fi
|
||
elif [[ "${base_ref}" == origin/* ]]; then
|
||
remote_branch=${base_ref#origin/}
|
||
if with_timeout 8 git fetch --quiet origin "${remote_branch}" >/dev/null 2>&1; then
|
||
fetch_succeeded=1
|
||
else
|
||
notes+=("未能联网拉取 ${base_ref},以下按本地已有引用对比。")
|
||
fi
|
||
fi
|
||
|
||
if git rev-parse --verify --quiet "${base_ref}^{commit}" >/dev/null; then
|
||
base_verified=1
|
||
behind=$(git rev-list --count "HEAD..${base_ref}")
|
||
ahead=$(git rev-list --count "${base_ref}..HEAD")
|
||
if [ "${behind}" -gt 0 ]; then
|
||
warnings+=("当前分支 ${branch} 落后 ${base_ref} ${behind} 个提交(领先 ${ahead} 个)。")
|
||
fi
|
||
else
|
||
notes+=("本地没有 ${base_ref},未对比基线分支。")
|
||
fi
|
||
|
||
# 2. 与线上 build commit 对比(缓存,避免每次会话都访问集群)
|
||
live=""
|
||
if [ "${skip_cluster}" = "1" ]; then
|
||
notes+=("未查询线上版本(FRESHNESS_SKIP_CLUSTER=1 或 --no-cluster)。")
|
||
else
|
||
if [ -s "$cache_file" ]; then
|
||
age=$(perl -e 'print time - (stat shift)[9]' "$cache_file")
|
||
[ "$age" -lt "$cache_ttl" ] && live=$(cat "$cache_file")
|
||
fi
|
||
if [ -z "$live" ] && [ -f "$kubeconfig" ] && command -v "$kubectl_bin" >/dev/null 2>&1; then
|
||
fetched=""
|
||
for deployment in $deployments; do
|
||
commit=$(with_timeout 6 "$kubectl_bin" --kubeconfig "$kubeconfig" -n "$namespace" get deploy "${deployment}" \
|
||
-o 'jsonpath={.spec.template.spec.containers[0].env[?(@.name=="DROBOTICFLOW_BUILD_COMMIT")].value}' 2>/dev/null) || commit=""
|
||
if [ -z "$commit" ]; then
|
||
fetched=""
|
||
break
|
||
fi
|
||
fetched+="${deployment} $commit"$'\n'
|
||
done
|
||
if [ -n "$fetched" ]; then
|
||
live=$fetched
|
||
mkdir -p "$(dirname "$cache_file")" && printf '%s' "$live" >"$cache_file"
|
||
fi
|
||
fi
|
||
if [ -z "$live" ]; then
|
||
notes+=("线上版本未知(集群不可达或无本机凭据)。")
|
||
else
|
||
live_verified=1
|
||
while read -r deployment commit; do
|
||
[ -n "${deployment}" ] || continue
|
||
short=${commit:0:8}
|
||
if ! git cat-file -e "$commit^{commit}" 2>/dev/null; then
|
||
warnings+=("本地没有线上提交 ${short}(${deployment}),请先拉取对应分支再判断。")
|
||
elif ! git merge-base --is-ancestor "$commit" HEAD; then
|
||
warnings+=("当前分支不包含线上 ${deployment} 的版本 ${short}。")
|
||
fi
|
||
done <<<"$live"
|
||
fi
|
||
fi
|
||
|
||
# 3. 输出
|
||
if [ ${#warnings[@]} -gt 0 ]; then
|
||
echo "⚠ 分支新鲜度检查(${branch}):"
|
||
for line in "${warnings[@]}"; do echo " - ${line}"; done
|
||
echo " 读代码、诊断线上问题或改动前,请在包含线上版本的独立工作区进行,并告知用户。"
|
||
elif [ "$base_verified" -eq 1 ] && [ "$fetch_succeeded" -eq 1 ] && [ "$live_verified" -eq 1 ]; then
|
||
echo "分支新鲜度检查:${branch} 已是最新(包含 ${base_ref} 与线上版本)。"
|
||
elif [ "$base_verified" -eq 1 ]; then
|
||
echo "分支新鲜度检查:${branch} 仅完成本地引用比较(包含本地 ${base_ref},远端或线上版本未确认)。"
|
||
else
|
||
echo "分支新鲜度检查:${branch} 未能完成基线与线上版本确认。"
|
||
fi
|
||
for line in ${notes[@]+"${notes[@]}"}; do echo " · ${line}"; done
|
||
exit 0
|