Skip to content

Commit e529ea3

Browse files
committed
fix: 隔离注册令牌缓存,修复多组织环境下的令牌冲突
1 parent 865e69a commit e529ea3

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

runner.sh

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,21 @@ shell_get_reg_token() {
288288
local now ts cached_token
289289
now=$(date +%s)
290290

291-
if [[ -f "$REG_TOKEN_CACHE_FILE" ]]; then
292-
ts=$(head -n1 "$REG_TOKEN_CACHE_FILE" 2>/dev/null || true)
293-
cached_token=$(sed -n '2p' "$REG_TOKEN_CACHE_FILE" 2>/dev/null || true)
291+
# 先确保 ORG 已设置,以便构建正确的缓存文件名
292+
shell_get_org_and_pat
293+
294+
# 根据组织/仓库构建缓存文件名,支持多组织场景
295+
local cache_suffix=""
296+
if [[ -n "${REPO:-}" ]]; then
297+
cache_suffix=".${ORG}.${REPO}"
298+
elif [[ -n "${ORG:-}" ]]; then
299+
cache_suffix=".${ORG}"
300+
fi
301+
local token_cache_file="${REG_TOKEN_CACHE_FILE}${cache_suffix}"
302+
303+
if [[ -f "$token_cache_file" ]]; then
304+
ts=$(head -n1 "$token_cache_file" 2>/dev/null || true)
305+
cached_token=$(sed -n '2p' "$token_cache_file" 2>/dev/null || true)
294306
if [[ -n "$ts" && -n "$cached_token" && "$ts" =~ ^[0-9]+$ ]]; then
295307
if (( now - ts < REG_TOKEN_CACHE_TTL )); then
296308
REG_TOKEN="$cached_token"
@@ -301,19 +313,18 @@ shell_get_reg_token() {
301313
fi
302314

303315
if [[ -n "${REG_TOKEN:-}" && "${REG_TOKEN:-}" != "null" ]]; then
304-
printf '%s\n%s\n' "$now" "$REG_TOKEN" > "$REG_TOKEN_CACHE_FILE"
316+
printf '%s\n%s\n' "$now" "$REG_TOKEN" > "$token_cache_file"
305317
printf '%s\n' "$REG_TOKEN"
306318
return 0
307319
fi
308320

309-
shell_get_org_and_pat
310-
shell_info "Requesting <${ORG:-${REPO}}> registration token..." >&2
321+
shell_info "Requesting <${ORG}${REPO:+/}${REPO}> registration token..." >&2
311322
local new_token
312323
new_token="$(github_fetch_reg_token || true)"
313324
[[ -n "$new_token" && "$new_token" != "null" ]] || shell_die "Failed to fetch registration token!"
314325
REG_TOKEN="$new_token"
315326
export REG_TOKEN
316-
printf '%s\n%s\n' "$now" "$REG_TOKEN" > "$REG_TOKEN_CACHE_FILE"
327+
printf '%s\n%s\n' "$now" "$REG_TOKEN" > "$token_cache_file"
317328
# Keep compose file in sync when fetching a fresh token
318329
printf '%s\n' "$REG_TOKEN"
319330
}

0 commit comments

Comments
 (0)