name: Clear cache

on:
  schedule:
    # Run everyday at: https://crontab.guru/#0_6_*_*_*.
    - cron: '0 6 * * *'
  workflow_dispatch:

permissions:
  actions: write

jobs:
  clear-cache:
    runs-on: ubuntu-latest
    steps:
      - name: Clear Firefox cache
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            console.log("Querying")

            const caches = await github.paginate(
              github.rest.actions.getActionsCacheList,
              {
                owner: context.repo.owner,
                repo: context.repo.repo,
              }
            )
            console.log(caches.length)
            for (const cache of caches) {
              if (cache.key.toLowerCase().startsWith('firefox')) {
                console.log(cache)
                github.rest.actions.deleteActionsCacheById({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  cache_id: cache.id,
                })
              }
            }
            console.log("Clear completed")
