- Every command takes --json (crosscode.script-report/1 on stdout, the log on stderr) and --config (the .env values by layer, from the agent). - .env files come from the release's env.template.json (lib/envtpl.js): written whole on install; on deploy only new variables are appended, and a new required one with no value stops the deploy at the checks. - install --frp: Hub-given ports, a marked frpc.ini block (backup, frpc verify, reload; [common] admin lines added once), no nginx. A database on another server via DB_ADMIN_URL. Refuses an existing database or role. Everything the run made is removed if it fails. - restart, export (crosscode.church-export/1 ZIP), uninstall (final backup, uploads and .env kept 30 days) and purge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
9.9 KiB
Bash
250 lines
9.9 KiB
Bash
#!/usr/bin/env bats
|
|
# crosscode deploy against a local git "origin", with stub Events hooks that log what they do
|
|
# instead of running npm, Prisma or PM2. Covers the checks, requirements, order, rollback and
|
|
# --every-church. What needs a real server is tested on a VM (EVENTS-2.2-PLAN §6).
|
|
|
|
git_q() { git -c user.name=test -c user.email=test@test -c init.defaultBranch=main "$@"; }
|
|
|
|
setup() {
|
|
TMP="$BATS_TEST_TMPDIR"
|
|
export CROSSCODE_TEST=1 CROSSCODE_NO_UPDATE=1
|
|
export CROSSCODE_SITES="$TMP/sites"
|
|
export CROSSCODE_REPO="$TMP/origin"
|
|
export LOG="$TMP/log"
|
|
: > "$LOG"
|
|
|
|
# The crosscode script, with stub hooks for Events.
|
|
export CROSSCODE_HOME="$TMP/tools"
|
|
mkdir -p "$CROSSCODE_HOME/products"
|
|
cp -R "$BATS_TEST_DIRNAME/../lib" "$BATS_TEST_DIRNAME/../crosscode" "$CROSSCODE_HOME/"
|
|
cat > "$CROSSCODE_HOME/products/events.sh" <<'EOF'
|
|
ref() { git -C "$1" describe --tags --exact-match 2>/dev/null || echo main; }
|
|
events_backup() { echo "backup $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != backup ]; }
|
|
events_install_deps() { echo "install $(ref "$1")" >> "$LOG"; }
|
|
events_migrate() { echo "migrate $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != migrate ]; }
|
|
events_build() { echo "build $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != build ]; }
|
|
events_rollback_build() { echo "rollback_build $(ref "$1")" >> "$LOG"; }
|
|
events_expected_version() { cat "$1/apps/events/VERSION"; }
|
|
events_reload() { echo "reload $(ref "$1")" >> "$LOG"; }
|
|
events_health_url() { echo http://stub/health; }
|
|
wait_for_version() { echo "health $2" >> "$LOG"; [ "${FAIL_HEALTH:-}" != "$2" ]; }
|
|
EOF
|
|
|
|
# origin: events@2.2.0, events@2.2.1, events@2.3.0 (which requires accounts 1.0.0).
|
|
git_q init -q "$TMP/origin"
|
|
mkdir -p "$TMP/origin/apps/events"
|
|
release 2.2.0 "## 2.2.0"
|
|
release 2.2.1 "## 2.2.1"
|
|
release 2.3.0 "## 2.3.0 — requires accounts 1.0.0"
|
|
|
|
add_church hope events@2.2.0
|
|
}
|
|
|
|
release() {
|
|
printf '%s\n' "$1" > "$TMP/origin/apps/events/VERSION"
|
|
printf '# What is new\n\n%s\n\n- Notes.\n' "$2" > "$TMP/origin/apps/events/WHATS-NEW.md"
|
|
git_q -C "$TMP/origin" add -A
|
|
git_q -C "$TMP/origin" commit -q -m "events $1"
|
|
git_q -C "$TMP/origin" tag "events@$1"
|
|
}
|
|
|
|
add_church() {
|
|
local church="$1" at="$2"
|
|
mkdir -p "$CROSSCODE_SITES/$church"
|
|
git_q clone -q "$CROSSCODE_REPO" "$CROSSCODE_SITES/$church/events"
|
|
git_q -C "$CROSSCODE_SITES/$church/events" checkout -q --detach "$at"
|
|
cat > "$CROSSCODE_SITES/$church/church.env" <<EOF
|
|
CHURCH=$church
|
|
PRODUCTS="events"
|
|
ADDRESS=$church.co.za
|
|
EVENTS_DIR=$CROSSCODE_SITES/$church/events
|
|
EVENTS_USER=$(id -un)
|
|
EVENTS_PM2="$church-events-backend $church-events-frontend"
|
|
EVENTS_PORTS="5000 3000"
|
|
EOF
|
|
}
|
|
|
|
on() { git -C "$CROSSCODE_SITES/$1/events" describe --tags --exact-match 2>/dev/null || echo main; }
|
|
cc() { bash "$CROSSCODE_HOME/crosscode" "$@"; }
|
|
|
|
@test "deploy --check refuses a release whose requirement isn't met, and changes nothing" {
|
|
run cc deploy hope events --check
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"events 2.3.0 requires accounts 1.0.0"* ]]
|
|
[[ "$output" == *"Nothing was changed"* ]]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
[ ! -s "$LOG" ]
|
|
}
|
|
|
|
@test "deploy to the latest release is refused while its requirement isn't met" {
|
|
run cc deploy hope events
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"Requirements not met"* ]]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
[ ! -s "$LOG" ]
|
|
}
|
|
|
|
@test "deploy --check with a met release says it's ready" {
|
|
run cc deploy hope events --check --version events@2.2.1
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"events@2.2.0 → events@2.2.1"* ]]
|
|
[[ "$output" == *"ready to deploy"* ]]
|
|
[ ! -s "$LOG" ]
|
|
}
|
|
|
|
@test "deploy runs backup, checkout, install, migrate, build, reload and health, in that order" {
|
|
run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 0 ]
|
|
[ "$(on hope)" = events@2.2.1 ]
|
|
[ "$(cat "$LOG")" = "$(printf '%s\n' 'backup events@2.2.0' 'install events@2.2.1' 'migrate events@2.2.1' 'build events@2.2.1' 'reload events@2.2.1' 'health 2.2.1')" ]
|
|
[[ "$output" == *"updated events@2.2.0 → events@2.2.1"* ]]
|
|
}
|
|
|
|
@test "a church already on the release is left alone" {
|
|
run cc deploy hope events --version events@2.2.0
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"already on events@2.2.0"* ]]
|
|
[ ! -s "$LOG" ]
|
|
}
|
|
|
|
@test "a failed backup changes nothing" {
|
|
FAIL_AT=backup run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
[[ "$output" == *"failed at the backup"* ]]
|
|
run grep -q install "$LOG"
|
|
[ "$status" -ne 0 ]
|
|
}
|
|
|
|
@test "a failed migration goes back to the previous release, without reloading" {
|
|
FAIL_AT=migrate run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
[[ "$output" == *"rolled back to events@2.2.0 (the running version was never stopped)"* ]]
|
|
grep -qx "rollback_build events@2.2.0" "$LOG"
|
|
run grep -q reload "$LOG"
|
|
[ "$status" -ne 0 ]
|
|
}
|
|
|
|
@test "a release that doesn't come up is rolled back, reloaded and checked again" {
|
|
FAIL_HEALTH=2.2.1 run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
[ "$(grep -c reload "$LOG")" -eq 2 ]
|
|
grep -qx "health 2.2.0" "$LOG"
|
|
[[ "$output" == *"rolled back to events@2.2.0 and running again"* ]]
|
|
}
|
|
|
|
@test "files changed by hand stop the deploy" {
|
|
echo "hand edit" >> "$CROSSCODE_SITES/hope/events/apps/events/VERSION"
|
|
run cc deploy hope events --version events@2.2.1 --check
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"changed or added by hand"* ]]
|
|
[[ "$output" == *"apps/events/VERSION"* ]]
|
|
run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[ ! -s "$LOG" ]
|
|
}
|
|
|
|
@test "--allow-local-changes deploys with a file added by hand" {
|
|
echo "notes" > "$CROSSCODE_SITES/hope/events/apps/events/notes.txt"
|
|
run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[ ! -s "$LOG" ]
|
|
run cc deploy hope events --version events@2.2.1 --allow-local-changes
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"deploying anyway"* ]]
|
|
[ "$(on hope)" = events@2.2.1 ]
|
|
# --check never passes with changes, even with the flag.
|
|
run cc deploy hope events --check --allow-local-changes
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "ignored files (.env, ecosystem.config.js) don't count as changes" {
|
|
printf '.env\n/apps/events/ecosystem.config.js\n' > "$CROSSCODE_SITES/hope/events/.git/info/exclude"
|
|
echo "SECRET=1" > "$CROSSCODE_SITES/hope/events/.env"
|
|
echo "module.exports = {}" > "$CROSSCODE_SITES/hope/events/apps/events/ecosystem.config.js"
|
|
run cc deploy hope events --version events@2.2.1
|
|
[ "$status" -eq 0 ]
|
|
[ "$(on hope)" = events@2.2.1 ]
|
|
}
|
|
|
|
@test "a product the church doesn't have is refused; nothing named does nothing" {
|
|
run cc deploy hope website
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"hope has no website"* ]]
|
|
run cc deploy hope
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"Nothing to deploy"* ]]
|
|
}
|
|
|
|
@test "deploy --main checks out the latest main" {
|
|
release 2.4.0-dev "## 2.4.0-dev"
|
|
git_q -C "$TMP/origin" tag -d events@2.4.0-dev >/dev/null
|
|
run cc deploy hope events --main
|
|
[ "$status" -eq 0 ]
|
|
[ "$(on hope)" = main ]
|
|
grep -qx "health 2.4.0-dev" "$LOG"
|
|
}
|
|
|
|
@test "deploy --every-church updates each church, one after the other" {
|
|
add_church grace events@2.2.0
|
|
run cc deploy --every-church events --version events@2.2.1
|
|
[ "$status" -eq 0 ]
|
|
[ "$(on hope)" = events@2.2.1 ]
|
|
[ "$(on grace)" = events@2.2.1 ]
|
|
[ "$(grep -c '^health 2.2.1' "$LOG")" -eq 2 ]
|
|
}
|
|
|
|
@test "deploy --every-church stops at the first church that fails" {
|
|
add_church grace events@2.2.0
|
|
FAIL_AT=build run cc deploy --every-church events --version events@2.2.1
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"Stopped at "* ]]
|
|
[ "$(grep -c '^backup' "$LOG")" -eq 1 ]
|
|
}
|
|
|
|
@test "unknown church" {
|
|
run cc deploy nowhere events
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *'No church "nowhere"'* ]]
|
|
}
|
|
|
|
@test "deploy --json: a rolled-back release is reported as rolled-back, with each product's result" {
|
|
out="$(FAIL_HEALTH=2.2.1 cc deploy hope events --version events@2.2.1 --json 2>/dev/null || true)"
|
|
node -e '
|
|
const r = JSON.parse(process.argv[1]);
|
|
if (r.schema !== "crosscode.script-report/1" || r.status !== "rolled-back") throw new Error(r.status);
|
|
if (r.result.products.events.to !== "events@2.2.1" || r.result.products.events.from !== "events@2.2.0") throw new Error("products");
|
|
if (!r.steps.some(s => s.name.includes("reloading PM2"))) throw new Error("steps");' "$out"
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
}
|
|
|
|
@test "a release with a new required setting and no value for it stops at the checks" {
|
|
echo 'events_env_vars() { printf "%s\n" --var domain=events.hope.co.za --var apiPort=5000; }' >> "$CROSSCODE_HOME/products/events.sh"
|
|
mkdir -p "$CROSSCODE_SITES/hope/events/apps/events/backend"
|
|
printf 'DATABASE_URL=x\n' > "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
|
|
# As in the real repo, .env files are ignored by git.
|
|
echo .env >> "$CROSSCODE_SITES/hope/events/.git/info/exclude"
|
|
cat > "$TMP/origin/apps/events/env.template.json" <<'TPL'
|
|
{ "schema": "crosscode.env-template/1", "product": "events", "files": { "backend/.env": [
|
|
{ "name": "DATABASE_URL", "layer": "generated", "required": true },
|
|
{ "name": "NEW_SERVER_SETTING", "layer": "server", "required": true },
|
|
{ "name": "NEW_DEFAULT", "layer": "defaults", "default": "on" } ] } }
|
|
TPL
|
|
release 2.2.2 "## 2.2.2"
|
|
run cc deploy hope events --version events@2.2.2
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"NEW_SERVER_SETTING (server, backend/.env)"* ]]
|
|
[[ "$output" == *"Nothing was changed"* ]]
|
|
[ "$(on hope)" = events@2.2.0 ]
|
|
|
|
echo '{ "server": { "NEW_SERVER_SETTING": "yes" } }' > "$TMP/config.json"
|
|
run cc deploy hope events --version events@2.2.2 --config "$TMP/config.json"
|
|
[ "$status" -eq 0 ]
|
|
[ "$(on hope)" = events@2.2.2 ]
|
|
grep -qx 'DATABASE_URL=x' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
|
|
grep -qx 'NEW_SERVER_SETTING=yes' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
|
|
grep -qx 'NEW_DEFAULT=on' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
|
|
}
|