xiaowei-system/scripts/gitea-push-watchdog.sh

52 lines
1.9 KiB
Bash
Executable File
Raw Permalink 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.

#!/bin/bash
# ============================================================
# gitea-push-watchdog.sh — Gitea 推送看门狗
# 每天由 cron 触发no_agent 模式)
# 正常时静默(已推送 = 无输出),需要推送时自动推,异常才告警
# ============================================================
# 注意:不使用 set -e网络抖动时自身兜底
# === 配置 ===
REPO_DIR="$HOME/.hermes"
GITEA_HOST="192.168.188.11:3000" # StarVPN 网段Gitea 仅走此通道
FEISHU_WEBHOOK="https://open.feishu.cn/open-apis/bot/v2/hook/446db983-e392-4d2c-bfb8-f9060e5df3ad"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
cd "$REPO_DIR" || { echo "❌ [$TIMESTAMP] cd $REPO_DIR 失败"; exit 1; }
# === 探测 Gitea 可达TCP非 ping —— 禁 ICMP 铁律) ===
if ! timeout 4 bash -c "cat < /dev/null > /dev/tcp/$GITEA_HOST" 2>/dev/null; then
# 外网常态不可达 = 静默跳过(跟 dual-backup 同判据:外网不报错)
exit 0
fi
# === fetch 远端,刷新 origin/main ===
timeout 30 git fetch origin main 2>/dev/null || { echo "⚠️ [$TIMESTAMP] git fetch 失败(网络异常?)"; exit 1; }
# === 统计未推送 commit ===
AHEAD=$(git rev-list --count origin/main..main 2>/dev/null || echo 0)
if [ "$AHEAD" = "0" ]; then
# 全部已推送 = 正常静默
exit 0
fi
# === 有未推送 → 自动推送 ===
UNPUSHED=$(git log --oneline origin/main..main 2>/dev/null | head -20)
if timeout 60 git push origin main > /tmp/gitea-push.log 2>&1; then
echo "✅ [$TIMESTAMP] Gitea 自动推送完成:$AHEAD 个 commit"
echo ""
echo "$UNPUSHED"
exit 0
else
# 推送失败 → 告警(不退出,交给 cron 投递)
echo "🔴 [$TIMESTAMP] Gitea 推送失败($AHEAD 个 commit 待推)"
echo ""
echo "$UNPUSHED"
echo ""
echo "--- push 错误 ---"
tail -5 /tmp/gitea-push.log
exit 0
fi