zebra/.github/workflows/delete-gcp-resources.yml

63 lines
2.6 KiB
YAML
Raw Normal View History

name: Delete GCP resources
on:
schedule:
- cron: "0 0 1 * *"
workflow_dispatch:
jobs:
delete-resources:
name: Delete old GCP resources
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
# Setup gcloud CLI
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v0.8.1
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'
token_format: 'access_token'
# Deletes all the instances template older than 30 days
- name: Delete old instance templates
run: |
TEMPLATES=$(gcloud compute instance-templates list --sort-by=creationTimestamp --filter="creationTimestamp < $(date --date='30 days ago' '+%Y%m%d')" --format='value(NAME)')
for TEMPLATE in $TEMPLATES
do
gcloud compute instance-templates delete ${TEMPLATE} --quiet || continue
done
# Deletes cached images older than 90 days
#
# A search is done is done for each of this images:
# - Images created on Pull Requests older than 30 days
# - Images created on the `main` branch older than 60 days
# - Any other remaining image older than 90 days
# TODO: we should improve this approach and filter by disk type, and just keep the 2 latest images of each type (zebra checkpoint, zebra tip, lwd tip)
- name: Delete old cache disks
run: |
PR_OLD_CACHE_DISKS=$(gcloud compute images list --sort-by=creationTimestamp --filter="name~-cache-.+[0-9a-f]+-merge AND creationTimestamp < $(date --date='30 days ago' '+%Y%m%d')" --format='value(NAME)')
for DISK in $PR_OLD_CACHE_DISKS
do
gcloud compute image delete ${DISK} --quiet || continue
done
MAIN_OLD_CACHE_DISKS=$(gcloud compute images list --sort-by=creationTimestamp --filter="name~-cache-main AND creationTimestamp < $(date --date='60 days ago' '+%Y%m%d')" --format='value(NAME)')
for DISK in $MAIN_OLD_CACHE_DISKS
do
gcloud compute image delete ${DISK} --quiet || continue
done
ALL_OLD_CACHE_DISKS=$(gcloud compute images list --sort-by=creationTimestamp --filter="name~-cache- AND creationTimestamp < $(date --date='90 days ago' '+%Y%m%d')" --format='value(NAME)')
for DISK in $ALL_OLD_CACHE_DISKS
do
gcloud compute image delete ${DISK} --quiet || continue
done