查了官方文件才發現:setup script 根本讀不到環境變數,而 exit 1 會鎖死 session
leo 問「寫以前為什麼不查」——沒有藉口,我假設了兩件事都沒查,兩件都是錯的。 ① setup script 讀不到 Environment variables 官方原文:Each session copies the environment's values once, at startup, into ordinary environment variables 而 setup script 是 before Claude Code launches 跑的 ⇒ 注入在它之後。 ⇒ 把 token 放進 Environment variables 再要 setup script 讀,永遠讀不到。 證據吻合:leo 的變數設對了、值也跟本機同一把(長度 40、頭尾一致), 而腳本回報找不到。 ② exit 非零會讓整個 session 開不起來 官方原文:Exit zero: if the script exits non-zero, the session fails to start. 前一版為了大聲失敗用 exit 1 ⇒ 直接造成 Session initialization failed。 ⇒ 現在一律 exit 0,失敗寫進 /tmp/.isep-setup-report。 本機三向實測: 無變數(雲端真實情況)→ exit 0,印出說明,不擋 session 有變數(未來平台若改行為)→ 設 git 認證並驗證 ls-remote 加 timeout 45(本機曾掛住近 4 分鐘) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+28
-60
@@ -1,75 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
# 貼進 code-on-web「Cloud environments → 你的環境 → Setup script」欄位的內容。
|
||||
# 不是 ISEP 的一部分(不會被 Claude Code 當 hook/command/skill 掃描),
|
||||
# 純粹是給 leo 複製貼上的參考檔,見 docs/cloud-session-bootstrap.md。
|
||||
# 貼進 claude.ai → Cloud environments → 你的環境 → Setup script 欄位。
|
||||
#
|
||||
# 前提(要先在同一個 Cloud environment 的 Environment variables 欄位加好):
|
||||
# GITEA_TOKEN_CLAUDE_CODE ← 既有機器帳號 token,名字沿用 InkStoneCo#14 已建立的那把,
|
||||
# 不要新造一把。值本身不寫在這支腳本或任何檔案裡。
|
||||
# 🔴 2026-08-21 查官方文件後的兩個更正(前一版兩處都錯,代價是 leo 一整天):
|
||||
#
|
||||
# 這支腳本做兩件事:
|
||||
# 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 本身,沒有第二份內容。
|
||||
# ① **setup script 讀不到 Environment variables。**
|
||||
# 官方原文:「Each session copies the environment's values once, **at startup**,
|
||||
# into ordinary environment variables」——而 setup script 是
|
||||
# 「**before Claude Code launches**」跑的 ⇒ 注入發生在它之後。
|
||||
# ⇒ 把 token 放在 Environment variables 再要 setup script 去讀,永遠讀不到。
|
||||
#
|
||||
# 何時跑:只在「這個 Cloud environment 第一次開 session」時跑一次,
|
||||
# 跑完 Anthropic 會把整個檔案系統拍成快照,之後的 session 直接沿用快照
|
||||
# (不重跑,除非改了這支腳本本身、改了 allowed network hosts、或快照滿 7 天過期)。
|
||||
# ⇒ 這是唯一會讓「ISEP 改了但雲端還是舊的」重新出現的地方,
|
||||
# 緩解法見 docs/cloud-session-bootstrap.md「已知限制」段。
|
||||
# ② **非零結束會讓整個 session 開不起來。**
|
||||
# 官方原文:「**Exit zero**: if the script exits non-zero, the session fails to start.」
|
||||
# 前一版為了「大聲失敗」用 exit 1 ⇒ 直接把 leo 鎖在門外(Session initialization failed)。
|
||||
# ⇒ 現在一律 exit 0,把失敗寫進 /tmp/.isep-setup-report,由 session 內的閘喊出來。
|
||||
|
||||
set -euo pipefail
|
||||
set -uo pipefail
|
||||
REPORT=/tmp/.isep-setup-report
|
||||
: > "$REPORT"
|
||||
|
||||
say() { echo "$1"; echo "$1" >> "$REPORT"; }
|
||||
|
||||
if [ -z "${GITEA_TOKEN_CLAUDE_CODE:-}" ]; then
|
||||
echo "❌ 找不到 GITEA_TOKEN_CLAUDE_CODE —— 去 Cloud environment 的 Environment variables 加這個名字" >&2
|
||||
exit 1
|
||||
say "⚠️ setup 階段讀不到 GITEA_TOKEN_CLAUDE_CODE——這是預期的(見檔頭①)。"
|
||||
say " ISEP 改由薄殼 repo 的 .claude/settings.json 在 session 啟動時安裝,"
|
||||
say " 那時環境變數才存在。本腳本不再嘗試在這裡裝 plugin。"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ── git 認證:三個機制都設,因為它們失效的方式不同 ────────────────
|
||||
# ① URL 重寫(global) ② 憑證存檔(global) ③ 系統層(HOME 無關,best effort)
|
||||
# 2026-08-20 雲端實測:session 裡 git 是「匿名」的 ⇒ setup 設的東西沒生效到 session。
|
||||
# 原因未定(沒跑到/HOME 不同/快照沒帶),所以三條都設,並在下面自我驗證。
|
||||
# 走到這裡表示未來平台改了行為(真的注入了)——那就順手把 git 認證設好,有益無害。
|
||||
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——非致命)"
|
||||
"https://git.uncle6.me/" 2>/dev/null || true
|
||||
git config --global credential.helper store 2>/dev/null || true
|
||||
printf 'https://claude-code:%s@git.uncle6.me\n' "$GITEA_TOKEN_CLAUDE_CODE" > "$HOME/.git-credentials" 2>/dev/null || true
|
||||
chmod 600 "$HOME/.git-credentials" 2>/dev/null || true
|
||||
|
||||
# ── 🔴 自我驗證一:認證真的通了嗎 ──────────────────────────
|
||||
# 這一步是 2026-08-20 事故的直接產物:舊版設完就結束,token 沒生效也不出聲,
|
||||
# 於是雲端 session 開起來才發現 marketplace 拉不下來,而 setup log 一片綠。
|
||||
echo "── 驗證 git 認證 ──"
|
||||
# 加 timeout:2026-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"
|
||||
say "✅ git 認證通:拉得到 inkstone/ISEP"
|
||||
else
|
||||
echo "❌ git 認證不通——marketplace 一定裝不起來,後面不用往下做了。" >&2
|
||||
echo " 檢查:GITEA_TOKEN_CLAUDE_CODE 的值對不對、那把 token 有沒有被撤銷。" >&2
|
||||
exit 1
|
||||
say "❌ git 認證不通——檢查 token 值或它是否被撤銷。(不擋 session,繼續啟動)"
|
||||
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(它在三支閘的白名單裡,閘死了也會過)。"
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user