Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ SHARED_PATHS=${SHARED_PATHS:-".env storage"}
readonly RELEASES="$APP_ROOT/releases"
readonly SHARED="$APP_ROOT/shared"
readonly CURRENT="$APP_ROOT/current"
readonly STAMP=$(date -u '+%Y%m%dT%H%M%SZ')
STAMP=$(date -u '+%Y%m%dT%H%M%SZ')
readonly STAMP
readonly NEW_RELEASE="$RELEASES/$STAMP"

log() { printf '\033[1;34m▸\033[0m %s\n' "$*"; }
Expand Down Expand Up @@ -96,7 +97,9 @@ rollback() {
printf '\033[1;31m✗ deploy failed — rolling back\033[0m\n' >&2
if [[ -n $PREVIOUS && -d $PREVIOUS ]]; then
atomic_symlink "$PREVIOUS" "$CURRENT"
[[ -n $RELOAD_CMD ]] && eval "$RELOAD_CMD" || true
if [[ -n $RELOAD_CMD ]]; then
eval "$RELOAD_CMD" || printf ' reload command failed during rollback\n' >&2
fi
printf '\033[1;33m↩ restored %s\033[0m\n' "$(basename "$PREVIOUS")" >&2
printf ' the failed release is left at %s for inspection\n' "$NEW_RELEASE" >&2
else
Expand Down Expand Up @@ -204,13 +207,21 @@ ROLLBACK_ARMED=0
# The currently-linked release is excluded explicitly rather than relying on
# it being newest — a rollback makes an older release the live one.
log "Pruning to the last $KEEP_RELEASES releases"
# find -printf '%f' is GNU-only, so the basename is stripped with sed instead.
# Release directory names are our own UTC timestamps, so they contain no
# newlines or spaces and sort lexicographically in chronological order.
live=$(basename "$(readlink -f "$CURRENT")")
pruned=0
while IFS= read -r old; do
[[ -n $old ]] || continue
[[ $old == "$live" ]] && continue
rm -rf "${RELEASES:?}/$old"
(( pruned++ ))
done < <(ls -1 "$RELEASES" 2>/dev/null | sort -r | tail -n +$(( KEEP_RELEASES + 1 )))
[[ $pruned -gt 0 ]] && log " removed $pruned old release(s)"
pruned=$(( pruned + 1 ))
done < <(find "$RELEASES" -mindepth 1 -maxdepth 1 -type d 2>/dev/null \
| sed 's|.*/||' | sort -r | tail -n +$(( KEEP_RELEASES + 1 )))

if [[ $pruned -gt 0 ]]; then
log " removed $pruned old release(s)"
fi

ok "Deployed $STAMP"
Loading