Files
blog/scripts/setup-cnb-remotes.sh
T

155 lines
6.0 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# 配置远端:CNB 主仓 + Gitee 辅仓(异地备份,不参与构建)
#
# 历史沿革:
# 2026-10-04 从「GitHub + 自建 Gitea 双推」切到「CNB 主 + GitHub 备份」
# 2026-10-06 GitHub 账号被平台标记、仓库被按 AUP 清空 → 移除 gh,
# 改为「CNB 主 + Gitee 辅」(见 架构总览.md §5.2)
#
# 为什么不用「一个 remote 挂多个 pushurl」:
# 实测(2026-10-04)git 对多个 pushurl 是「顺序推、遇错即停」——
# 第一个失败,后面的远端一个都不会推。那就失去备份意义了。
# 故拆成两个 remote + 一个 alias,两段各自独立、互不阻塞。
#
# 用法:
# bash scripts/setup-cnb-remotes.sh <CNB_URL> [GITEE_URL]
#
# 例:
# bash scripts/setup-cnb-remotes.sh https://cnb.cool/zqlit/blog \
# https://gitee.com/zqlit/blog.git
#
# 附带令牌(直接写进 Windows 凭据管理器,以后不再输):
# CNB_TOKEN='你的令牌' GITEE_TOKEN='你的私人令牌' \
# bash scripts/setup-cnb-remotes.sh https://cnb.cool/zqlit/blog https://gitee.com/zqlit/blog.git
#
# 回退:脚本会把改动前的配置备份到 .workbuddy-backup/git-remotes.<时间戳>.txt
set -euo pipefail
CNB_URL="${1:-}"
GITEE_URL="${2:-${GITEE_URL:-}}"
if [ -z "$CNB_URL" ]; then
cat <<'USAGE'
用法:bash scripts/setup-cnb-remotes.sh <CNB_URL> [GITEE_URL]
提示:
· CNB / Gitee 仓库地址直接用仓库页地址,带不带 .git 都行
· CNB **不支持 SSH**,只能 HTTPS + 访问令牌(用户名固定填 cnb,密码填令牌)
令牌:「个人设置 → 访问令牌」,勾「代码仓库 → 读写」
· Gitee 支持 HTTPS + 私人令牌(也可用 SSH)
令牌:「设置 → 私人令牌」→ https://gitee.com/profile/personal_access_tokens
勾选 projects(仓库读写);用户名填 Gitee 用户名
· ⚠️ Gitee 免费版:单仓库 ≤ 500MB、单文件 ≤ 50MB。
本仓 `bin/linux/hugo` 是 83.1MB,**必须先移出 git** 才推得上去。
USAGE
exit 1
fi
case "$CNB_URL" in
https://cnb.cool/*) ;;
*) echo "地址看起来不是 CNB 的:$CNB_URL"; exit 1 ;;
esac
if [ -n "$GITEE_URL" ]; then
case "$GITEE_URL" in
https://gitee.com/*|git@gitee.com:*) ;;
*) echo "地址看起来不是 Gitee 的:$GITEE_URL"; exit 1 ;;
esac
fi
ROOT="$(git rev-parse --show-toplevel)"
cd "$ROOT"
STAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP=".workbuddy-backup/git-remotes.$STAMP.txt"
mkdir -p .workbuddy-backup
{
echo "# 改动前的 git 远端配置($STAMP)"
echo "# 用途:回退参考。本文件含明文凭据,勿提交、勿外传。"
echo
echo "## git remote -v"
git remote -v
echo
echo "## 原始 remote.* 配置"
git config --get-regexp '^remote\.' || true
} > "$BACKUP"
echo "原配置已备份:$BACKUP"
# 1) origin = CNB(主仓,fetch + push,触发构建)
git remote remove origin 2>/dev/null || true
git remote add origin "$CNB_URL"
# 2) gitee = 辅仓(异地备份,不参与构建)
# 没给地址就保留现有同名 remote 不动(避免把已配好的删掉)
if [ -n "$GITEE_URL" ]; then
git remote remove gitee 2>/dev/null || true
git remote add gitee "$GITEE_URL"
else
echo "未提供 GITEE_URL —— 跳过 gitee remote(现有同名远端保持不变)"
fi
# 3) gh(GitHub)已退役,若有残留直接清掉
if git remote | grep -qx gh; then
git remote remove gh
echo "已移除残留的 gh(GitHub)远端"
fi
# 4) 自建 Gitea 暂时留着当只读参考。确认新链路稳定后再手工执行:
# git remote remove gitea
# 5) 一条命令推两段。用「;」而不是「&&」——CNB 失败时照样把 Gitee 推上去
git config alias.pushall '!git push origin main; git push gitee main'
# 6) 凭据
#
# 本机全局 helper 是 GCM(git-credential-manager.exe)。实测它对 cnb.cool 这类
# 第三方 HTTPS 远端会**每次都弹窗**,而且 GCM_INTERACTIVE=never +
# GIT_TERMINAL_PROMPT=0 都压不住 —— git ls-remote 直接挂死(timeout 25s 未返回)。
# → 本仓显式改用 wincred:同样写 Windows 凭据管理器(加密),但没有 UI 弹窗。
# (另:PortableGit 未打包 git-credential-store,exec-path / mingw64/bin 都没有)
store_cred() { # host username password
printf 'protocol=https\nhost=%s\nusername=%s\npassword=%s\n\n' "$1" "$2" "$3" \
| git credential-wincred store
}
if [ -n "${CNB_TOKEN:-}" ] || [ -n "${GITEE_TOKEN:-}" ]; then
git config --local credential.helper ""
fi
if [ -n "${CNB_TOKEN:-}" ]; then
git config --local credential.https://cnb.cool.helper wincred
store_cred cnb.cool cnb "$CNB_TOKEN"
echo "CNB 凭据已写入 Windows 凭据管理器(wincred,无弹窗)"
else
echo "未提供 CNB_TOKEN,已跳过。需要时重跑:"
echo " CNB_TOKEN='你的令牌' bash scripts/setup-cnb-remotes.sh $CNB_URL"
fi
if [ -n "${GITEE_TOKEN:-}" ]; then
if [ -z "${GITEE_USER:-}" ]; then
echo "⚠️ 提供了 GITEE_TOKEN 但没给 GITEE_USER(Gitee 用户名),跳过 Gitee 凭据写入"
else
git config --local credential.https://gitee.com.helper wincred
store_cred gitee.com "$GITEE_USER" "$GITEE_TOKEN"
echo "Gitee 凭据已写入 Windows 凭据管理器(wincred)"
fi
else
echo "未提供 GITEE_TOKEN,已跳过。需要时重跑:"
echo " GITEE_USER='你的用户名' GITEE_TOKEN='你的私人令牌' bash scripts/setup-cnb-remotes.sh $CNB_URL \$GITEE_URL"
fi
echo
echo "=== 当前远端 ==="
git remote -v | sed -E 's#://[^@/]*@#://***@#g'
echo
echo "=== 推送前自检 ==="
echo " 1) 有没有超 Gitee 单文件上限(50MB)的文件:"
echo " git ls-tree -r -l HEAD | awk '\$4>52428800 {print \$4/1048576\" MB \"\$5}'"
echo " 2) .git 体积(Gitee 上限 500MB):du -sh .git"
echo
echo "=== 下一步 ==="
echo " git push origin main # 推到 CNB(触发构建)"
echo " git push gitee main # 推到 Gitee 辅仓"
echo " git pushall # 两段一起"