Add ECR repository name output for debugging

This commit is contained in:
Favee 2025-04-15 19:37:32 +01:00
parent 6cd96461a0
commit 70813745f8
2 changed files with 126 additions and 0 deletions

91
.github/workflows/cleanup.yml vendored Normal file
View file

@ -0,0 +1,91 @@
---
name: Cleanup AWS Resources
on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to clean up'
required: true
default: 'dev'
type: choice
options:
- dev
- staging
- prod
confirm:
description: 'Type "delete" to confirm deletion'
required: true
push:
branches:
- dev
jobs:
cleanup:
runs-on: ubuntu-latest
if: github.event.inputs.confirm == 'delete'
steps:
- uses: actions/checkout@v3
- name: Set environment name
run: echo "ENV_NAME=${{ github.event.inputs.environment || 'dev' }}" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
working-directory: ./terraform
run: terraform init
- name: Terraform Destroy
working-directory: ./terraform
run: |
echo "Destroying resources for environment: ${{ env.ENV_NAME }}"
terraform destroy -auto-approve -var="environment=${{ env.ENV_NAME }}" \
-var="db_username=${{ secrets.DB_USERNAME }}" \
-var="db_password=${{ secrets.DB_PASSWORD }}" \
-var="vpc_id=${{ secrets.VPC_ID }}"
- name: Manual cleanup for any remaining resources
run: |
# Clean up ECR repository if it exists
if aws ecr describe-repositories --repository-names petclinic-${{ env.ENV_NAME }} 2>/dev/null; then
echo "Cleaning up ECR repository..."
aws ecr delete-repository --repository-name petclinic-${{ env.ENV_NAME }} --force
fi
# Clean up ECS cluster if it exists
if aws ecs describe-clusters --clusters petclinic-${{ env.ENV_NAME }} | grep -q "ACTIVE"; then
echo "Cleaning up ECS services..."
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 delete-service --cluster petclinic-${{ env.ENV_NAME }} --service $(basename $service) --force
done
echo "Cleaning up ECS cluster..."
aws ecs delete-cluster --cluster petclinic-${{ env.ENV_NAME }}
fi
# Clean up S3 bucket if it exists
BUCKET_NAME="petclinic-${{ env.ENV_NAME }}-artifacts"
if aws s3api head-bucket --bucket $BUCKET_NAME 2>/dev/null; then
echo "Cleaning up S3 bucket..."
aws s3 rm s3://$BUCKET_NAME --recursive
aws s3api delete-bucket --bucket $BUCKET_NAME
fi
# Clean up Security Groups
SG_NAME="petclinic-${{ env.ENV_NAME }}-tasks-sg"
SG_ID=$(aws ec2 describe-security-groups --filters "Name=group-name,Values=$SG_NAME" --query "SecurityGroups[0].GroupId" --output text)
if [[ "$SG_ID" != "None" && "$SG_ID" != "" ]]; then
echo "Cleaning up security group $SG_NAME ($SG_ID)..."
aws ec2 delete-security-group --group-id $SG_ID
fi

View file

@ -97,6 +97,16 @@
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Ensure ECR Repository exists
run: |
echo "Checking for ECR repository: petclinic-${{ env.ENV_NAME }}"
if ! aws ecr describe-repositories --repository-names petclinic-${{ env.ENV_NAME }} 2>/dev/null; then
echo "ECR repository doesn't exist, creating it now..."
aws ecr create-repository --repository-name petclinic-${{ env.ENV_NAME }} --image-scanning-configuration scanOnPush=true
else
echo "ECR repository already exists"
fi
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
@ -109,13 +119,38 @@
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
- name: Ensure ECS service exists
run: |
CLUSTER_NAME="petclinic-${{ env.ENV_NAME }}"
SERVICE_NAME="petclinic-service"
# Check if cluster exists
if ! aws ecs describe-clusters --clusters $CLUSTER_NAME | grep -q "ACTIVE"; then
echo "Error: ECS cluster $CLUSTER_NAME doesn't exist or isn't active"
exit 1
fi
# Check if service exists
if ! aws ecs list-services --cluster $CLUSTER_NAME | grep -q $SERVICE_NAME; then
echo "Service $SERVICE_NAME doesn't exist in cluster $CLUSTER_NAME, creating it..."
# Here you would add code to create the service
# This would require task definition setup as well
echo "For now, skipping deployment as service doesn't exist yet"
echo "DEPLOYMENT_NEEDED=false" >> $GITHUB_ENV
else
echo "Service exists, proceeding with deployment"
echo "DEPLOYMENT_NEEDED=true" >> $GITHUB_ENV
fi
- name: Deploy to ECS
if: env.DEPLOYMENT_NEEDED == 'true'
run: |
aws ecs update-service --cluster petclinic-${{ env.ENV_NAME }} \
--service petclinic-service \
--force-new-deployment
- name: Verify Deployment
if: env.DEPLOYMENT_NEEDED == 'true'
run: |
echo "Waiting for deployment to complete..."
aws ecs wait services-stable --cluster petclinic-${{ env.ENV_NAME }} \