deploy-kvm4.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. # Deploy main to briven.tech on kvm4. Runs from the operator's local
  3. # machine — wraps git push + ssh pull + docker compose build/up so the
  4. # rollout becomes one command:
  5. #
  6. # ./scripts/deploy-kvm4.sh # api+web+docs
  7. # ./scripts/deploy-kvm4.sh api # just api
  8. # ./scripts/deploy-kvm4.sh api web docs runtime # explicit set
  9. # ./scripts/deploy-kvm4.sh --no-push api # local build only
  10. # # (use when konnos is down)
  11. #
  12. # What it does:
  13. # 1. push current HEAD to konnos (skipped with --no-push)
  14. # 2. ssh into kvm4, git stash any drift, git pull origin main
  15. # 3. docker compose build the requested services with
  16. # BRIVEN_BUILD_SHA + BRIVEN_BUILD_AT injected as build-args so
  17. # /info reflects the running commit
  18. # 4. up -d --force-recreate the same services
  19. # 5. wait + curl /info on api so the operator sees the new sha live
  20. #
  21. # Doesn't redeploy database / redis / minio — they survive across
  22. # code changes. Pass them explicitly if you need to.
  23. set -euo pipefail
  24. HOST="root@187.124.209.17"
  25. REMOTE_REPO="/etc/dokploy/compose/briven/code"
  26. PROJECT_NAME="briven"
  27. COMPOSE_FILE="infra/dokploy/compose.dokploy.yml"
  28. PUSH=1
  29. SERVICES=()
  30. for arg in "$@"; do
  31. case "$arg" in
  32. --no-push) PUSH=0 ;;
  33. -h|--help)
  34. sed -n '2,22p' "$0" | sed 's/^# \{0,1\}//'
  35. exit 0
  36. ;;
  37. *) SERVICES+=("$arg") ;;
  38. esac
  39. done
  40. if [[ ${#SERVICES[@]} -eq 0 ]]; then
  41. SERVICES=(api web docs)
  42. fi
  43. repo_root="$(cd "$(dirname "$0")/.." && pwd)"
  44. cd "$repo_root"
  45. SHA=$(git rev-parse --short HEAD)
  46. NOW=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
  47. DIRTY=""
  48. if ! git diff --quiet HEAD; then
  49. DIRTY=" (dirty tree — uncommitted local changes will NOT deploy)"
  50. fi
  51. echo "→ deploying $SHA at $NOW to $HOST$DIRTY"
  52. echo "→ services: ${SERVICES[*]}"
  53. # ─── 1. push to konnos ─────────────────────────────────────────────────
  54. if [[ "$PUSH" -eq 1 ]]; then
  55. echo "→ pushing to konnos"
  56. if ! git push origin main; then
  57. echo "✖ push to konnos failed. pass --no-push to deploy from rsync only."
  58. exit 1
  59. fi
  60. fi
  61. # ─── 2-5. pull + build + up + verify on kvm4 ───────────────────────────
  62. ssh "$HOST" "
  63. set -euo pipefail
  64. cd $REMOTE_REPO
  65. echo ' → stash any drift'
  66. git stash --include-untracked >/dev/null 2>&1 || true
  67. echo ' → git pull origin main'
  68. git pull origin main | tail -3
  69. echo ' → docker compose build (sha=$SHA)'
  70. docker compose --project-name $PROJECT_NAME --env-file .env -f $COMPOSE_FILE build \
  71. --build-arg BRIVEN_BUILD_SHA=$SHA --build-arg BRIVEN_BUILD_AT='$NOW' \
  72. ${SERVICES[*]} 2>&1 | tail -3
  73. echo ' → docker compose up -d --force-recreate'
  74. docker compose --project-name $PROJECT_NAME --env-file .env -f $COMPOSE_FILE up -d \
  75. --force-recreate --no-deps ${SERVICES[*]} 2>&1 | tail -3
  76. if printf '%s\n' ${SERVICES[*]} | grep -q '^api$'; then
  77. sleep 5
  78. echo
  79. echo ' → verify /info reflects the new build'
  80. curl -sS https://api.briven.tech/info
  81. echo
  82. fi
  83. "
  84. echo "✔ deploy complete"