Files
Scaffolder 4e5ccb2367
deploy / deploy (push) Successful in 5m12s
initial commit
Change-Id: I9ad6c3837a837ad68f28b6ea1901397a9a432227
2026-05-13 08:33:37 +00:00

57 lines
2.1 KiB
YAML

name: cleanup-expired
on:
schedule:
- cron: "17 * * * *"
workflow_dispatch:
jobs:
cleanup-expired:
runs-on:
- toolzoo-host
env:
AWS_REGION: us-east-1
APP_STACK_PREFIX: temp-stack
TTL_GRACE_SECONDS: "600"
steps:
- name: Cleanup expired app stacks
shell: bash
run: |
set -euo pipefail
now_epoch="$(date -u +%s)"
prefix="${APP_STACK_PREFIX}-"
stacks="$(aws cloudformation list-stacks \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE \
--query "StackSummaries[?starts_with(StackName, \`${prefix}\`)].StackName" \
--output text)"
for stack in $stacks; do
expires_at="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='ExpiresAt'].Value | [0]" --output text)"
owner_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Owner'].Value | [0]" --output text)"
repo_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Repository'].Value | [0]" --output text)"
if [[ "$expires_at" == "None" || -z "$expires_at" ]]; then
continue
fi
expires_epoch="$(date -u -d "$expires_at" +%s 2>/dev/null || echo 0)"
if [[ "$expires_epoch" -eq 0 ]]; then
continue
fi
if (( now_epoch >= expires_epoch + TTL_GRACE_SECONDS )); then
aws cloudformation delete-stack --stack-name "$stack"
aws cloudformation wait stack-delete-complete --stack-name "$stack"
if [[ "$owner_raw" != "None" && "$repo_raw" != "None" && -n "$owner_raw" && -n "$repo_raw" ]]; then
repo_slug="$(printf '%s' "$repo_raw" | tr '[:upper:]' '[:lower:]')"
ecr_repo="temp/${repo_slug}"
if aws ecr describe-repositories --repository-names "$ecr_repo" >/dev/null 2>&1; then
aws ecr delete-repository --repository-name "$ecr_repo" --force
fi
fi
fi
done