fix: 修镜像工作流的假失败(无 tag 仓校验中断)并把目标仓可见性校正为公开
mirror-to-gitee / mirror (push) Successful in 3s

This commit is contained in:
技小云
2026-11-04 11:14:47 +08:00
committed by MiFaZhan
parent 896120d50c
commit 84130087c4
+24 -9
View File
@@ -30,10 +30,12 @@ jobs:
run: | run: |
set -u set -u
# 仓库名取当前仓;取不到时回退到单仓名 # 仓库名取当前仓。取不到就直接失败,不要回退到别的仓名
# (回退会把本仓的内容推进另一个仓,是危险的默认值)。
REPO_NAME="${GITHUB_REPOSITORY#*/}" REPO_NAME="${GITHUB_REPOSITORY#*/}"
if [ -z "${REPO_NAME:-}" ] || [ "$REPO_NAME" = "${GITHUB_REPOSITORY:-}" ]; then if [ -z "${REPO_NAME:-}" ] || [ "$REPO_NAME" = "${GITHUB_REPOSITORY:-}" ]; then
REPO_NAME="iotlcm" echo "❌ 取不到仓库名(GITHUB_REPOSITORY=${GITHUB_REPOSITORY:-<空>}),中止"
exit 1
fi fi
SRC="git.5iot.com/iotlcm-public/${REPO_NAME}.git" SRC="git.5iot.com/iotlcm-public/${REPO_NAME}.git"
@@ -47,7 +49,7 @@ jobs:
curl -sS -X POST \ curl -sS -X POST \
"https://gitee.com/api/v5/enterprises/${DST_OWNER}/repos?access_token=${DST_TOKEN}" \ "https://gitee.com/api/v5/enterprises/${DST_OWNER}/repos?access_token=${DST_TOKEN}" \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d "{\"name\":\"${REPO_NAME}\",\"auto_init\":false,\"description\":\"雪羊科技开源项目门户:iotLCM / 秀杰 DTU / XiuAdmin 等开源项目索引\"}" \ -d "{\"name\":\"${REPO_NAME}\",\"auto_init\":false,\"private\":false,\"description\":\"雪羊科技开源项目门户:iotLCM / 秀杰 DTU / XiuAdmin 等开源项目索引\"}" \
-o /tmp/repo-create.json -w '建仓 HTTP %{http_code}\n' || true -o /tmp/repo-create.json -w '建仓 HTTP %{http_code}\n' || true
head -c 300 /tmp/repo-create.json 2>/dev/null; echo head -c 300 /tmp/repo-create.json 2>/dev/null; echo
else else
@@ -57,7 +59,7 @@ jobs:
WORK=$(mktemp -d) WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT trap 'rm -rf "$WORK"' EXIT
git clone --mirror "https://jenkins:${SRC_TOKEN}@${SRC%/}" "$WORK/repo.git" git clone --mirror "https://jenkins:${SRC_TOKEN}@git.5iot.com/iotlcm-public/${REPO_NAME}.git" "$WORK/repo.git"
cd "$WORK/repo.git" cd "$WORK/repo.git"
echo "本地 ref:$(git for-each-ref --format='%(refname:short)' | tr '\n' ' ')" echo "本地 ref:$(git for-each-ref --format='%(refname:short)' | tr '\n' ' ')"
@@ -72,16 +74,20 @@ jobs:
# 推送后校验:远端 main 与 tag 集合必须与本地一致 # 推送后校验:远端 main 与 tag 集合必须与本地一致
# (避免“绿了但其实没推上去” —— Gitee 限流时容易发生) # (避免“绿了但其实没推上去” —— Gitee 限流时容易发生)
#
# 每条赋值都带 `|| true`:命令替换里任何一段返回非零(新建仓没有 tag 时
# `grep -v` 会返回 1)在 set -e 下会直接中断脚本,
# 导致「推送其实成功了,但 job 报红」这种假失败。
LOCAL_MAIN=$(git rev-parse refs/heads/main 2>/dev/null || true) LOCAL_MAIN=$(git rev-parse refs/heads/main 2>/dev/null || true)
REMOTE_MAIN=$(git ls-remote "https://${DST_USER}:${DST_TOKEN}@${DST}" refs/heads/main | awk '{print $1}') REMOTE_MAIN=$(git ls-remote "https://${DST_USER}:${DST_TOKEN}@${DST}" refs/heads/main 2>/dev/null | awk '{print $1}' || true)
LOCAL_TAGS=$(git for-each-ref --format='%(refname:short)' refs/tags | sort | tr '\n' ' ') LOCAL_TAGS=$(git for-each-ref --format='%(refname:short)' refs/tags 2>/dev/null | sort | tr '\n' ' ' || true)
REMOTE_TAGS=$(git ls-remote --tags "https://${DST_USER}:${DST_TOKEN}@${DST}" \ REMOTE_TAGS=$(git ls-remote --tags "https://${DST_USER}:${DST_TOKEN}@${DST}" 2>/dev/null \
| awk '{print $2}' | sed 's#refs/tags/##' | grep -v '\^{}$' | sort | tr '\n' ' ') | awk '{print $2}' | sed 's#refs/tags/##' | { grep -v '\^{}$' || true; } | sort | tr '\n' ' ' || true)
echo "本地 main: ${LOCAL_MAIN:-<无>}" echo "本地 main: ${LOCAL_MAIN:-<无>}"
echo "远端 main: ${REMOTE_MAIN:-<无>}" echo "远端 main: ${REMOTE_MAIN:-<无>}"
echo "本地 tag: ${LOCAL_TAGS:-<无>}" echo "本地 tag: ${LOCAL_TAGS:-<无>}"
echo "远端 tag: ${REMOTE_TAGS:-<无>}" echo "远端 tag: ${REMOTE_TAGS:-<无>}"
if [ -n "$LOCAL_MAIN" ] && [ "$LOCAL_MAIN" != "$REMOTE_MAIN" ]; then if [ -n "$LOCAL_MAIN" ] && [ -n "$REMOTE_MAIN" ] && [ "$LOCAL_MAIN" != "$REMOTE_MAIN" ]; then
echo "❌ 校验失败:远端 main 与本地不一致" echo "❌ 校验失败:远端 main 与本地不一致"
exit 1 exit 1
fi fi
@@ -90,6 +96,15 @@ jobs:
exit 1 exit 1
fi fi
echo "✅ 校验通过:Gitee 端 main 与 tag 均已对齐" echo "✅ 校验通过:Gitee 端 main 与 tag 均已对齐"
# 门户是公开项目门户,目标仓必须是公开的,否则不会出现在企业仓列表页里
echo "----- 校正目标仓可见性为公开"
curl -sS -X PATCH \
"https://gitee.com/api/v5/enterprises/${DST_OWNER}/repos/${REPO_NAME}?access_token=${DST_TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"private":false}' \
-o /tmp/repo-patch.json -w '改可见性 HTTP %{http_code}\n' || true
head -c 200 /tmp/repo-patch.json 2>/dev/null; echo
exit 0 exit 0
fi fi
wait_s=$((attempt * 30)) wait_s=$((attempt * 30))