# shellcheck shell=bash # crosscode deploy: checks first for every product named, then each product in a fixed order # (Accounts, Events, Website, App), each rolled back on its own if it fails (ROADMAP §8). deploy_usage() { cat <<'EOF' crosscode deploy [events] [website] [app] [options] crosscode deploy --every-church [events] [website] [app] [options] Updates Accounts (always, once the church has it) and the products named, to their latest released tags. Products not named aren't touched. --version @ that release instead of the latest (repeatable, one per product) --main the latest main instead of a tag (staging churches only) --check only the checks; changes nothing --allow-local-changes deploy even if files in a checkout were changed by hand EOF } # Set by cmd_deploy, read by deploy_church. declare -gA DEPLOY_VERSION=() DEPLOY_MAIN=0 DEPLOY_CHECK=0 DEPLOY_EVERY=0 cmd_deploy() { local church="" arg p local -a named=() while [ $# -gt 0 ]; do arg="$1" case "$arg" in --version) [ $# -ge 2 ] || die "--version needs @." [[ "$2" =~ ^([a-z]+)@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version $2: expected @, e.g. events@2.2.0." p="${BASH_REMATCH[1]}" is_known_product "$p" || die "--version $2: unknown product \"$p\"." DEPLOY_VERSION[$p]="$2" shift 2 ;; --main) DEPLOY_MAIN=1; shift ;; --check) DEPLOY_CHECK=1; shift ;; --allow-local-changes) ALLOW_LOCAL_CHANGES=1; shift ;; --every-church) DEPLOY_EVERY=1; shift ;; -h|--help) deploy_usage; return 0 ;; -*) die "Unknown option $arg (crosscode deploy --help)." ;; *) if [ "$DEPLOY_EVERY" = 0 ] && [ -z "$church" ]; then church="$arg" else is_known_product "$arg" || die "Unknown product \"$arg\" (accounts, events, website, app)." named+=("$arg") fi shift ;; esac done [ "$DEPLOY_MAIN" = 0 ] || [ "${#DEPLOY_VERSION[@]}" = 0 ] || die "Use --main or --version, not both." # The church name was taken as the first word; with --every-church after it, it's a product. if [ "$DEPLOY_EVERY" = 1 ] && [ -n "$church" ]; then is_known_product "$church" || die "--every-church doesn't take a church name." named=("$church" "${named[@]}") church="" fi for p in "${!DEPLOY_VERSION[@]}"; do [ "$p" = accounts ] || printf '%s\n' "${named[@]}" | grep -qx "$p" || die "--version ${DEPLOY_VERSION[$p]}: name $p as well (crosscode deploy $p --version …)." done require_root if [ "$DEPLOY_EVERY" = 1 ]; then local -a churches mapfile -t churches < <(list_churches) [ "${#churches[@]}" -gt 0 ] || die "No churches on this server ($CROSSCODE_SITES/*/church.env)." local i rc for i in "${!churches[@]}"; do echo step "Church ${churches[$i]} ($((i + 1)) of ${#churches[@]})" set +e (deploy_church "${churches[$i]}" "${named[@]}") rc=$? set -e if [ "$rc" != 0 ]; then warn "Stopped at ${churches[$i]}. Not reached: ${churches[*]:$((i + 1))}" return 1 fi done return 0 fi [ -n "$church" ] || { deploy_usage; die "Name a church, or use --every-church."; } deploy_church "$church" "${named[@]}" } # deploy_church [products…]: the checks, then the deploy. Must be called as a plain # command (not in `if` or after `||`), so `set -e` keeps working inside it. deploy_church() { local church="$1" shift load_church "$church" local p rp rv local -a wanted=() products=() church_has_product accounts && wanted+=(accounts) for p in "$@"; do if church_has_product "$p"; then wanted+=("$p") elif [ "$DEPLOY_EVERY" = 1 ]; then info "$church has no $p: skipped." else die "$church has no $p (its products: $(cenv PRODUCTS))." fi done mapfile -t products < <(in_product_order "${wanted[@]}") if [ "${#products[@]}" = 0 ]; then info "Nothing to deploy for $church. Name the products to update (its products: $(cenv PRODUCTS))." return 0 fi # ── 1. Checks, for every product, before anything is touched ────────────────────────────── step "Checks for $church: ${products[*]}" local -A DIR=() USR=() PREV=() TARGET=() TVER=() local failed=0 dir user changes major t v major="$(node_major)" if [ "$major" -lt "$CROSSCODE_NODE_MIN" ]; then warn "This server runs Node $(node -v 2>/dev/null || echo '(none)'). Releases need Node $CROSSCODE_NODE_MIN or later (24 LTS recommended)." failed=1 elif [ "$major" -lt "$CROSSCODE_NODE_WARN" ]; then warn "This server runs Node $(node -v), which no longer gets security fixes. Move to Node 24 LTS." fi for p in "${products[@]}"; do is_supported_product "$p" || die "This version of crosscode can't deploy $p yet." # shellcheck source=/dev/null source "$CROSSCODE_HOME/products/$p.sh" dir="$(pvar "$p" DIR)" user="$(pvar "$p" USER)" if [ -z "$dir" ] || [ ! -d "$dir/.git" ]; then warn "$p: $dir isn't a checkout (church.env $(upper "$p")_DIR)."; failed=1; continue; fi if [ -z "$user" ] || ! id "$user" >/dev/null 2>&1; then warn "$p: no app user \"$user\" (church.env $(upper "$p")_USER)."; failed=1; continue; fi DIR[$p]="$dir"; USR[$p]="$user" if ! run_as "$user" "$dir" git fetch --tags --quiet origin; then warn "$p: couldn't fetch from origin."; failed=1; continue fi PREV[$p]="$(checkout_ref "$dir" "$p")" if [ -n "${DEPLOY_VERSION[$p]:-}" ]; then t="${DEPLOY_VERSION[$p]}" git_in "$dir" rev-parse -q --verify "refs/tags/$t" >/dev/null || { warn "$p: there's no tag $t."; failed=1; continue; } elif [ "$DEPLOY_MAIN" = 1 ]; then t="origin/main" else t="$(latest_tag "$dir" "$p")" [ -n "$t" ] || { warn "$p: no $p@x.y.z release tags found."; failed=1; continue; } fi TARGET[$p]="$t" if [ "$t" = origin/main ]; then TVER[$p]=""; else TVER[$p]="$(tag_version "$t")"; fi changes="$(local_changes "$dir")" if [ -n "$changes" ]; then warn "$p: files in $dir were changed or added by hand:" printf '%s\n' "$changes" | sed 's/^/ /' >&2 info "A release would build them in (or refuse to replace them). Move added files (??) out of" info "the folder and put changed ones ( M) back with \"git checkout -- \", after checking" info "nothing in them is needed." if [ "${ALLOW_LOCAL_CHANGES:-}" = 1 ] && [ "$DEPLOY_CHECK" = 0 ]; then info "--allow-local-changes: deploying anyway." else failed=1 fi fi done [ "$failed" = 0 ] || die "Checks failed for $church. Nothing was changed." # Requirements between products (decision 24): from the target release's WHATS-NEW.md heading. local req_ok for p in "${products[@]}"; do [ -n "${TVER[$p]}" ] || { info "$p: deploying main, so no release requirements to check."; continue; } while read -r rp rv; do [ -n "$rp" ] && [ "$rp" != "$p" ] || continue req_ok=0 if [ -n "${TARGET[$rp]:-}" ] && [ "$(product_index "$rp")" -lt "$(product_index "$p")" ]; then # Updated earlier in this same run. if [ -z "${TVER[$rp]}" ] || version_ge "${TVER[$rp]}" "$rv"; then req_ok=1; fi v="${TVER[$rp]:-main}" elif church_has_product "$rp"; then v="$(checkout_ref "$(pvar "$rp" DIR)" "$rp" 2>/dev/null || true)" v="$(tag_version "$v")" version_ge "$v" "$rv" 2>/dev/null && req_ok=1 else v="not installed" fi if [ "$req_ok" = 1 ]; then info "$p ${TVER[$p]} requires $rp $rv: met ($rp ${v})." else warn "$p ${TVER[$p]} requires $rp $rv, and $church has $rp ${v:-unknown}. Update $rp first (or in the same run)." failed=1 fi done < <(git_in "${DIR[$p]}" show "${TARGET[$p]}:apps/$p/WHATS-NEW.md" 2>/dev/null | whatsnew_requires "${TVER[$p]}") done [ "$failed" = 0 ] || die "Requirements not met for $church. Nothing was changed." for p in "${products[@]}"; do if [ "${PREV[$p]}" = "${TARGET[$p]}" ]; then info "$p: already on ${PREV[$p]}." else info "$p: ${PREV[$p]} → ${TARGET[$p]}" fi done if [ "$DEPLOY_CHECK" = 1 ]; then step "Checks passed for $church: ready to deploy." return 0 fi # ── 2. Each product in order; a failure rolls that product back and stops the run ───────── local -A RESULT=() local stopped=0 for p in "${products[@]}"; do if [ "$stopped" = 1 ]; then RESULT[$p]="not reached"; continue; fi if [ "${PREV[$p]}" = "${TARGET[$p]}" ]; then RESULT[$p]="already on ${PREV[$p]}"; continue; fi deploy_product "$p" "${DIR[$p]}" "${USR[$p]}" "${PREV[$p]}" "${TARGET[$p]}" RESULT[$p]="$DEPLOY_RESULT" [ "$DEPLOY_OK" = 1 ] || stopped=1 done echo step "Summary for $church" for p in "${products[@]}"; do info "$(printf '%-9s %s' "$p" "${RESULT[$p]}")"; done [ "$stopped" = 0 ] || return 1 } # deploy_product : sets DEPLOY_OK and # DEPLOY_RESULT. Always returns 0, so `set -e` inside the steps works (see deploy_church). deploy_product() { local p="$1" dir="$2" user="$3" prev="$4" target="$5" stage_file rc stage stage_file="$(mktemp)" echo step "$p: $prev → $target" set +e ( set -e echo backup > "$stage_file" step "$p: backing up the database" "${p}_backup" "$dir" "$user" echo checkout > "$stage_file" step "$p: checking out $target" run_as "$user" "$dir" git checkout -q --detach "$target" step "$p: installing" "${p}_install_deps" "$dir" "$user" step "$p: migrations" "${p}_migrate" "$dir" "$user" step "$p: building" "${p}_build" "$dir" "$user" expected="$("${p}_expected_version" "$dir")" echo reload > "$stage_file" step "$p: reloading PM2" "${p}_reload" "$dir" "$user" step "$p: waiting for /health to report $expected" wait_for_version "$("${p}_health_url")" "$expected" 60 ) rc=$? set -e stage="$(cat "$stage_file")" rm -f "$stage_file" if [ "$rc" = 0 ]; then DEPLOY_OK=1 DEPLOY_RESULT="updated $prev → $(checkout_ref "$dir" "$p")" return 0 fi DEPLOY_OK=0 if [ "$stage" = backup ]; then warn "$p: the backup failed, so nothing was changed." DEPLOY_RESULT="failed at the backup; still on $prev, nothing changed" return 0 fi warn "$p: deploy failed. Going back to $prev (database migrations aren't undone; the backup has the database as it was)." set +e ( set -e run_as "$user" "$dir" git checkout -q --detach "$prev" "${p}_rollback_build" "$dir" "$user" if [ "$stage" = reload ]; then "${p}_reload" "$dir" "$user" wait_for_version "$("${p}_health_url")" "$("${p}_expected_version" "$dir")" 60 fi ) rc=$? set -e if [ "$rc" = 0 ]; then if [ "$stage" = reload ]; then DEPLOY_RESULT="FAILED, rolled back to $prev and running again" else DEPLOY_RESULT="FAILED, rolled back to $prev (the running version was never stopped)" fi else DEPLOY_RESULT="FAILED, and going back to $prev failed too: check it now (crosscode status, pm2 logs)" fi return 0 }