name: Delete GCP resources on: # Run daily, when most devs aren't working # 0700 UTC is after AEST working hours but before ET working hours schedule: - cron: "0 7 * * *" workflow_dispatch: env: # Delete all resources created before $DELETE_INSTANCE_DAYS days ago. # We keep this short to reduce CPU, RAM, and storage costs. DELETE_INSTANCE_DAYS: 3 # Delete all other resources created before $DELETE_AGE_DAYS days ago. # We keep this short to reduce storage costs. DELETE_AGE_DAYS: 2 # But keep the latest $KEEP_LATEST_IMAGE_COUNT images of each type. # We keep this small to reduce storage costs. KEEP_LATEST_IMAGE_COUNT: 2 jobs: delete-resources: name: Delete old GCP resources runs-on: ubuntu-latest permissions: contents: 'read' id-token: 'write' steps: - uses: actions/checkout@v3.2.0 with: persist-credentials: false # Setup gcloud CLI - name: Authenticate to Google Cloud id: auth uses: google-github-actions/auth@v1.0.0 with: retries: '3' workload_identity_provider: 'projects/143793276228/locations/global/workloadIdentityPools/github-actions/providers/github-oidc' service_account: 'github-service-account@zealous-zebra.iam.gserviceaccount.com' - name: Set up Cloud SDK uses: google-github-actions/setup-gcloud@v1.0.1 # Deletes all instances older than $DELETE_INSTANCE_DAYS days. # # We only delete instances that end in 7 or more hex characters, # to avoid deleting managed instance groups and manually created instances. # # ${INSTANCE_AND_ZONE} expands to: # --zone= # so it can't be shell-quoted. - name: Delete old instances run: | DELETE_BEFORE_DATE=$(date --date="$DELETE_INSTANCE_DAYS days ago" '+%Y%m%d') IFS=$'\n' INSTANCES=$(gcloud compute instances list --sort-by=creationTimestamp --filter="name~-[0-9a-f]{7,}$ AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME,ZONE)' | \ sed 's/\(.*\)\t\(.*\)/\1 --zone=\2/') for INSTANCE_AND_ZONE in $INSTANCES do IFS=$' ' gcloud compute instances delete --verbosity=info ${INSTANCE_AND_ZONE} --delete-disks=all || continue IFS=$'\n' done # Deletes all the instance templates older than $DELETE_AGE_DAYS days. - name: Delete old instance templates run: | DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d') TEMPLATES=$(gcloud compute instance-templates list --sort-by=creationTimestamp --filter="name~-[0-9a-f]{7,}$ AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)') for TEMPLATE in $TEMPLATES do gcloud compute instance-templates delete "${TEMPLATE}" || continue done # Deletes all the disks older than $DELETE_AGE_DAYS days. # # Disks that are attached to an instance template can't be deleted, so it is safe to try to delete all disks here. # # ${DISK_AND_LOCATION} expands to: # --[zone|region]= # so it can't be shell-quoted. - name: Delete old disks run: | DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d') IFS=$'\n' # Disks created by PR jobs, and other jobs that use a commit hash COMMIT_DISKS=$(gcloud compute disks list --sort-by=creationTimestamp --filter="name~-[0-9a-f]{7,}$ AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME,LOCATION,LOCATION_SCOPE)' | \ sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1 --\3=\2/') for DISK_AND_LOCATION in $COMMIT_DISKS do IFS=$' ' gcloud compute disks delete --verbosity=info ${DISK_AND_LOCATION} || continue IFS=$'\n' done IFS=$'\n' # Disks created by managed instance groups, and other jobs that start with "zebrad-" ZEBRAD_DISKS=$(gcloud compute disks list --sort-by=creationTimestamp --filter="name~^zebrad- AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME,LOCATION,LOCATION_SCOPE)' | \ sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1 --\3=\2/') for DISK_AND_LOCATION in $ZEBRAD_DISKS do IFS=$' ' gcloud compute disks delete --verbosity=info ${DISK_AND_LOCATION} || continue IFS=$'\n' done # Deletes cache images older than $DELETE_AGE_DAYS days. # # Keeps all images younger than $DELETE_AGE_DAYS. # Also keeps $KEEP_LATEST_IMAGE_COUNT older images of each type: # - zebrad checkpoint cache # - zebrad tip cache # - lightwalletd + zebrad tip cache # # TODO: # - keep the latest $KEEP_LATEST_IMAGE_COUNT, if there are at least that many recent images, delete all the outdated images # - when we add testnet to the workflows, keep the latest $KEEP_LATEST_IMAGE_COUNT testnet images, # and the latest $KEEP_LATEST_IMAGE_COUNT mainnet images. - name: Delete old cache disks run: | DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d') ZEBRAD_CHECKPOINT_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^zebrad-cache-.*net-checkpoint AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)') KEPT_IMAGES=0 for IMAGE in $ZEBRAD_CHECKPOINT_IMAGES do if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]]; then KEPT_IMAGES=$((KEPT_IMAGES+1)) echo "Keeping image $KEPT_IMAGES named $IMAGE" continue fi gcloud compute images delete "${IMAGE}" || continue done ZEBRAD_TIP_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^zebrad-cache-.*net-tip AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)') KEPT_IMAGES=0 for IMAGE in $ZEBRAD_TIP_IMAGES do if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]]; then KEPT_IMAGES=$((KEPT_IMAGES+1)) echo "Keeping image $KEPT_IMAGES named $IMAGE" continue fi gcloud compute images delete "${IMAGE}" || continue done LWD_TIP_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^lwd-cache-.*net-tip AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)') KEPT_IMAGES=0 for IMAGE in $LWD_TIP_IMAGES do if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]]; then KEPT_IMAGES=$((KEPT_IMAGES+1)) echo "Keeping image $KEPT_IMAGES named $IMAGE" continue fi gcloud compute images delete "${IMAGE}" || continue done