fix(role): 檢查腳本自行載入設定,修正 cron 環境取不到變數

晨間狀態檢查在 cron 下實際不會運作:cron 沒有互動 shell 的環境變數,
而 ~/.bashrc 多數在非互動時提早 return,導致範例腳本永遠拿不到 GITEA_TOKEN
而安靜結束,功能等於無效。

- examples/check-gitea-prs.sh 新增 load_env_var():依序從 ~/.roles/.env、
  ~/.bashrc、~/.profile 只抽取所需變數的那一行並 eval 該行賦值,
  不要求使用者把權杖複製到新檔案,也不必寫進 crontab
- role skill 補設定來源說明:非機密放 ~/.roles/.env(權限 600),
  權杖留在原本位置不要複製副本
- 版號 0.0.4 → 0.0.5

驗證:以 env -i 模擬 cron 環境(完全無環境變數)執行,成功取得設定並列出
待合併 PR;確認輸出無疑似權杖字串、.env 不含權杖。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-07-29 09:12:15 +08:00
co-authored by Claude Opus 5
parent e10865ced7
commit 70efcd55f2
5 changed files with 39 additions and 5 deletions
+28 -1
View File
@@ -8,17 +8,44 @@
# cp check-gitea-prs.sh ~/.roles/YUI01.checks/
# chmod +x ~/.roles/YUI01.checks/check-gitea-prs.sh
#
# 設定(環境變數,需寫進 crontab 或 ~/.roles 的環境,cron 沒有互動 shell 的變數):
# 設定(環境變數):
# GITEA_HOST Gitea 站台,例如 https://gitea.example.com
# GITEA_TOKEN 存取權杖(本腳本不會輸出它;晨間檢查寫入記憶前仍會再遮蔽一次)
# GITEA_REPOS 逗號分隔的 owner/repo 清單,例如 plugins/generic,plugins/code
#
# cron 沒有互動 shell 的環境變數,且 ~/.bashrc 多數在非互動時會提早 return,
# 因此本腳本會依序從 ~/.roles/.env、~/.bashrc、~/.profile **只抽取所需變數的那一行**,
# 不要求使用者把權杖複製到新檔案,也不必寫進 crontab。
# 建議把非機密設定(HOSTREPOS)放 ~/.roles/.env,權杖留在原本的位置。
#
# 慣例:**沒有需要回報的事情就不要輸出任何內容**。晨間檢查只在有輸出時才寫記憶,
# 靜默即代表「一切正常,不必打擾使用者」。
# ==============================================================================
set -u
# 從使用者既有的設定檔補齊未設定的變數。只取用「NAME=」開頭的那一行並 eval 該行賦值,
# 風險等同使用者自己 source 這些檔案;不會讀取或輸出其他內容。
load_env_var() {
local name="$1" file line current
eval "current=\${$name:-}"
[ -n "$current" ] && return 0
for file in "$HOME/.roles/.env" "$HOME/.bashrc" "$HOME/.profile"; do
[ -f "$file" ] || continue
line="$(grep -m1 -E "^[[:space:]]*(export[[:space:]]+)?${name}=" "$file" 2>/dev/null)" || true
[ -n "$line" ] || continue
eval "$(printf '%s' "$line" | sed -E 's/^[[:space:]]*export[[:space:]]+//')" 2>/dev/null || continue
export "$name"
eval "current=\${$name:-}"
[ -n "$current" ] && return 0
done
return 0
}
load_env_var GITEA_HOST
load_env_var GITEA_TOKEN
load_env_var GITEA_REPOS
HOST="${GITEA_HOST:-}"
TOKEN="${GITEA_TOKEN:-}"
REPOS="${GITEA_REPOS:-}"