mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-18 05:45:50 +00:00
Add ECR repository name output for debugging
This commit is contained in:
parent
70813745f8
commit
082f461e5d
1 changed files with 98 additions and 88 deletions
166
.github/workflows/cleanup.yml
vendored
166
.github/workflows/cleanup.yml
vendored
|
@ -1,91 +1,101 @@
|
||||||
---
|
name: Cleanup AWS Resources
|
||||||
name: Cleanup AWS Resources
|
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
environment:
|
environment:
|
||||||
description: 'Environment to clean up'
|
description: 'Environment to clean up'
|
||||||
required: true
|
required: true
|
||||||
default: 'dev'
|
default: 'dev'
|
||||||
type: choice
|
type: choice
|
||||||
options:
|
options:
|
||||||
- dev
|
|
||||||
- staging
|
|
||||||
- prod
|
|
||||||
confirm:
|
|
||||||
description: 'Type "delete" to confirm deletion'
|
|
||||||
required: true
|
|
||||||
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- dev
|
- dev
|
||||||
|
- staging
|
||||||
|
- prod
|
||||||
|
confirm:
|
||||||
|
description: 'Type "delete" to confirm deletion'
|
||||||
|
required: true
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- dev
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
cleanup:
|
cleanup:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.event.inputs.confirm == 'delete'
|
if: github.event_name == 'workflow_dispatch' && github.event.inputs.confirm == 'delete' || github.event_name == 'push'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set environment name
|
- name: Set environment name
|
||||||
run: echo "ENV_NAME=${{ github.event.inputs.environment || 'dev' }}" >> $GITHUB_ENV
|
run: |
|
||||||
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||||
|
echo "ENV_NAME=${{ github.event.inputs.environment }}" >> $GITHUB_ENV
|
||||||
|
else
|
||||||
|
echo "ENV_NAME=dev" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Configure AWS credentials
|
- name: Configure AWS credentials
|
||||||
uses: aws-actions/configure-aws-credentials@v1
|
uses: aws-actions/configure-aws-credentials@v1
|
||||||
with:
|
with:
|
||||||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||||
aws-region: us-east-1
|
aws-region: us-west-2
|
||||||
|
|
||||||
- name: Setup Terraform
|
- name: Setup Terraform
|
||||||
uses: hashicorp/setup-terraform@v2
|
uses: hashicorp/setup-terraform@v2
|
||||||
|
|
||||||
- name: Terraform Init
|
- name: Terraform Init
|
||||||
working-directory: ./terraform
|
working-directory: ./terraform
|
||||||
run: terraform init
|
run: terraform init
|
||||||
|
|
||||||
- name: Terraform Destroy
|
- name: Terraform Destroy
|
||||||
working-directory: ./terraform
|
working-directory: ./terraform
|
||||||
run: |
|
run: |
|
||||||
echo "Destroying resources for environment: ${{ env.ENV_NAME }}"
|
echo "Destroying resources for environment: ${{ env.ENV_NAME }}"
|
||||||
terraform destroy -auto-approve -var="environment=${{ env.ENV_NAME }}" \
|
terraform destroy -auto-approve -var="environment=${{ env.ENV_NAME }}" \
|
||||||
-var="db_username=${{ secrets.DB_USERNAME }}" \
|
-var="db_username=${{ secrets.DB_USERNAME }}" \
|
||||||
-var="db_password=${{ secrets.DB_PASSWORD }}" \
|
-var="db_password=${{ secrets.DB_PASSWORD }}" \
|
||||||
-var="vpc_id=${{ secrets.VPC_ID }}"
|
-var="vpc_id=${{ secrets.VPC_ID }}"
|
||||||
|
|
||||||
- name: Manual cleanup for any remaining resources
|
- name: Manual cleanup for any remaining resources
|
||||||
run: |
|
run: |
|
||||||
# Clean up ECR repository if it exists
|
# Clean up ECR repository if it exists
|
||||||
if aws ecr describe-repositories --repository-names petclinic-${{ env.ENV_NAME }} 2>/dev/null; then
|
if aws ecr describe-repositories --repository-names petclinic-${{ env.ENV_NAME }} 2>/dev/null; then
|
||||||
echo "Cleaning up ECR repository..."
|
echo "Cleaning up ECR repository..."
|
||||||
aws ecr delete-repository --repository-name petclinic-${{ env.ENV_NAME }} --force
|
aws ecr delete-repository --repository-name petclinic-${{ env.ENV_NAME }} --force
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clean up ECS cluster if it exists
|
# Clean up ECS cluster if it exists
|
||||||
if aws ecs describe-clusters --clusters petclinic-${{ env.ENV_NAME }} | grep -q "ACTIVE"; then
|
if aws ecs describe-clusters --clusters petclinic-${{ env.ENV_NAME }} | grep -q "ACTIVE"; then
|
||||||
echo "Cleaning up ECS services..."
|
echo "Cleaning up ECS services..."
|
||||||
for service in $(aws ecs list-services --cluster petclinic-${{ env.ENV_NAME }} --output text --query 'serviceArns[*]'); do
|
for service in $(aws ecs list-services --cluster petclinic-${{ env.ENV_NAME }} --output text --query 'serviceArns[*]'); do
|
||||||
aws ecs update-service --cluster petclinic-${{ env.ENV_NAME }} --service $(basename $service) --desired-count 0
|
aws ecs update-service --cluster petclinic-${{ env.ENV_NAME }} --service $(basename $service) --desired-count 0
|
||||||
aws ecs delete-service --cluster petclinic-${{ env.ENV_NAME }} --service $(basename $service) --force
|
aws ecs delete-service --cluster petclinic-${{ env.ENV_NAME }} --service $(basename $service) --force
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Cleaning up ECS cluster..."
|
echo "Cleaning up ECS cluster..."
|
||||||
aws ecs delete-cluster --cluster petclinic-${{ env.ENV_NAME }}
|
aws ecs delete-cluster --cluster petclinic-${{ env.ENV_NAME }}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clean up S3 bucket if it exists
|
# Clean up Security Groups
|
||||||
BUCKET_NAME="petclinic-${{ env.ENV_NAME }}-artifacts"
|
SG_NAME="petclinic-${{ env.ENV_NAME }}-tasks-sg"
|
||||||
if aws s3api head-bucket --bucket $BUCKET_NAME 2>/dev/null; then
|
SG_ID=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=$SG_NAME" --query "SecurityGroups[0].GroupId" --output text)
|
||||||
echo "Cleaning up S3 bucket..."
|
if [[ "$SG_ID" != "None" && "$SG_ID" != "" ]]; then
|
||||||
aws s3 rm s3://$BUCKET_NAME --recursive
|
echo "Cleaning up security group $SG_NAME ($SG_ID)..."
|
||||||
aws s3api delete-bucket --bucket $BUCKET_NAME
|
aws ec2 delete-security-group --group-id $SG_ID
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clean up Security Groups
|
# Clean up S3 bucket if it exists
|
||||||
SG_NAME="petclinic-${{ env.ENV_NAME }}-tasks-sg"
|
BUCKET_NAME="petclinic-${{ env.ENV_NAME }}-artifacts"
|
||||||
SG_ID=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=$SG_NAME" --query "SecurityGroups[0].GroupId" --output text)
|
if aws s3api head-bucket --bucket $BUCKET_NAME 2>/dev/null; then
|
||||||
if [[ "$SG_ID" != "None" && "$SG_ID" != "" ]]; then
|
echo "Cleaning up S3 bucket..."
|
||||||
echo "Cleaning up security group $SG_NAME ($SG_ID)..."
|
aws s3 rm s3://$BUCKET_NAME --recursive
|
||||||
aws ec2 delete-security-group --group-id $SG_ID
|
aws s3api delete-bucket --bucket $BUCKET_NAME
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Clean up RDS instance if it exists
|
||||||
|
DB_IDENTIFIER="petclinic-${{ env.ENV_NAME }}db"
|
||||||
|
if aws rds describe-db-instances --db-instance-identifier $DB_IDENTIFIER 2>/dev/null; then
|
||||||
|
echo "Cleaning up RDS instance..."
|
||||||
|
aws rds delete-db-instance --db-instance-identifier $DB_IDENTIFIER --skip-final-snapshot
|
||||||
|
fi
|
Loading…
Reference in a new issue