name: 'Tiles Cleanup'
on:
  workflow_dispatch:
  schedule:
    # Every day at midnight
    - cron: '0 0 * * *'

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: true

jobs:
  cleanup_tiles:
    name: Cleanup Tiles & Styles
    defaults:
      run:
        working-directory: 'deployment'
    runs-on: ubuntu-latest
    outputs:
      KEY: ${{ steps.latest_build.outputs.KEY }}
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Setup Rclone
        uses: AnimMouse/setup-rclone@26ef2133e09fd383329a1d4c1fbe6a71db1976c9 # v1.12.0
        with:
          disable_base64: true
          rclone_config: |
            [r2]
            type = s3
            provider = Cloudflare
            region = auto
            endpoint = https://601adaaa1eab190cdfe2945f9a6c89d0.r2.cloudflarestorage.com
            access_key_id = ${{ secrets.CLOUDFLARE_TILES_R2_KV_TOKEN_ID }}
            secret_access_key = ${{ secrets.CLOUDFLARE_TILES_R2_KV_TOKEN_HASHED_VALUE }}

      - name: Cleanup all non-latest tiles
        run: ./scripts/r2-cleanup.sh
