diff --git a/.github/workflows/delete-gcp-resources.yml b/.github/workflows/delete-gcp-resources.yml new file mode 100644 index 00000000..6cb58f7a --- /dev/null +++ b/.github/workflows/delete-gcp-resources.yml @@ -0,0 +1,33 @@ +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.0 + with: + 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