Delete GCP resources #325
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This workflow is designed to delete old Google Cloud Platform (GCP) resources to save on costs. | |
| # | |
| # 1. Deletes specific instances in GCP older than a defined number of days. | |
| # 2. Deletes instance templates older than a set number of days. | |
| # 3. Deletes older disks not currently in use, with certain ones prefixed by commit hashes or "zebrad-". | |
| # 4. Deletes cache images from GCP, retaining a specified number of the latest images for certain types like zebrad checkpoint cache, zebrad tip cache, and lightwalletd + zebrad tip cache. | |
| # | |
| # It uses the gcloud CLI for its operations and is scheduled to run daily at 0700 UTC against the dev project. | |
| 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 | |
| permissions: | |
| contents: read | |
| jobs: | |
| delete-resources: | |
| name: Delete old GCP resources | |
| if: github.repository_owner == 'ZcashFoundation' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| environment: dev | |
| steps: | |
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2 | |
| with: | |
| persist-credentials: false | |
| # Setup gcloud CLI | |
| - name: Authenticate to Google Cloud | |
| id: auth | |
| uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 #v3.0.0 | |
| with: | |
| workload_identity_provider: '${{ vars.GCP_WIF }}' | |
| service_account: '${{ vars.GCP_DEPLOYMENTS_SA }}' | |
| - name: Set up Cloud SDK | |
| uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db #v3.0.1 | |
| # Deletes all mainnet and testnet 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. | |
| - name: Delete old instances | |
| run: | | |
| ./.github/workflows/scripts/gcp-delete-old-instances.sh | |
| # Deletes all the instance templates older than $DELETE_AGE_DAYS days. | |
| - name: Delete old instance templates | |
| run: | | |
| ./.github/workflows/scripts/gcp-delete-old-templates.sh | |
| # Deletes all mainnet and testnet disks older than $DELETE_AGE_DAYS days. | |
| - name: Delete old disks | |
| run: | | |
| ./.github/workflows/scripts/gcp-delete-old-disks.sh | |
| # Deletes mainnet and testnet 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, for each network: | |
| # - zebrad checkpoint cache | |
| # - zebrad tip cache | |
| # - lightwalletd + zebrad tip cache | |
| # | |
| # TODO: | |
| # - refactor out repeated shell script code | |
| - name: Delete old cache images | |
| run: | | |
| ./.github/workflows/scripts/gcp-delete-old-cache-images.sh | |
| delete-resources-success: | |
| name: Delete GCP resources success | |
| runs-on: ubuntu-latest | |
| # Only run when the owner-specific cleanup jobs were eligible to execute | |
| if: >- | |
| ${{ | |
| always() && | |
| github.repository_owner == 'ZcashFoundation' | |
| }} | |
| needs: | |
| - delete-resources | |
| timeout-minutes: 1 | |
| steps: | |
| - name: Decide whether the needed jobs succeeded or failed | |
| uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe #v1.2.2 | |
| with: | |
| jobs: ${{ toJSON(needs) }} |