#!/bin/bash set -euo pipefail # Rollback deployment by swapping colors # Usage: ./rollback-deployment.sh # Source common functions SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPT_DIR}/common-lib.sh" validate_deployment_env # Get current state STATE=$(get_deployment_state) echo "🔍 Current deployment state: $STATE" if [ "$STATE" = "none" ]; then echo "❌ No active deployment found to rollback" exit 1 fi if [ "$STATE" != "both" ]; then echo "❌ Rollback requires both colors to be running" echo " Current state: only $STATE is running" echo "" echo " To perform a manual rollback:" echo " 1. Find the previous release in ${RELEASES_PATH}/" echo " 2. Update the symlink: ln -sfn ${CURRENT_LINK_PATH}" echo " 3. Redeploy using: ./deploy-blue-green.sh" exit 1 fi # Both colors running - determine which is newer CURRENT_COLOR=$(get_current_color) ROLLBACK_COLOR=$(switch_color "$CURRENT_COLOR") # Get image tags for both deployments CURRENT_IMAGE=$(get_deployment_image_tag "$CURRENT_COLOR") ROLLBACK_IMAGE=$(get_deployment_image_tag "$ROLLBACK_COLOR") echo "🔄 Rolling back from $CURRENT_COLOR (newer) to $ROLLBACK_COLOR (older)" echo " Current image: ${CURRENT_IMAGE}" echo " Rollback image: ${ROLLBACK_IMAGE}" # Verify the rollback image exists if ! run_on_target "docker images -q 'badbl0cks/pkmntrade-club:${ROLLBACK_IMAGE}' | grep -q ."; then echo "❌ Rollback image not found: badbl0cks/pkmntrade-club:${ROLLBACK_IMAGE}" echo " The image may have been pruned. Cannot perform rollback." exit 1 fi # Verify rollback color is healthy ROLLBACK_PROJECT=$(get_project_name "$ROLLBACK_COLOR") HEALTHY_COUNT=$(count_containers "label=com.docker.compose.project=${ROLLBACK_PROJECT} --filter status=running") if [ "$HEALTHY_COUNT" -eq 0 ]; then echo "❌ No healthy $ROLLBACK_COLOR containers found, cannot perform rollback" exit 1 fi echo "✅ Found $HEALTHY_COUNT healthy $ROLLBACK_COLOR containers" # Store the release path of the current color before we change the symlink CURRENT_RELEASE_PATH=$(readlink -f "${CURRENT_LINK_PATH}") echo "🔄 Performing rollback..." echo "🔎 Finding release for rollback color ($ROLLBACK_COLOR)..." # Find the second newest release directory. This is assumed to be the rollback target. ROLLBACK_RELEASE_PATH=$(ls -dt "${RELEASES_PATH}"/*/ | sed -n '2p' | tr -d '\n') if [ -z "$ROLLBACK_RELEASE_PATH" ]; then echo "❌ Could not find a previous release to rollback to in ${RELEASES_PATH}" exit 1 fi echo " Found rollback release: ${ROLLBACK_RELEASE_PATH}" echo "🔗 Switching 'current' symlink to point to rollback release..." ln -sfn "${ROLLBACK_RELEASE_PATH}" "${CURRENT_LINK_PATH}" # Refresh gatekeepers to switch traffic to the rollback color refresh_gatekeepers wait_with_countdown 10 "⏳ Waiting for traffic to stabilize on $ROLLBACK_COLOR..." # Stop and clean up current color containers, using the correct release path export CLEANUP_RELEASE_PATH="${CURRENT_RELEASE_PATH}" cleanup_color_containers "$CURRENT_COLOR" unset CLEANUP_RELEASE_PATH # Refresh gatekeepers again to remove routes to the old color refresh_gatekeepers echo "✅ Rollback completed!" echo " Active deployment: $ROLLBACK_COLOR" echo "" echo "📌 Note: The next deployment will now deploy as $CURRENT_COLOR"