修正 Jenkins 发布源码校验
API 发布流水线保留上游构建 commit Jenkins 二次 checkout 改为浅拉优先并按需逐步加深 同步生产运维文档和团队排障记忆
This commit is contained in:
@@ -51,11 +51,71 @@ fetch_source_branch() {
|
||||
fi
|
||||
|
||||
echo "[jenkins-checkout-source] 尝试 Git 远端: ${remote_url:-origin}"
|
||||
if [[ -z "${COMMIT_HASH}" ]]; then
|
||||
git fetch --no-tags --prune --depth=1 origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
git fetch --no-tags --prune --depth=1 origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
}
|
||||
|
||||
is_shallow_repository() {
|
||||
[[ "$(git rev-parse --is-shallow-repository 2>/dev/null || echo false)" == "true" ]]
|
||||
}
|
||||
|
||||
resolve_requested_commit_if_on_branch() {
|
||||
local requested_commit="$1"
|
||||
local resolved_commit
|
||||
|
||||
if ! git cat-file -e "${requested_commit}^{commit}" 2>/dev/null; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
resolved_commit="$(git rev-parse "${requested_commit}^{commit}")"
|
||||
if ! git merge-base --is-ancestor "${resolved_commit}" "refs/remotes/origin/${SOURCE_BRANCH}" 2>/dev/null; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
printf "%s\n" "${resolved_commit}"
|
||||
}
|
||||
|
||||
resolve_requested_commit_with_deepen() {
|
||||
local requested_commit="$1"
|
||||
local deepen_steps_raw="${GENARRATIVE_JENKINS_CHECKOUT_DEEPEN_STEPS:-50 200 1000 5000}"
|
||||
local deepen_steps=()
|
||||
local deepen_depth
|
||||
local resolved_commit
|
||||
|
||||
# 中文注释:上游构建 commit 通常就是分支 HEAD,先吃浅克隆;确实不是浅历史内提交时再逐步加深。
|
||||
if resolved_commit="$(resolve_requested_commit_if_on_branch "${requested_commit}")"; then
|
||||
printf "%s\n" "${resolved_commit}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
read -r -a deepen_steps <<<"${deepen_steps_raw}"
|
||||
for deepen_depth in "${deepen_steps[@]}"; do
|
||||
if [[ ! "${deepen_depth}" =~ ^[0-9]+$ || "${deepen_depth}" -le 1 ]]; then
|
||||
echo "[jenkins-checkout-source] 忽略无效加深深度: ${deepen_depth}" >&2
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "[jenkins-checkout-source] 浅历史未命中 commit=${requested_commit},加深到 depth=${deepen_depth}" >&2
|
||||
if is_shallow_repository; then
|
||||
git fetch --no-tags --prune --depth="${deepen_depth}" origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
else
|
||||
git fetch --no-tags --prune origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
fi
|
||||
|
||||
if resolved_commit="$(resolve_requested_commit_if_on_branch "${requested_commit}")"; then
|
||||
printf "%s\n" "${resolved_commit}"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
if is_shallow_repository; then
|
||||
echo "[jenkins-checkout-source] 逐步加深仍未命中 commit=${requested_commit},最后尝试展开完整历史" >&2
|
||||
git fetch --unshallow --no-tags origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}" || \
|
||||
git fetch --no-tags --prune origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
else
|
||||
git fetch --no-tags --prune origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}"
|
||||
fi
|
||||
|
||||
resolve_requested_commit_if_on_branch "${requested_commit}"
|
||||
}
|
||||
|
||||
add_git_remote_candidate "${GIT_REMOTE_URL}"
|
||||
@@ -80,17 +140,11 @@ else
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n "${COMMIT_HASH}" && "$(git rev-parse --is-shallow-repository 2>/dev/null || echo false)" == "true" ]]; then
|
||||
git fetch --unshallow --no-tags origin "+refs/heads/${SOURCE_BRANCH}:refs/remotes/origin/${SOURCE_BRANCH}" || true
|
||||
fi
|
||||
|
||||
git cat-file -e "refs/remotes/origin/${SOURCE_BRANCH}^{commit}"
|
||||
|
||||
if [[ -n "${COMMIT_HASH}" ]]; then
|
||||
git cat-file -e "${COMMIT_HASH}^{commit}"
|
||||
RESOLVED_COMMIT="$(git rev-parse "${COMMIT_HASH}^{commit}")"
|
||||
if ! git merge-base --is-ancestor "${RESOLVED_COMMIT}" "refs/remotes/origin/${SOURCE_BRANCH}"; then
|
||||
echo "[jenkins-checkout-source] 指定 commit 不属于 origin/${SOURCE_BRANCH}: ${RESOLVED_COMMIT}" >&2
|
||||
if ! RESOLVED_COMMIT="$(resolve_requested_commit_with_deepen "${COMMIT_HASH}")"; then
|
||||
echo "[jenkins-checkout-source] 指定 commit 不属于 origin/${SOURCE_BRANCH}: ${COMMIT_HASH}" >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user