gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc6162f00c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmrec6162f00/deploy.sh

11.9 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — gitoria.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.
  3. #
  4. # ./deploy.sh gates → backup → rsync → restart the container → public URL 200
  5. # ./deploy.sh --dry-run gates → rsync -n (shows what WOULD be sent), no backup, 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. # (backup: tars storage/.sessions/.env that exist on the target to Loreana's
  12. # /media/SLOW1TB2/deploy-backups/<app>/, keeps newest 5; --target DIR backs up DIR instead)
  13. #
  14. # THE FIRST DEPLOY is done by the architect on Byrodin (folder, data, nginx vhost, cert,
  15. # DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/, server.*,
  16. # testapp/ and logs are never sent, so live data on Byrodin is never touched.
  17. # No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).
  18. set -euo pipefail
  19. # ---- the app ------------------------------------------------------------------------------
  20. APP=gitoria.worldapi.org
  21. CONTAINER=gitoria.worldapi.org
  22. [email protected]:/CONTAINERS/projects/gitoria.worldapi.org
  23. DEFAULT_URL=https://gitoria.worldapi.org/
  24. GATES=(
  25. "node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page
  26. "node theme/check-theme.mjs ."
  27. "node tests/browser.mjs"
  28. "node tests/security.mjs"
  29. "node tests/private.mjs" # go34: private repos, organizations (158 checks)
  30. "node tests/ident-access-gitoria.mjs" # ident#27: an idt_ access key as the git password
  31. "node tests/apps.mjs" # gitoria#41: apps connected to a repo (URL allow-list, connect flow, add/remove/rotate)
  32. "node tests/pull-numbers.mjs" # gitoria#40: PR numbers, refs/pull/N/head, the changes of syncPulls, wasHeadOf
  33. "node tests/pull-ssh.mjs" # gitoria#40: the same over the sshd door (docker)
  34. "node tests/app-token.mjs" # gitoria#42: an app trades its key for a one-hour token; git read with it, no push
  35. "node tests/events.mjs" # gitoria#43: signed events to apps (HMAC, retries, Merge button, ping, delivery log, two processes, retention)
  36. )
  37. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  38. EXCLUDES=(
  39. /.git/ /.gitignore
  40. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  41. '*.log' '*.pid' node_modules/
  42. )
  43. # ---------------------------------------------------------------------------------------------
  44. DRY=0
  45. SKIP=0
  46. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  47. URL=${DEPLOY_URL:-$DEFAULT_URL}
  48. # how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)
  49. URL_WAIT=${DEPLOY_URL_WAIT:-120}
  50. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  51. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  52. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  53. while [ $# -gt 0 ]; do
  54. case "$1" in
  55. --dry-run) DRY=1 ;;
  56. --skip-tests) SKIP=1 ;;
  57. --target) TARGET=${2:?--target needs a value}; shift ;;
  58. --target=*) TARGET=${1#--target=} ;;
  59. --url) URL=${2:?--url needs a value}; shift ;;
  60. --url=*) URL=${1#--url=} ;;
  61. -h|--help) usage; exit 0 ;;
  62. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  63. esac
  64. shift
  65. done
  66. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  67. cd "$HERE"
  68. step() { printf '\n==> %s\n' "$*"; }
  69. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  70. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  71. # a target `host:dir` is remote, a plain path is local
  72. if [[ "$TARGET" == *:* ]]; then
  73. REMOTE_HOST=${TARGET%%:*}
  74. REMOTE_DIR=${TARGET#*:}
  75. else
  76. REMOTE_HOST=
  77. REMOTE_DIR=$TARGET
  78. fi
  79. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  80. echo " from $HERE"
  81. echo " url $URL"
  82. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  83. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  84. [ -f project.hl ] || die "project.hl is missing here"
  85. command -v rsync >/dev/null || die "rsync is not installed"
  86. # ---- 1. the gates ---------------------------------------------------------------------------
  87. if [ "$SKIP" = 1 ]; then
  88. step "[1/5] GATES SKIPPED"
  89. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  90. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  91. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  92. else
  93. step "[1/5] gates (${#GATES[@]})"
  94. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  95. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  96. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  97. for g in "${GATES[@]}"; do
  98. printf ' $ %s\n' "$g"
  99. out=$(mktemp)
  100. if ! $g > "$out" 2>&1; then
  101. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  102. echo " (full output: $out)"
  103. die "gate failed: $g"
  104. fi
  105. grep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || true
  106. rm -f "$out"
  107. done
  108. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  109. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  110. leaked=""
  111. while read -r pid ppid; do
  112. [ -z "$pid" ] && continue
  113. case "$before" in *" $pid "*) continue ;; esac
  114. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  115. leaked="$leaked $pid"
  116. done < <(chromes)
  117. if [ -n "$leaked" ]; then
  118. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  119. fi
  120. fi
  121. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  122. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  123. BACKUP_DIR="$BACKUP_ROOT/$APP"
  124. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  125. if [ "$DRY" = 1 ]; then
  126. step "[2/5] DRY RUN: backup skipped"
  127. else
  128. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  129. mkdir -p "$BACKUP_DIR"
  130. if [ -n "$REMOTE_HOST" ]; then
  131. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  132. else
  133. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  134. fi
  135. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  136. if [ -z "$present" ]; then
  137. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  138. else
  139. echo " taring: $present"
  140. if [ -n "$REMOTE_HOST" ]; then
  141. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  142. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  143. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  144. run $SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' && { find $present -type d; find $present -type f ! -name '*.mpack' | LC_ALL=C sort; find $present -type f -name '*.mpack' | LC_ALL=C sort; } | tar czf - --warning=no-file-changed --ignore-failed-read --no-recursion -T -; r=\$?; [ \$r -le 1 ]" > "$BACKUP_FILE" \
  145. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  146. else
  147. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  148. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  149. fi
  150. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  151. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  152. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  153. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  154. fi
  155. fi
  156. # ---- 3. rsync the code ----------------------------------------------------------------------
  157. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  158. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  159. if [ -n "$REMOTE_HOST" ]; then
  160. RSYNC+=(-e "$SSH")
  161. else
  162. mkdir -p "$REMOTE_DIR"
  163. fi
  164. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  165. step "[3/5] rsync preview (what would be sent)"
  166. preview=$(mktemp)
  167. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  168. sed 's/^/ /' "$preview"
  169. echo " ($(grep -c . "$preview" || true) lines)"
  170. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  171. die "the preview holds a path that must never be sent (see above)"
  172. fi
  173. rm -f "$preview"
  174. if [ "$DRY" = 1 ]; then
  175. step "[3/5] DRY RUN: nothing sent"
  176. else
  177. step "[3/5] rsync"
  178. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  179. fi
  180. # ---- 4. restart the container, without a gap ------------------------------------------------
  181. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.
  182. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on
  183. # /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the
  184. # standby never comes up, nothing live was touched and the deploy stops here.
  185. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  186. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  187. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  188. if [ "$DRY" = 1 ]; then
  189. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  190. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  191. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  192. else
  193. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  194. if [ -n "$REMOTE_HOST" ]; then
  195. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  196. $SSH "$REMOTE_HOST" "APP_DIR='$REMOTE_DIR' bash -s -- $SWAP_ARGS" < "$SWAP" || die "the hand-over failed — see the log above (the standby keeps serving if the main container did not come back)"
  197. else
  198. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  199. fi
  200. fi
  201. # the helper only restarts the app container: bring the other service (the git-over-SSH sshd) up to date too, without restarting it when nothing changed
  202. if [ "$DRY" != 1 ]; then
  203. # the sshd image COPIES gitoria-shell / gitoria-keys / sshd_config: rebuild it first (cached when nothing changed), then `up -d` recreates the container only if its image changed (GO34c)
  204. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  205. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  206. fi
  207. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  208. if [ "$DRY" = 1 ]; then
  209. step "[5/5] DRY RUN: would check $URL answers 200"
  210. step "dry run done — nothing was sent, nothing restarted"
  211. exit 0
  212. fi
  213. step "[5/5] $URL must answer 200 (up to ${URL_WAIT}s: a new Hybriel compiles the app at its first start, ~20 s, hybriel#148)"
  214. code=000
  215. start=$SECONDS
  216. i=0
  217. while :; do
  218. i=$((i + 1))
  219. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  220. echo " try $i ($((SECONDS - start)) s): $code"
  221. [ "$code" = 200 ] && break
  222. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  223. sleep 2
  224. done
  225. if [ "$code" != 200 ]; then
  226. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  227. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  228. fi
  229. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre
  • 9364fb16gitoria#40: ssh pushes call /__git/pushed before and after (ensurePulls, notifyPush, syncPulls); gate over the sshd imagemre
  • fdb9ed28gitoria#41: repo owners can connect an app (merge w41-apps-connect)mre
  • 2ef23df0gitoria#40: the browser gate reads the title without the #Nmre
  • 9af07ecegitoria#41: connect an app to a repo (apps table, connect flow, manual add, allow-list, settings)mre
  • b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmre
  • 06d622daown image tag gitoria-runtime:debian12 for gitoria and standby (shared tag lost git)mre
  • 20caa33agitoria#37: media queries in the language's @media block (minWidth = '60rem') in list, codebrowser and repomre