run_update_and_commit.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  4. CONFIG_PATH="${1:-${APP_DIR}/config.json}"
  5. DOMAIN_FILE="${APP_DIR}/runtime/current_domain.txt"
  6. before=""
  7. if [[ -f "$DOMAIN_FILE" ]]; then
  8. before="$(tr -d '\r\n' < "$DOMAIN_FILE")"
  9. fi
  10. /usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH"
  11. after=""
  12. if [[ -f "$DOMAIN_FILE" ]]; then
  13. after="$(tr -d '\r\n' < "$DOMAIN_FILE")"
  14. fi
  15. if [[ -z "$after" ]]; then
  16. echo "[vmess-domain-rotator] empty selected domain, skip git commit"
  17. exit 0
  18. fi
  19. if [[ "$before" == "$after" ]]; then
  20. echo "[vmess-domain-rotator] domain unchanged (${after}), skip git commit"
  21. exit 0
  22. fi
  23. if ! command -v git >/dev/null 2>&1; then
  24. echo "[vmess-domain-rotator] git not found, skip git commit"
  25. exit 0
  26. fi
  27. if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  28. echo "[vmess-domain-rotator] not a git repo, skip git commit"
  29. exit 0
  30. fi
  31. git -C "$APP_DIR" add runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
  32. if git -C "$APP_DIR" diff --cached --quiet; then
  33. echo "[vmess-domain-rotator] no staged changes, skip git commit"
  34. exit 0
  35. fi
  36. commit_name="${GIT_COMMIT_NAME:-vmess-domain-rotator}"
  37. commit_email="${GIT_COMMIT_EMAIL:-vmess-domain-rotator@localhost}"
  38. ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
  39. git -C "$APP_DIR" \
  40. -c user.name="$commit_name" \
  41. -c user.email="$commit_email" \
  42. commit -m "chore: rotate preferred domain to ${after} (${ts})"
  43. echo "[vmess-domain-rotator] committed domain change: ${before} -> ${after}"