deploy-remote.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. remote="${MAILHUB_DEPLOY_REMOTE:-root@192.227.215.183}"
  4. remote_dir="${MAILHUB_DEPLOY_DIR:-/www/wwwroot/mail.ss5.xyz}"
  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 "${branch}" ]]; then
  9. echo "Unable to detect current git branch." >&2
  10. exit 1
  11. fi
  12. git fetch origin "${branch}"
  13. local_head="$(git rev-parse HEAD)"
  14. remote_head="$(git rev-parse "origin/${branch}")"
  15. if [[ "${local_head}" != "${remote_head}" ]]; then
  16. echo "Local HEAD is not pushed to origin/${branch}." >&2
  17. echo "Commit and push first, then run this deploy script." >&2
  18. exit 1
  19. fi
  20. ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=4 "${remote}" \
  21. 'bash -s' -- "${remote_dir}" "${branch}" "${git_url}" "${stash_remote}" <<'REMOTE'
  22. set -euo pipefail
  23. remote_dir="$1"
  24. branch="$2"
  25. git_url="$3"
  26. stash_remote="$4"
  27. cd "${remote_dir}"
  28. if ! git remote get-url origin >/dev/null 2>&1; then
  29. git remote add origin "${git_url}"
  30. fi
  31. if [[ -n "$(git status --porcelain)" ]]; then
  32. if [[ "${stash_remote}" == "1" ]]; then
  33. git stash push -u -m "pre-deploy-$(date -u +%Y%m%d-%H%M%S)"
  34. else
  35. echo "Remote working tree is dirty. Set MAILHUB_DEPLOY_STASH_REMOTE=1 to stash it before pulling." >&2
  36. git status --short >&2
  37. exit 1
  38. fi
  39. fi
  40. git fetch origin "${branch}"
  41. git checkout "${branch}"
  42. git pull --ff-only origin "${branch}"
  43. docker compose up -d --build
  44. docker compose ps
  45. REMOTE