monitor_git.sh 970 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. # 设置 Git 仓库的 URL
  3. GIT_REPO_URL="http://120.46.190.49:10880/joenyliang/ql_api.git"
  4. GIT_BRANCH="main"
  5. # 设置工作目录
  6. WORK_DIR="/docker"
  7. # 进入工作目录
  8. cd $WORK_DIR
  9. # 配置 Git 凭证助手
  10. git config --global credential.helper store
  11. # 初始化 Git 仓库(如果尚未初始化)
  12. if [ ! -d ".git" ]; then
  13. git init
  14. git remote add origin $GIT_REPO_URL
  15. git fetch
  16. git checkout -t origin/$GIT_BRANCH
  17. fi
  18. # 监测 Git 仓库的更新
  19. while true; do
  20. git fetch origin $GIT_BRANCH
  21. LOCAL=$(git rev-parse HEAD)
  22. REMOTE=$(git rev-parse origin/$GIT_BRANCH)
  23. if [ $LOCAL != $REMOTE ]; then
  24. echo "New updates found. Pulling latest changes..."
  25. git pull origin $GIT_BRANCH
  26. echo "Restarting Django server..."
  27. pkill -f "python manage.py runserver"
  28. python manage.py migrate
  29. python manage.py runserver 0.0.0.0:8084 &
  30. fi
  31. # 每隔60秒检查一次
  32. sleep 60
  33. done