Skip to content

Commit c46c565

Browse files
authored
fix: pause resources and prune env (#49)
1 parent aa37a19 commit c46c565

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: .Destroy Stack
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
### Required
7+
environment_name:
8+
description: 'The name of the environment to deploy to'
9+
required: true
10+
default: 'dev'
11+
type: string
12+
command:
13+
description: 'The terragrunt command to run'
14+
default: 'destroy'
15+
required: false
16+
type: string
17+
tag:
18+
description: 'The tag of the containers to deploy'
19+
default: 'latest'
20+
type: string
21+
required: false
22+
app_env:
23+
required: false
24+
type: string
25+
description: 'The APP env separates between AWS ENV and Actual APP, since AWS dev is where PR, and TEST is deployed'
26+
env:
27+
AWS_REGION: ca-central-1
28+
jobs:
29+
stack-prefix:
30+
name: Stack Prefix
31+
uses: ./.github/workflows/.stack-prefix.yml
32+
api:
33+
name: Destroy API
34+
needs: [stack-prefix]
35+
uses: ./.github/workflows/.deployer.yml
36+
with:
37+
environment_name: ${{ inputs.environment_name }}
38+
command: ${{ inputs.command }}
39+
tag: ${{ inputs.tag }}
40+
app_env: ${{ inputs.app_env }}
41+
working_directory: api
42+
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
43+
secrets: inherit
44+
db:
45+
name: Destroy Database
46+
needs: [stack-prefix, api]
47+
uses: ./.github/workflows/.deployer.yml
48+
with:
49+
environment_name: ${{ inputs.environment_name }}
50+
command: ${{ inputs.command }}
51+
working_directory: database
52+
app_env: ${{ inputs.app_env }}
53+
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
54+
secrets: inherit
55+
56+
cloudfront:
57+
name: Destroy Cloudfront
58+
needs: [stack-prefix]
59+
uses: ./.github/workflows/.deployer.yml
60+
with:
61+
environment_name: ${{ inputs.environment_name }}
62+
command: ${{ inputs.command }}
63+
tag: ${{ inputs.tag }}
64+
app_env: ${{ inputs.app_env }}
65+
working_directory: frontend
66+
stack_prefix: ${{ needs.stack-prefix.outputs.stack_prefix }}
67+
secrets: inherit
68+

.github/workflows/pause-resources.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@ jobs:
2727
- name: Pause AWS Resources
2828
shell: bash
2929
run: |
30-
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json
31-
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service --desired-count 0 --no-cli-pager --output json
30+
CLUSTER_NAME="ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev"
31+
SERVICE_NAME="${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service"
32+
CLUSTER_EXISTS=$(aws ecs describe-clusters --clusters $CLUSTER_NAME --query 'clusters[0].status' --output text)
33+
if [ "$CLUSTER_EXISTS" = "ACTIVE" ]; then
34+
SERVICE_EXISTS=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].status' --output text)
35+
if [ "$SERVICE_EXISTS" = "ACTIVE" ]; then
36+
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/$CLUSTER_NAME/$SERVICE_NAME --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json
37+
else
38+
echo "ECS service $SERVICE_NAME does not exist in cluster $CLUSTER_NAME."
39+
fi
40+
else
41+
echo "ECS cluster $CLUSTER_NAME does not exist."
42+
fi
3243
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --query 'DBClusters[0].Status' --output text)
3344
if [ "$DB_CLUSTER_STATUS" = "available" ]; then
3445
aws rds stop-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --no-cli-pager --output json
@@ -52,8 +63,19 @@ jobs:
5263
- name: Pause AWS Resources
5364
shell: bash
5465
run: |
55-
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test/${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json
56-
aws ecs update-service --cluster ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test --service ${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service --desired-count 0 --no-cli-pager --output json
66+
CLUSTER_NAME="ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test"
67+
SERVICE_NAME="${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service"
68+
CLUSTER_EXISTS=$(aws ecs describe-clusters --clusters $CLUSTER_NAME --query 'clusters[0].status' --output text)
69+
if [ "$CLUSTER_EXISTS" = "ACTIVE" ]; then
70+
SERVICE_EXISTS=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].status' --output text)
71+
if [ "$SERVICE_EXISTS" = "ACTIVE" ]; then
72+
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/$CLUSTER_NAME/$SERVICE_NAME --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json
73+
else
74+
echo "ECS service $SERVICE_NAME does not exist in cluster $CLUSTER_NAME."
75+
fi
76+
else
77+
echo "ECS cluster $CLUSTER_NAME does not exist."
78+
fi
5779
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --query 'DBClusters[0].Status' --output text)
5880
if [ "$DB_CLUSTER_STATUS" = "available" ]; then
5981
aws rds stop-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --no-cli-pager --output json

.github/workflows/prune-env.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ jobs:
2525
destroy-dev:
2626
name: Destroy Stack Dev
2727
if: ${{ github.event.inputs.app_env == 'dev' }}
28-
uses: ./.github/workflows/.deploy_stack.yml
28+
uses: ./.github/workflows/.destroy_stack.yml
2929
secrets: inherit
3030
with:
3131
environment_name: ${{ github.event.inputs.environment_name }}
32-
command: destroy
3332
app_env: ${{ github.event.inputs.app_env }}
3433
destroy-test:
3534
name: Destroy Stack Test
3635
if: ${{ github.event.inputs.app_env == 'test' }}
37-
uses: ./.github/workflows/.deploy_stack.yml
36+
uses: ./.github/workflows/.destroy_stack.yml
3837
secrets: inherit
3938
with:
4039
environment_name: ${{ github.event.inputs.environment_name }}
41-
command: destroy
4240
app_env: ${{ github.event.inputs.app_env }}

0 commit comments

Comments
 (0)