Files
pulse/deploy/pulse_deploy.sh
T
jaredandClaude Opus 5.5 cb12fec28c
CI / Test (jest) (push) Successful in 47s
CI / Lint (eslint) (push) Successful in 52s
CI / Security (npm audit) (push) Successful in 17s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / Test (jest) (pull_request) Failing after 12s
CI / Lint (eslint) (pull_request) Successful in 14s
CI / Security (npm audit) (pull_request) Successful in 11s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
Deploys: exact sha, replay protection, health check and automatic rollback
#45 phase 2.

- GET /healthz (unauthenticated): running git sha, version, database
  reachability, connected workers, uptime. 503 when the database is down.
- deploy/pulse_deploy.sh, versioned for the first time (it only existed
  as a hand-edited file on CT 122). It takes <sha> <ts> from the signed
  webhook payload and:
  - refuses requests older than 5 minutes, or not newer than the last
    accepted one (replays);
  - refuses commits that aren't on main;
  - checks out exactly that sha (before, it deployed whatever main was);
  - runs npm ci --omit=dev (before, npm install --production) and
    restarts;
  - waits for /healthz to report the sha with a working database, and
    otherwise rolls back to the previous commit.
- tests/deploy-script.test.js runs the script in a sandbox (temporary git
  origin, fake systemctl/npm/curl): exact-sha deploy with .env kept, replay,
  stale request, off-main commit, malformed input, and rollback of an
  unhealthy build.
- CI's deploy job now succeeds only once production reports the tested sha
  as healthy, before tagging. Before, it only checked that the server
  answered.
- README: CI/CD and deploy documentation, including installing the script
  and the hooks.json arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 02:25:35 -05:00

97 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Pulse production deploy (#45 phase 2), run by the webhook listener on CT 122.
#
# Source of truth: deploy/pulse_deploy.sh in the Pulse repo.
# Installed copy: /usr/local/bin/pulse_deploy.sh (install it from the repo; see README "Deploys").
#
# Usage: pulse_deploy.sh <sha> <ts>
# sha full 40-char commit CI tested; must be on origin/main
# ts unix time CI signed the request at
# Both come from the HMAC-signed webhook payload, so they can't be altered. The
# timestamp makes every signature unique: stale or replayed requests are refused.
#
# Deploys exactly <sha>, then waits for /healthz to report that sha with a working
# database. Otherwise it rolls back to the previous commit.
set -euo pipefail
# Overridable only so tests can run the script in a sandbox.
APP_ROOT=${PULSE_DEPLOY_APP_ROOT:-/opt/pulse-server}
BRANCH=main
STATE_DIR=${PULSE_DEPLOY_STATE_DIR:-/var/lib/pulse-deploy}
LOG=${PULSE_DEPLOY_LOG:-/var/log/pulse-deploy.log}
LOCK=${PULSE_DEPLOY_LOCK:-/run/pulse-deploy.lock}
HEALTH_URL=${PULSE_DEPLOY_HEALTH_URL:-http://127.0.0.1:8080/healthz}
MAX_AGE_S=300 # refuse requests signed more than 5 minutes ago
HEALTH_TRIES=${PULSE_DEPLOY_HEALTH_TRIES:-45} # x 2 s = 90 s for the new process to report healthy
log() { echo "$(date '+%F %T') [pulse-deploy] $*" | tee -a "$LOG"; }
fail() { log "REFUSED: $*"; exit 1; }
SHA="${1:-}"
TS="${2:-}"
# One deploy at a time (CI also queues deploys, this covers manual runs).
exec 9>"$LOCK"
flock -w 600 9 || fail "timed out waiting for another deploy"
[[ "$SHA" =~ ^[0-9a-f]{40}$ ]] || fail "missing or invalid sha"
[[ "$TS" =~ ^[0-9]+$ ]] || fail "missing or invalid timestamp"
now=$(date +%s)
age=$(( now - TS ))
(( age >= -60 && age <= MAX_AGE_S )) || fail "request is ${age}s old (limit ${MAX_AGE_S}s)"
mkdir -p "$STATE_DIR"
chmod 700 "$STATE_DIR"
last_ts=$(cat "$STATE_DIR/last_ts" 2>/dev/null || echo 0)
(( TS > last_ts )) || fail "timestamp $TS is not newer than the last accepted request ($last_ts): replay or out of order"
echo "$TS" > "$STATE_DIR/last_ts"
cd "$APP_ROOT"
git fetch --quiet origin "$BRANCH"
git cat-file -e "${SHA}^{commit}" 2>/dev/null || fail "commit $SHA not found after fetch"
git merge-base --is-ancestor "$SHA" "origin/$BRANCH" || fail "commit $SHA is not on $BRANCH"
PREV=$(git rev-parse HEAD)
deploy_to() {
# .env and node_modules are untracked, so a forced checkout leaves .env alone.
# Explicit returns: `set -e` does not apply inside a function used in an `if`.
git checkout --quiet --force --detach "$1" || { log "checkout of $1 failed"; return 1; }
npm ci --omit=dev --no-audit --no-fund >>"$LOG" 2>&1 || { log "npm ci failed for $1"; return 1; }
systemctl restart pulse || { log "restart failed for $1"; return 1; }
}
healthy() {
local want="$1" body
for _ in $(seq 1 "$HEALTH_TRIES"); do
if body=$(curl -fsS --max-time 3 "$HEALTH_URL" 2>/dev/null) \
&& grep -q "\"sha\":\"$want\"" <<<"$body" \
&& grep -q '"db":true' <<<"$body"; then
return 0
fi
sleep "${PULSE_DEPLOY_HEALTH_SLEEP:-2}"
done
return 1
}
if [ "$PREV" = "$SHA" ] && healthy "$SHA"; then
log "already running $SHA and healthy; nothing to do"
exit 0
fi
log "deploying $SHA (previous $PREV)"
if deploy_to "$SHA" && healthy "$SHA"; then
echo "$SHA" > "$STATE_DIR/current"
log "deployed $SHA: healthy"
exit 0
fi
log "FAILED: $SHA did not become healthy; rolling back to $PREV"
if deploy_to "$PREV" && healthy "$PREV"; then
log "rolled back to $PREV: healthy"
else
log "ROLLBACK FAILED: check journalctl -u pulse"
fi
exit 1