From cf31c1b0f68e08a7d89cd2742af4fb80562562d2 Mon Sep 17 00:00:00 2001 From: garvit3835 Date: Fri, 16 May 2025 15:35:07 +0530 Subject: [PATCH] ci to clean ec2 volumes in self-hosted --- .github/workflows/clean-aws-disks.yaml | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/workflows/clean-aws-disks.yaml diff --git a/.github/workflows/clean-aws-disks.yaml b/.github/workflows/clean-aws-disks.yaml new file mode 100644 index 0000000..1b89541 --- /dev/null +++ b/.github/workflows/clean-aws-disks.yaml @@ -0,0 +1,56 @@ +name: Clean AWS Unused EC2 Volumes + +on: + push: + branches: + - garvit/clean-aws-volumes + schedule: + - cron: '0 7 * * *' # Runs daily at 7:00 UTC + workflow_dispatch: + +jobs: + clean-ec2-volumes: + runs-on: ubuntu-latest + + permissions: + id-token: write + contents: read + + steps: + - name: Configure AWS Credential + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::484907513542:role/github-actions-oidc-role + aws-region: us-west-1 + + - name: Install jq + run: sudo apt-get update && sudo apt-get install -y jq + + - name: List and delete unattached EC2 volumes across all regions + run: | + echo "๐ŸŒ Fetching all available AWS regions..." + regions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output text) + + for region in $regions; do + echo "๐Ÿ” Checking region: $region" + + volumes=$(aws ec2 describe-volumes \ + --region "$region" \ + --filters Name=status,Values=available \ + --query "Volumes[].VolumeId" \ + --output text) + + if [[ -z "$volumes" ]]; then + echo "โœ… No unused EC2 volumes found in $region." + continue + fi + + for volume_id in $volumes; do + echo "โ†’ Deleting volume $volume_id in $region" + aws ec2 delete-volume --region "$region" --volume-id "$volume_id" + done + + echo "๐Ÿงน Cleanup done for region: $region" + done + + echo "๐ŸŽ‰ Cleanup complete!" \ No newline at end of file