gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4a2d71254a2d7125initial commitmre4a2d7125/deploy.sh

6.7 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — gitoria.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.
  3. #
  4. # ./deploy.sh gates → rsync → restart the container → public URL 200
  5. # ./deploy.sh --dry-run gates → rsync -n (shows what WOULD be sent), no restart, no URL check
  6. # ./deploy.sh --skip-tests skips the gates (LOUD warning) — only when you know why
  7. # ./deploy.sh --target DIR|HOST:DIR another destination (default below); a local DIR is
  8. # how the script is tested without touching Byrodin
  9. # ./deploy.sh --url URL the URL that must answer 200 after the restart
  10. # (env: DEPLOY_TARGET, DEPLOY_URL, DEPLOY_SSH override the same defaults)
  11. #
  12. # THE FIRST DEPLOY is done by the architect on Byrodin (folder, data, nginx vhost, cert,
  13. # DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/, server.*,
  14. # testapp/ and logs are never sent, so live data on Byrodin is never touched.
  15. # No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).
  16. set -euo pipefail
  17. # ---- the app ------------------------------------------------------------------------------
  18. APP=gitoria.worldapi.org
  19. CONTAINER=gitoria.worldapi.org
  20. [email protected]:/CONTAINERS/projects/gitoria.worldapi.org
  21. DEFAULT_URL=https://gitoria.worldapi.org/
  22. GATES=(
  23. "node tests/browser.mjs"
  24. )
  25. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  26. EXCLUDES=(
  27. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  28. '*.log' '*.pid' node_modules/
  29. )
  30. # ---------------------------------------------------------------------------------------------
  31. DRY=0
  32. SKIP=0
  33. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  34. URL=${DEPLOY_URL:-$DEFAULT_URL}
  35. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  36. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  37. usage() { sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'; }
  38. while [ $# -gt 0 ]; do
  39. case "$1" in
  40. --dry-run) DRY=1 ;;
  41. --skip-tests) SKIP=1 ;;
  42. --target) TARGET=${2:?--target needs a value}; shift ;;
  43. --target=*) TARGET=${1#--target=} ;;
  44. --url) URL=${2:?--url needs a value}; shift ;;
  45. --url=*) URL=${1#--url=} ;;
  46. -h|--help) usage; exit 0 ;;
  47. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  48. esac
  49. shift
  50. done
  51. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  52. cd "$HERE"
  53. step() { printf '\n==> %s\n' "$*"; }
  54. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  55. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  56. # a target `host:dir` is remote, a plain path is local
  57. if [[ "$TARGET" == *:* ]]; then
  58. REMOTE_HOST=${TARGET%%:*}
  59. REMOTE_DIR=${TARGET#*:}
  60. else
  61. REMOTE_HOST=
  62. REMOTE_DIR=$TARGET
  63. fi
  64. step "[0/4] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  65. echo " from $HERE"
  66. echo " url $URL"
  67. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  68. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  69. [ -f project.hl ] || die "project.hl is missing here"
  70. command -v rsync >/dev/null || die "rsync is not installed"
  71. # ---- 1. the gates ---------------------------------------------------------------------------
  72. if [ "$SKIP" = 1 ]; then
  73. step "[1/4] GATES SKIPPED"
  74. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  75. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  76. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  77. else
  78. step "[1/4] gates (${#GATES[@]})"
  79. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  80. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  81. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  82. for g in "${GATES[@]}"; do
  83. printf ' $ %s\n' "$g"
  84. out=$(mktemp)
  85. if ! $g > "$out" 2>&1; then
  86. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  87. echo " (full output: $out)"
  88. die "gate failed: $g"
  89. fi
  90. grep -E 'passed,' "$out" | tail -1 | sed 's/^/ /'
  91. rm -f "$out"
  92. done
  93. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  94. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  95. leaked=""
  96. while read -r pid ppid; do
  97. [ -z "$pid" ] && continue
  98. case "$before" in *" $pid "*) continue ;; esac
  99. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  100. leaked="$leaked $pid"
  101. done < <(chromes)
  102. if [ -n "$leaked" ]; then
  103. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  104. fi
  105. fi
  106. # ---- 2. rsync the code ----------------------------------------------------------------------
  107. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  108. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  109. if [ -n "$REMOTE_HOST" ]; then
  110. RSYNC+=(-e "$SSH")
  111. else
  112. mkdir -p "$REMOTE_DIR"
  113. fi
  114. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  115. step "[2/4] rsync preview (what would be sent)"
  116. preview=$(mktemp)
  117. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  118. sed 's/^/ /' "$preview"
  119. echo " ($(grep -c . "$preview" || true) lines)"
  120. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  121. die "the preview holds a path that must never be sent (see above)"
  122. fi
  123. rm -f "$preview"
  124. if [ "$DRY" = 1 ]; then
  125. step "[2/4] DRY RUN: nothing sent"
  126. else
  127. step "[2/4] rsync"
  128. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  129. fi
  130. # ---- 3. restart the container ---------------------------------------------------------------
  131. RESTART="cd '$REMOTE_DIR' && docker compose up -d --build && docker compose restart && docker compose ps"
  132. if [ "$DRY" = 1 ]; then
  133. step "[3/4] DRY RUN: would restart $CONTAINER"
  134. if [ -n "$REMOTE_HOST" ]; then echo " would run: $SSH $REMOTE_HOST \"$RESTART\""; else echo " would run: bash -c \"$RESTART\""; fi
  135. else
  136. step "[3/4] restart $CONTAINER"
  137. if [ -n "$REMOTE_HOST" ]; then
  138. run $SSH "$REMOTE_HOST" "$RESTART"
  139. else
  140. run bash -c "$RESTART"
  141. fi
  142. fi
  143. # ---- 4. the URL answers 200 -----------------------------------------------------------------
  144. if [ "$DRY" = 1 ]; then
  145. step "[4/4] DRY RUN: would check $URL answers 200"
  146. step "dry run done — nothing was sent, nothing restarted"
  147. exit 0
  148. fi
  149. step "[4/4] $URL must answer 200"
  150. code=000
  151. for i in $(seq 1 20); do
  152. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  153. echo " try $i: $code"
  154. [ "$code" = 200 ] && break
  155. sleep 1
  156. done
  157. if [ "$code" != 200 ]; then
  158. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  159. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  160. fi
  161. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 4a2d7125initial commitmre