run_update_and_commit.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. push_remote="${GIT_PUSH_REMOTE:-origin}"
  44. if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
  45. push_remote=""
  46. while IFS= read -r r; do
  47. push_remote="$r"
  48. break
  49. done < <(git -C "$APP_DIR" remote)
  50. fi
  51. current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  52. if [[ -z "$push_remote" ]]; then
  53. echo "[vmess-domain-rotator] no remote found, skip git push"
  54. elif [[ -z "$current_branch" ]]; then
  55. echo "[vmess-domain-rotator] detached HEAD, skip git push"
  56. else
  57. if git -C "$APP_DIR" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
  58. if git -C "$APP_DIR" push; then
  59. echo "[vmess-domain-rotator] pushed to ${push_remote}/${current_branch}"
  60. else
  61. echo "[vmess-domain-rotator] git push failed"
  62. fi
  63. else
  64. if git -C "$APP_DIR" push -u "$push_remote" "$current_branch"; then
  65. echo "[vmess-domain-rotator] pushed to ${push_remote}/${current_branch} (set upstream)"
  66. else
  67. echo "[vmess-domain-rotator] git push failed"
  68. fi
  69. fi
  70. fi
  71. echo "[vmess-domain-rotator] committed domain change: ${before} -> ${after}"