Files
ISEP/docs/cloud-setup-script.sh
T
Leo 36d8e05a32 認證驗證加 timeout:掛住比失敗更糟
2026-08-21 本機隔離測試:git ls-remote 掛住近 4 分鐘不回。
setup 卡在那裡看起來像還在跑,不會有人知道它壞了。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 00:34:34 +08:00

76 lines
4.9 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
# 貼進 code-on-web「Cloud environments → 你的環境 → Setup script」欄位的內容。
# 不是 ISEP 的一部分(不會被 Claude Code 當 hook/command/skill 掃描),
# 純粹是給 leo 複製貼上的參考檔,見 docs/cloud-session-bootstrap.md。
#
# 前提(要先在同一個 Cloud environment 的 Environment variables 欄位加好):
# GITEA_TOKEN_CLAUDE_CODE ← 既有機器帳號 token,名字沿用 InkStoneCo#14 已建立的那把,
# 不要新造一把。值本身不寫在這支腳本或任何檔案裡。
#
# 這支腳本做兩件事:
# 1. 設定 git URL 重寫,讓任何對 git.uncle6.me 的 clone 都能用 GITEA_TOKEN_CLAUDE_CODE 認證
# (官方文件對「CI/CD 裝私有 marketplace」建議的寫法,見 references 段)。
# 2. 直接把 ISEP 裝成 user-scope plugin ——不是「複製一份」,是跟本機一樣走
# `claude plugin marketplace add` + `claude plugin install`,裝的東西
# 100% 來自 inkstone/ISEP 這個 repo 本身,沒有第二份內容。
#
# 何時跑:只在「這個 Cloud environment 第一次開 session」時跑一次,
# 跑完 Anthropic 會把整個檔案系統拍成快照,之後的 session 直接沿用快照
# (不重跑,除非改了這支腳本本身、改了 allowed network hosts、或快照滿 7 天過期)。
# ⇒ 這是唯一會讓「ISEP 改了但雲端還是舊的」重新出現的地方,
# 緩解法見 docs/cloud-session-bootstrap.md「已知限制」段。
set -euo pipefail
if [ -z "${GITEA_TOKEN_CLAUDE_CODE:-}" ]; then
echo "❌ 找不到 GITEA_TOKEN_CLAUDE_CODE —— 去 Cloud environment 的 Environment variables 加這個名字" >&2
exit 1
fi
# ── git 認證:三個機制都設,因為它們失效的方式不同 ────────────────
# ① URL 重寫(global ② 憑證存檔(global ③ 系統層(HOME 無關,best effort
# 2026-08-20 雲端實測:session 裡 git 是「匿名」的 ⇒ setup 設的東西沒生效到 session。
# 原因未定(沒跑到/HOME 不同/快照沒帶),所以三條都設,並在下面自我驗證。
git config --global url."https://claude-code:${GITEA_TOKEN_CLAUDE_CODE}@git.uncle6.me/".insteadOf \
"https://git.uncle6.me/"
git config --global credential.helper store
printf 'https://claude-code:%s@git.uncle6.me\n' "$GITEA_TOKEN_CLAUDE_CODE" > "$HOME/.git-credentials"
chmod 600 "$HOME/.git-credentials"
git config --system url."https://claude-code:${GITEA_TOKEN_CLAUDE_CODE}@git.uncle6.me/".insteadOf \
"https://git.uncle6.me/" 2>/dev/null || echo "system 層設不了,只靠 global——非致命)"
# ── 🔴 自我驗證一:認證真的通了嗎 ──────────────────────────
# 這一步是 2026-08-20 事故的直接產物:舊版設完就結束,token 沒生效也不出聲,
# 於是雲端 session 開起來才發現 marketplace 拉不下來,而 setup log 一片綠。
echo "── 驗證 git 認證 ──"
# 加 timeout2026-08-21 本機隔離測試時 ls-remote 掛住近 4 分鐘不回,
# 而「掛住」比「失敗」更糟——setup 會卡在那裡,看起來像還在跑。
if command -v timeout >/dev/null 2>&1; then TO="timeout 45"; else TO=""; fi
if GIT_TERMINAL_PROMPT=0 $TO git ls-remote https://git.uncle6.me/inkstone/ISEP.git >/dev/null 2>&1; then
echo "✅ git 認證通:拉得到 inkstone/ISEP"
else
echo "❌ git 認證不通——marketplace 一定裝不起來,後面不用往下做了。" >&2
echo " 檢查:GITEA_TOKEN_CLAUDE_CODE 的值對不對、那把 token 有沒有被撤銷。" >&2
exit 1
fi
# ── 安裝(user scope 只是備援)─────────────────────────────
# 🔴 官方文件(cloud-environments 的 what-carries-over 表)明文:
# 「Plugins enabled only in your user settings」→ **不會**帶到雲端 session。
# 真正生效的是薄殼 repo 的 .claude/settings.json 裡的 enabledPlugins extraKnownMarketplaces。
# 這兩行留著當本地備援,不是主要路徑——不要以為裝完就等於雲端有了。
claude plugin marketplace add https://git.uncle6.me/inkstone/ISEP.git --scope user 2>/dev/null || true
claude plugin install isep@inkstone --scope user 2>/dev/null || true
# ── 🔴 自我驗證二:marketplace 真的拉下來了嗎 ────────────────
echo "── 驗證 marketplace ──"
if claude plugin marketplace list 2>/dev/null | grep -q "inkstone"; then
echo "✅ marketplace inkstone 已就位"
else
echo "❌ marketplace 沒就位——session 啟動時 enabledPlugins 會是一張跳票的支票。" >&2
exit 1
fi
echo "✅ setup 完成。session 啟動後請用「有鑑別力的探針」驗閘,"
echo " 不要用 git tag(它在三支閘的白名單裡,閘死了也會過)。"