fix(ci): preserve gitea runner jobs on shutdown
All checks were successful
Code Review / ai-code-review (push) Successful in 46s

This commit is contained in:
Your Name
2026-05-01 16:16:27 +08:00
parent f72419dd17
commit cb5ab900c4
5 changed files with 191 additions and 7 deletions

View File

@@ -188,7 +188,8 @@ fi
# ──────────────────────────────────────────────
log "[6/6] 啟動 Gitea Act Runner..."
RUNNER_DIR="/home/wooo/act-runner"
if [ -f "$RUNNER_DIR/docker-compose.yml" ]; then
RUNNER_SERVICE="gitea-act-runner-host.service"
if [ -x "$RUNNER_DIR/act_runner" ] && [ -f "$RUNNER_DIR/config.yaml" ]; then
# 若舊的 .runner 配置指向過期 hostname先清除讓 runner 重新註冊
RUNNER_FILE="$RUNNER_DIR/data/.runner"
if [ -f "$RUNNER_FILE" ]; then
@@ -199,18 +200,47 @@ if [ -f "$RUNNER_DIR/docker-compose.yml" ]; then
fi
fi
cd "$RUNNER_DIR"
docker compose up -d 2>&1 | tail -3
# act_runner 預設 shutdown_timeout=0s會在 daemon 重啟時立刻取消
# 正在執行的 CD job。這會造成部署實際完成但 Gitea status 失真。
python3 - "$RUNNER_DIR/config.yaml" <<'PY' || true
import sys
from pathlib import Path
path = Path(sys.argv[1])
lines = path.read_text().splitlines()
if any(line.strip().startswith("shutdown_timeout:") for line in lines):
lines = [
" shutdown_timeout: 1h" if line.strip().startswith("shutdown_timeout:") else line
for line in lines
]
else:
for idx, line in enumerate(lines):
if line.strip().startswith("timeout:") and idx > 0:
lines.insert(idx + 1, " shutdown_timeout: 1h")
break
path.write_text("\n".join(lines) + "\n")
PY
if systemctl list-unit-files "$RUNNER_SERVICE" >/dev/null 2>&1; then
systemctl enable --now "$RUNNER_SERVICE" >/dev/null 2>&1 || true
elif ! pgrep -f "$RUNNER_DIR/act_runner daemon" >/dev/null; then
nohup "$RUNNER_DIR/run-host-runner.sh" >> "$RUNNER_DIR/host-runner.log" 2>&1 &
fi
# 已停用 Docker-wrapped runner避免它搶走 host label job。
docker update --restart=no gitea-runner >/dev/null 2>&1 || true
docker stop gitea-runner >/dev/null 2>&1 || true
sleep 15
# 驗證 runner 已連線 Gitea
if docker logs gitea-runner --tail 5 2>/dev/null | grep -q "SUCCESS\|Connected\|Listening"; then
log "✅ Gitea Act Runner 已連線"
if pgrep -f "$RUNNER_DIR/act_runner daemon" >/dev/null; then
log "✅ Gitea host act_runner 已啟動"
else
log "⚠️ Gitea Act Runner 可能尚未連線,查看: docker logs gitea-runner"
log "⚠️ Gitea host act_runner 可能尚未啟動,查看: $RUNNER_DIR/host-runner.log"
fi
else
log "⚠️ 找不到 act-runner compose 檔案: $RUNNER_DIR/docker-compose.yml"
log "⚠️ 找不到 act-runner binary/config: $RUNNER_DIR"
fi
# ──────────────────────────────────────────────