generated from StabilityNexus/Template-Repo
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Add Cache Reaper workflow to manage stale and orphaned caches #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kpj2006
wants to merge
8
commits into
AOSSIE-Org:main
Choose a base branch
from
kpj2006:patch-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a0c410
Add Cache Reaper workflow to manage stale and orphaned caches
kpj2006 0ecaed9
Add cache workaround comment to Cache Reaper workflow
kpj2006 6503455
Update .github/workflows/cache-reaper.yml
kpj2006 8e704da
Update .github/workflows/cache-reaper.yml
kpj2006 24452f5
Update .github/workflows/cache-reaper.yml
kpj2006 2b2d317
Validate stale_days input as a positive integer in Cache Reaper workflow
kpj2006 18966d7
Merge branch 'patch-3' of https://github.com/kpj2006/Template-Repo in…
kpj2006 faa71d6
Update .github/workflows/cache-reaper.yml
kpj2006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # cache workaround : https://prosopo.io/blog/github-actions-cache-chaos/ | ||
| name: 🧹 Cache Reaper | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: '0 2 * * 0' | ||
| workflow_dispatch: | ||
| inputs: | ||
| dry_run: | ||
| description: 'Dry run (log only)' | ||
| default: 'false' | ||
| type: choice | ||
| options: ['false', 'true'] | ||
| stale_days: | ||
| description: 'Days before cache is stale' | ||
| default: '7' | ||
| type: string | ||
|
|
||
| permissions: | ||
| actions: write | ||
| contents: read | ||
|
|
||
| jobs: | ||
| reap: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Delete stale & orphaned caches | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const DRY_RUN = '${{ github.event.inputs.dry_run }}' === 'true'; | ||
| const staleDaysRaw = '${{ github.event.inputs.stale_days }}' || '7'; | ||
| const STALE_DAYS = Number(staleDaysRaw); | ||
| if (!Number.isInteger(STALE_DAYS) || STALE_DAYS < 1) { | ||
| throw new Error(`stale_days must be a positive integer, got "${staleDaysRaw}"`); | ||
| } | ||
| const cutoff = new Date(); | ||
| cutoff.setDate(cutoff.getDate() - STALE_DAYS); | ||
|
|
||
| // Paginate all caches | ||
| let page = 1, all = []; | ||
| while (true) { | ||
| let data; | ||
| try { | ||
| ({ data } = await github.rest.actions.getActionsCacheList({ | ||
| owner: context.repo.owner, repo: context.repo.repo, | ||
| per_page: 100, page, | ||
| })); | ||
| } catch (err) { | ||
| core.setFailed(`Failed to list caches (page ${page}): ${err.message}`); | ||
| return; | ||
| } | ||
| if (!data.actions_caches.length) break; | ||
| all = all.concat(data.actions_caches); | ||
| if (data.actions_caches.length < 100) break; | ||
| page++; | ||
| } | ||
|
|
||
| let deleted = 0, freed = 0; | ||
| for (const cache of all) { | ||
| const stale = new Date(cache.last_accessed_at) < cutoff; | ||
| const zeroByte = (cache.size_in_bytes || 0) === 0; | ||
| let closedPR = false; | ||
| const prMatch = cache.ref?.match(/refs\/pull\/(\d+)\/merge/); | ||
| if (prMatch) { | ||
| try { | ||
| const { data: pr } = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: parseInt(prMatch[1]), | ||
| }); | ||
| closedPR = pr.state === 'closed'; | ||
| } catch (err) { | ||
| // PR may have been deleted; treat as closed | ||
| closedPR = true; | ||
| } | ||
| } | ||
|
|
||
| if (!stale && !zeroByte && !closedPR) continue; | ||
|
|
||
| const reason = stale ? `stale(${STALE_DAYS}d)` : zeroByte ? 'zero-byte' : 'closed-PR'; | ||
| console.log(`${DRY_RUN ? '[DRY]' : '🗑️'} ${reason} → ${cache.key}`); | ||
|
|
||
| if (!DRY_RUN) { | ||
| try { | ||
| await github.rest.actions.deleteActionsCacheById({ | ||
| owner: context.repo.owner, repo: context.repo.repo, | ||
| cache_id: cache.id, | ||
| }); | ||
| deleted++; | ||
| freed += cache.size_in_bytes || 0; | ||
| } catch (err) { | ||
| console.log(`⚠️ Failed to delete ${cache.key}: ${err.message}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(`Done: ${deleted} deleted, ${(freed/1024/1024).toFixed(1)} MB freed`); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.