Files
blog/scripts/setup-cnb-remotes.sh
T
zqlit b39dc29753 chore(远端): 移除 GitHub,改为「CNB 主仓 + Gitee 辅仓」
起因:GitHub 账号被平台标记,随后仓库被按 AUP 合规条款清空 ——
远端只剩一条孤立提交(无父提交、无内容):

    c21d5669  2026-10-06 20:08:32 +0800  zqlit
    chore: remove repository content (AUP compliance)

同时远端 main 的历史与本地**完全分叉**:两边只共享 2024-07-11 的 Initial commit,
之后每个提交 SHA 都不同(远端那份 989 提交、剥离了 .env/私钥/大二进制)。
所以 `git push gh main` 无法快进,也不可能再当备份用。

改动:

- `git remote remove gh`(改动前配置存 .workbuddy-backup/git-remotes.20261006-201354.txt)
- `pushall` 别名 → `git push origin main; git push gitee main`
- `scripts/setup-cnb-remotes.sh`:参数化第二个远端为 Gitee,
  顺带把「若残留 gh remote 就清掉」做进脚本;Gitee 凭据走 wincred
- `架构总览.md`:§2 发布旅程图、§5.2 远端表、§6 遗留待办全部改指向;
  新增 §5.2 的 GitHub 退役记录(含那条孤立提交的原文)
  与 §5.3「Gitee 辅仓的两条硬约束」实测核算
- `CNB构建落地方案.md`、`README.md`:远端与 pushall 说明同步
- **发布链路一起去 GitHub 化**(这几处原先都写死了 gh):
    · `deploy/editor-api/bootstrap.sh`  GH_URL/GH_SSH_KEY → GITEE_URL/GITEE_SSH_KEY,
      remote `gh` → `gitee`,known_hosts/私钥文件名跟着改;
      保留原设计:**没给私钥就自动降级成只推 origin**,不会让每次发布都报错
    · `docker-compose.editor.yml`   PUSH_REMOTES → origin,gitee
    · `editor-api/server.mjs`       默认值 → 'origin,gitee'
    · `editor-api/README.md`        变量表同步

★ 尚未解决 / 需要决策的两条 Gitee 硬约束(详见 架构总览.md §5.3):

1. **单文件 ≤ 50MB,而 `bin/linux/hugo` 是 83.1MB**
   —— 不管怎么瘦身历史,只要它还跟踪在 HEAD 里,Gitee 一律拒收。
2. 单仓库 ≤ 500MB,而 `.git` 是 620MB
   —— 移出那个 83MB 后 HEAD ≈ 374MB,才有余量。

另:线上 editor-api 容器的 .env 仍是 `PUSH_REMOTES=origin,gh`,
且仓库里还有一个 `gh` remote。因为推送逻辑对辅仓失败只警告、不阻断发布
(`git.mjs`:主仓成即算成),所以**线上发布没有坏**;
但要真正切到 Gitee,需要等 Gitee 仓库与私钥就位后再部署一次。
2026-10-06 20:18:29 +08:00

156 lines
6.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 # 两段一起"