Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

deploy.sh 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. #
  3. # deploy.sh — run this ON THE SERVER, from inside the cloned repo
  4. # (the app lives at /opt/bizgaze-support).
  5. #
  6. # What it does:
  7. # 1. Snapshots the current working tree (code + .env + certs, excluding .git)
  8. # into a rotating backup, keeping the newest $KEEP_BACKUPS.
  9. # 2. Pulls the latest commit of $GIT_BRANCH (git reset --hard, so the box
  10. # always matches origin exactly).
  11. # 3. Rebuilds the image and recreates the container. The named data volume
  12. # (data.db) persists; the untracked .env / server/cert.pem / server/key.pem
  13. # are gitignored and therefore preserved across every pull.
  14. # 4. Verifies the app is serving.
  15. #
  16. # Usage (on the server):
  17. # cd /opt/bizgaze-support
  18. # ./deploy.sh # pull latest + rebuild
  19. # ./deploy.sh --no-pull # rebuild current checkout (no git pull)
  20. # ./deploy.sh --rollback # restore newest backup snapshot + rebuild
  21. #
  22. # Fire it from your laptop without logging in:
  23. # plink -ssh -P 61 -pw '<pw>' root@118.95.33.89 \
  24. # "cd /opt/bizgaze-support && ./deploy.sh"
  25. set -euo pipefail
  26. GIT_BRANCH="${GIT_BRANCH:-master}"
  27. KEEP_BACKUPS="${KEEP_BACKUPS:-3}"
  28. CONTAINER="${CONTAINER:-bizgaze-support}"
  29. cd "$(dirname "$(readlink -f "$0")")"
  30. APP_DIR="$(pwd)"
  31. BK_DIR="${APP_DIR}.backups"
  32. log() { printf '\033[1;36m==>\033[0m %s\n' "$*"; }
  33. ok() { printf '\033[1;32m ✓\033[0m %s\n' "$*"; }
  34. warn(){ printf '\033[1;33m ! %s\033[0m\n' "$*"; }
  35. die() { printf '\033[1;31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
  36. [ -d .git ] || die "Not a git checkout. Run the one-time bootstrap first (see DEPLOY.md)."
  37. command -v docker >/dev/null || die "docker not found on this host."
  38. # --- args ---
  39. DO_PULL=1; ROLLBACK=0
  40. for a in "$@"; do case "$a" in
  41. --no-pull) DO_PULL=0 ;;
  42. --rollback) ROLLBACK=1 ;;
  43. -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
  44. *) die "Unknown argument: $a" ;;
  45. esac; done
  46. snapshot() {
  47. mkdir -p "$BK_DIR"
  48. local ts rev
  49. ts="$(date +%Y%m%d-%H%M%S)"
  50. rev="$(git rev-parse --short HEAD 2>/dev/null || echo nogit)"
  51. log "Snapshot -> $BK_DIR/${ts}-${rev}.tgz"
  52. tar --exclude=./.git --exclude=./node_modules \
  53. --exclude=./server/node_modules -czf "$BK_DIR/${ts}-${rev}.tgz" -C "$APP_DIR" .
  54. # rotate: keep newest $KEEP_BACKUPS
  55. local old
  56. old="$(ls -1t "$BK_DIR"/*.tgz 2>/dev/null | tail -n +$((KEEP_BACKUPS + 1)) || true)"
  57. if [ -n "$old" ]; then
  58. echo "Pruning old backups:"; echo "$old" | sed 's/^/ rm /'
  59. echo "$old" | xargs -r rm -f
  60. fi
  61. }
  62. rebuild() {
  63. [ -f .env ] || warn "No .env present — TURN/SSO secrets will be unset."
  64. log "Rebuilding and recreating the container …"
  65. docker compose config >/dev/null || die "docker-compose.yml is invalid."
  66. docker compose up -d --build
  67. docker compose ps
  68. }
  69. verify() {
  70. log "Verifying /api/ice …"
  71. if docker exec "$CONTAINER" wget -qO- http://localhost:8090/api/ice 2>/dev/null | grep -q iceServers; then
  72. ok "App is serving."
  73. else
  74. warn "Could not confirm /api/ice — check: docker logs $CONTAINER"
  75. fi
  76. }
  77. # --- rollback mode ---
  78. if [ "$ROLLBACK" -eq 1 ]; then
  79. latest="$(ls -1t "$BK_DIR"/*.tgz 2>/dev/null | head -n1 || true)"
  80. [ -n "$latest" ] || die "No backups found in $BK_DIR"
  81. log "Rolling back from: $latest"
  82. snapshot # snapshot the (broken) current state first
  83. tar -xzf "$latest" -C "$APP_DIR"
  84. rebuild; verify
  85. ok "Rolled back to $latest"
  86. exit 0
  87. fi
  88. # --- normal deploy ---
  89. snapshot
  90. if [ "$DO_PULL" -eq 1 ]; then
  91. log "Fetching origin/$GIT_BRANCH …"
  92. git fetch origin "$GIT_BRANCH"
  93. git reset --hard "origin/$GIT_BRANCH" # untracked .env / *.pem are preserved
  94. ok "Now at $(git rev-parse --short HEAD): $(git log -1 --pretty=%s)"
  95. fi
  96. rebuild
  97. verify
  98. ok "Deploy complete. Backups kept: $KEEP_BACKUPS (in $BK_DIR)."
  99. echo " Rollback with: ./deploy.sh --rollback"