diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 6cbddf7..3a7da63 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -174,6 +174,20 @@ jobs: --auto-scaling-group-name "$ACTIVE_ASG" \ --min-size 0 --desired-capacity 0 --max-size 0 + echo "đŸ’Ĩ Forcing termination of existing instances in $ACTIVE_ASG" + + INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$ACTIVE_ASG" \ + --query "AutoScalingGroups[0].Instances[*].InstanceId" \ + --output text) + + for INSTANCE_ID in $INSTANCE_IDS; do + echo "đŸ”Ĩ Terminating instance: $INSTANCE_ID" + aws autoscaling terminate-instance-in-auto-scaling-group \ + --instance-id "$INSTANCE_ID" \ + --should-decrement-desired-capacity + done + echo "âŗ Waiting for $ACTIVE_ASG to fully scale down..." while true; do INSTANCE_COUNT=$(aws autoscaling describe-auto-scaling-groups \ @@ -200,7 +214,8 @@ jobs: echo "ACTIVE_ASG_COLOR=$ACTIVE_ASG_COLOR" >> $GITHUB_ENV echo "DELETE_ASG_COLOR=$DELETE_ASG_COLOR" >> $GITHUB_ENV - + echo "DELETE_ASG=$DELETE_ASG" >> $GITHUB_ENV + - name: Wait for ALB Health Check → Switch Listener → Shutdown old ASG run: | DEPLOY_TG_NAME="weplate-tg-${ACTIVE_ASG_COLOR}" @@ -272,7 +287,39 @@ jobs: aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name "$DELETE_ASG" \ --min-size 0 --desired-capacity 0 --max-size 0 - echo "✅ $DELETE_ASG is now disabled" + + # Step 2: Terminate instances (if any) + INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$DELETE_ASG" \ + --query "AutoScalingGroups[0].Instances[*].InstanceId" \ + --output text) + + if [ -z "$INSTANCE_IDS" ]; then + echo "✅ $DELETE_ASG has no running instances." + else + for INSTANCE_ID in $INSTANCE_IDS; do + echo "đŸ”Ĩ Terminating instance in $DELETE_ASG: $INSTANCE_ID" + aws autoscaling terminate-instance-in-auto-scaling-group \ + --instance-id "$INSTANCE_ID" \ + --should-decrement-desired-capacity + done + + echo "âŗ Waiting for $DELETE_ASG to fully scale down..." + while true; do + INSTANCE_COUNT=$(aws autoscaling describe-auto-scaling-groups \ + --auto-scaling-group-names "$DELETE_ASG" \ + --query "AutoScalingGroups[0].Instances | length(@)" --output text) + + echo "🔄 $DELETE_ASG instance count: $INSTANCE_COUNT" + + if [ "$INSTANCE_COUNT" -eq 0 ]; then + echo "✅ All instances in $DELETE_ASG have been terminated." + break + fi + + sleep 10 + done + fi else echo "â„šī¸ No previous ASG to shut down" fi