Skip to content

Commit 9d44757

Browse files
authored
Create stop-ecs.yaml
1 parent 6ea1d70 commit 9d44757

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

.github/workflows/stop-ecs.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Stop All ECS Tasks
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
confirm:
7+
description: 'Type "yes" to confirm stopping all tasks'
8+
required: true
9+
type: string
10+
11+
env:
12+
AWS_REGION: ${{ vars.AWS_REGION || 'eu-central-1' }}
13+
ECS_SERVICE: ${{ vars.ECS_SERVICE || 'dev-zebra' }}
14+
ECS_CLUSTER: ${{ vars.ECS_CLUSTER || 'dev-zebra-cluster' }}
15+
16+
jobs:
17+
stop-tasks:
18+
name: Stop All ECS Tasks
19+
runs-on: ubuntu-latest
20+
environment: production
21+
steps:
22+
- name: Validate confirmation
23+
if: ${{ github.event.inputs.confirm != 'yes' }}
24+
run: |
25+
echo "Confirmation not provided. Please type 'yes' to confirm."
26+
exit 1
27+
28+
- name: Configure AWS credentials
29+
uses: aws-actions/configure-aws-credentials@v4
30+
with:
31+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
aws-region: ${{ env.AWS_REGION }}
34+
35+
- name: Stop all running tasks
36+
run: |
37+
echo "Fetching running tasks for service: $ECS_SERVICE in cluster: $ECS_CLUSTER"
38+
39+
TASK_ARNS=$(aws ecs list-tasks \
40+
--cluster $ECS_CLUSTER \
41+
--service-name $ECS_SERVICE \
42+
--query 'taskArns[]' \
43+
--output text)
44+
45+
if [ -z "$TASK_ARNS" ]; then
46+
echo "No running tasks found."
47+
exit 0
48+
fi
49+
50+
echo "Found tasks: $TASK_ARNS"
51+
52+
for TASK_ARN in $TASK_ARNS; do
53+
echo "Stopping task: $TASK_ARN"
54+
aws ecs stop-task \
55+
--cluster $ECS_CLUSTER \
56+
--task $TASK_ARN \
57+
--reason "Manually stopped via GitHub Actions"
58+
done
59+
60+
echo "All tasks have been stopped."
61+
62+
- name: Verify tasks stopped
63+
run: |
64+
echo "Waiting 30 seconds for tasks to stop..."
65+
sleep 30
66+
67+
echo "Current task status:"
68+
aws ecs describe-services \
69+
--cluster $ECS_CLUSTER \
70+
--services $ECS_SERVICE \
71+
--query 'services[0].{RunningCount:runningCount,PendingCount:pendingCount,DesiredCount:desiredCount}' \
72+
--output table

0 commit comments

Comments
 (0)