|
| 1 | +# Cache Cleanup |
| 2 | +# |
| 3 | +# Delete caches when a pull request is closed or on workflow dispatch. |
| 4 | +# |
| 5 | +# References: |
| 6 | +# |
| 7 | +# - https://docs.github.com/actions/learn-github-actions/contexts |
| 8 | +# - https://docs.github.com/actions/learn-github-actions/expressions |
| 9 | +# - https://docs.github.com/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries |
| 10 | +# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#pull_request |
| 11 | +# - https://docs.github.com/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch |
| 12 | +# - https://docs.github.com/actions/using-workflows/using-github-cli-in-workflows |
| 13 | +# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request |
| 14 | +# - https://docs.github.com/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_dispatch |
| 15 | +# - https://github.com/actions/checkout |
| 16 | +# - https://github.com/actions/gh-actions-cache |
| 17 | +# - https://github.com/hmarr/debug-action |
| 18 | + |
| 19 | +--- |
| 20 | +name: cache-cleanup |
| 21 | +on: |
| 22 | + pull_request: |
| 23 | + types: |
| 24 | + - closed |
| 25 | + workflow_dispatch: |
| 26 | + inputs: |
| 27 | + all: |
| 28 | + default: false |
| 29 | + description: delete caches without filtering by branch |
| 30 | + type: boolean |
| 31 | +permissions: |
| 32 | + actions: write |
| 33 | +env: |
| 34 | + BRANCH: | |
| 35 | + ${{ github.event.number && format('refs/pull/{0}/merge', github.event.number) || github.ref }} |
| 36 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | +concurrency: |
| 38 | + cancel-in-progress: true |
| 39 | + group: ${{ github.workflow }}-${{ format('refs/heads/{0}', github.head_ref || github.ref_name) }} |
| 40 | +jobs: |
| 41 | + cache-cleanup: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + steps: |
| 44 | + - id: debug |
| 45 | + name: Print environment variables and event payload |
| 46 | + uses: hmarr/debug-action@v2.1.0 |
| 47 | + - id: checkout |
| 48 | + name: Checkout main |
| 49 | + uses: actions/checkout@v3.5.0 |
| 50 | + with: |
| 51 | + persist-credentials: false |
| 52 | + ref: main |
| 53 | + - id: gh-actions-cache |
| 54 | + name: Install actions/gh-actions-cache |
| 55 | + run: gh extension install actions/gh-actions-cache |
| 56 | + - id: cleanup |
| 57 | + name: Delete caches${{ !inputs.all && format(' created by {0}', env.BRANCH) || '' }} |
| 58 | + env: |
| 59 | + BRANCH_FILTER: ${{ !inputs.all && format('--branch {0}', env.BRANCH) || '' }} |
| 60 | + run: | |
| 61 | + # prevent workflow failure while deleting cache keys |
| 62 | + set +e |
| 63 | +
|
| 64 | + # delete all caches or caches created by ${{ env.BRANCH }} |
| 65 | + for key in $(gh actions-cache list $BRANCH_FILTER --limit 100 | cut -f 1); do |
| 66 | + gh actions-cache delete $key $BRANCH_FILTER --confirm |
| 67 | + done |
0 commit comments