deploy-remote.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. remote="${MAILHUB_DEPLOY_REMOTE:-}"
  4. remote_dir="${MAILHUB_DEPLOY_DIR:-}"
  5. branch="${MAILHUB_DEPLOY_BRANCH:-$(git branch --show-current)}"
  6. git_url="${MAILHUB_DEPLOY_GIT_URL:-$(git remote get-url origin)}"
  7. stash_remote="${MAILHUB_DEPLOY_STASH_REMOTE:-0}"
  8. if [[ -z "${remote}" || -z "${remote_dir}" ]]; then
  9. echo "Set MAILHUB_DEPLOY_REMOTE and MAILHUB_DEPLOY_DIR before deploying." >&2
  10. echo "Example: MAILHUB_DEPLOY_REMOTE=deploy@example.com MAILHUB_DEPLOY_DIR=/opt/mailhub npm run deploy:remote" >&2
  11. exit 1
  12. fi
  13. if [[ -z "${branch}" ]]; then
  14. echo "Unable to detect current git branch." >&2
  15. exit 1
  16. fi
  17. git fetch origin "${branch}"
  18. local_head="$(git rev-parse HEAD)"
  19. remote_head="$(git rev-parse "origin/${branch}")"
  20. if [[ "${local_head}" != "${remote_head}" ]]; then
  21. echo "Local HEAD is not pushed to origin/${branch}." >&2
  22. echo "Commit and push first, then run this deploy script." >&2
  23. exit 1
  24. fi
  25. ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=4 "${remote}" \
  26. 'bash -s' -- "${remote_dir}" "${branch}" "${git_url}" "${stash_remote}" <<'REMOTE'
  27. set -euo pipefail
  28. remote_dir="$1"
  29. branch="$2"
  30. git_url="$3"
  31. stash_remote="$4"
  32. cd "${remote_dir}"
  33. wait_for_compose_health() {
  34. local timeout="${MAILHUB_DEPLOY_HEALTH_TIMEOUT:-180}"
  35. local deadline=$((SECONDS + timeout))
  36. local service container_id snapshot state health all_ready
  37. while (( SECONDS < deadline )); do
  38. all_ready=1
  39. for service in "$@"; do
  40. container_id="$(docker compose ps --all --quiet "${service}" 2>/dev/null | tail -n 1 || true)"
  41. if [[ -z "${container_id}" ]]; then
  42. all_ready=0
  43. continue
  44. fi
  45. snapshot="$(docker inspect \
  46. --format '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{else}}missing{{end}}' \
  47. "${container_id}" 2>/dev/null || true)"
  48. state="${snapshot%% *}"
  49. health="${snapshot#* }"
  50. if [[ "${state}" == "exited" || "${state}" == "dead" ]]; then
  51. docker compose logs --tail=100 "${service}" >&2 || true
  52. return 1
  53. fi
  54. [[ "${state}" == "running" && "${health}" == "healthy" ]] || all_ready=0
  55. done
  56. [[ "${all_ready}" == "1" ]] && return 0
  57. sleep 2
  58. done
  59. docker compose ps >&2 || true
  60. docker compose logs --tail=100 "$@" >&2 || true
  61. return 1
  62. }
  63. if ! git remote get-url origin >/dev/null 2>&1; then
  64. git remote add origin "${git_url}"
  65. fi
  66. if [[ -n "$(git status --porcelain)" ]]; then
  67. if [[ "${stash_remote}" == "1" ]]; then
  68. git stash push -u -m "pre-deploy-$(date -u +%Y%m%d-%H%M%S)"
  69. else
  70. echo "Remote working tree is dirty. Set MAILHUB_DEPLOY_STASH_REMOTE=1 to stash it before pulling." >&2
  71. git status --short >&2
  72. exit 1
  73. fi
  74. fi
  75. previous_revision="$(git rev-parse HEAD)"
  76. on_deploy_exit() {
  77. local status=$?
  78. trap - EXIT
  79. if [[ "${status}" != "0" ]]; then
  80. echo "Deployment failed. Previous revision was ${previous_revision}; inspect the running containers before rollback." >&2
  81. docker compose ps >&2 || true
  82. fi
  83. exit "${status}"
  84. }
  85. trap on_deploy_exit EXIT
  86. git fetch origin "${branch}"
  87. git checkout "${branch}"
  88. git pull --ff-only origin "${branch}"
  89. docker compose up -d --build
  90. wait_for_compose_health app postfix
  91. MAILHUB_CERT_RESTART=1 ./scripts/sync-tls-certificate.sh
  92. wait_for_compose_health app postfix
  93. docker compose ps
  94. trap - EXIT
  95. REMOTE