From c921e1ef56def1de87e851f4e83d7e02b202fad8 Mon Sep 17 00:00:00 2001 From: sonwonjae Date: Tue, 22 Apr 2025 02:01:37 +0900 Subject: [PATCH 1/2] fix: more than quickly remove instance --- .github/workflows/Release.yml | 48 ++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 6cbddf7..0de9770 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 \ @@ -272,7 +286,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 From bdbd18e26e5bcccc666725b4836e77f4d68ef250 Mon Sep 17 00:00:00 2001 From: sonwonjae Date: Tue, 22 Apr 2025 02:05:07 +0900 Subject: [PATCH 2/2] fix: add DELETE_ASG env --- .github/workflows/Release.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 0de9770..3a7da63 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -214,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}"