Skip to content

Commit 1ba90fe

Browse files
authored
fix(deploy): handle initial deployment detection and improve active container guard (#61)
1 parent 9c3cca5 commit 1ba90fe

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

scripts/deploy-bluegreen.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ fi
459459

460460
_ft_log "msg='slot resolved' active=$ACTIVE active_port=$ACTIVE_PORT inactive=$INACTIVE inactive_port=$INACTIVE_PORT"
461461

462+
# ---------------------------------------------------------------------------
463+
# INITIAL DEPLOYMENT DETECTION -- no containers exist yet
464+
# ---------------------------------------------------------------------------
465+
if ! docker ps -a --format '{{.Names}}' | grep -Eq '^api-(blue|green)$'; then
466+
_ft_log "msg='initial deployment detected — no existing containers'"
467+
INITIAL_DEPLOY=true
468+
fi
469+
462470
# ---------------------------------------------------------------------------
463471
# IDEMPOTENCY GUARD -- skip deploy if this exact SHA is already the active container
464472
# ---------------------------------------------------------------------------
@@ -790,18 +798,22 @@ unset _STABLE
790798
# ---------------------------------------------------------------------------
791799
_ft_state "CLEANUP" "msg='validating active container exists before cleanup' name=$ACTIVE_NAME"
792800

793-
# ACTIVE CONTAINER GUARD -- prevent edge-case race corruption
801+
# ACTIVE CONTAINER GUARD -- handle missing container gracefully (e.g., first deploy or crash)
794802
if ! docker ps --format '{{.Names}}' | grep -q "^$ACTIVE_NAME$"; then
795-
_ft_log "level=ERROR msg='active container missing before cleanup -- cannot safely proceed (possible race condition or crash)' name=$ACTIVE_NAME"
796-
_ft_snapshot
797-
_ft_exit 3 "DEPLOY_FAILED_FATAL" "reason=active_container_missing_before_cleanup"
803+
_ft_log "msg='active container missing — treating as first deploy, skipping cleanup' name=$ACTIVE_NAME"
804+
SKIP_CLEANUP=true
805+
else
806+
_ft_log "msg='active container guard passed' name=$ACTIVE_NAME"
798807
fi
799-
_ft_log "msg='active container guard passed' name=$ACTIVE_NAME"
800808

801809
# Graceful shutdown: allow in-flight requests to drain before forcing removal.
802-
docker stop --time 10 "$ACTIVE_NAME" 2>/dev/null || true
803-
docker rm "$ACTIVE_NAME" || true
804-
_ft_log "msg='previous container removed (graceful)' name=$ACTIVE_NAME"
810+
if [ "${SKIP_CLEANUP:-false}" != "true" ]; then
811+
docker stop --time 10 "$ACTIVE_NAME" 2>/dev/null || true
812+
docker rm "$ACTIVE_NAME" || true
813+
_ft_log "msg='previous container removed (graceful)' name=$ACTIVE_NAME"
814+
else
815+
_ft_log "msg='cleanup skipped (first deploy scenario or container already removed)'"
816+
fi
805817

806818
_ft_state "SUCCESS" "msg='deployment complete' container=$INACTIVE_NAME sha=$IMAGE_SHA slot=$INACTIVE port=$INACTIVE_PORT"
807819

0 commit comments

Comments
 (0)