Skip to content

Commit

Permalink
ci: remove cache (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPulse authored Sep 19, 2024
1 parent 2d9dfdf commit 8ee44d2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .github/workflows/cache-clean-by-branch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: cleanup caches by a branch
on:
pull_request:
types:
- closed

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup
run: |
gh extension install actions/gh-actions-cache
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge
72 changes: 72 additions & 0 deletions .github/workflows/cache-clean.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Cache Cleanup

on:
schedule:
- cron: '0 15 * * *'
workflow_dispatch:
inputs:
dry-run:
description: 'Perform a dry run without deleting caches'
required: false
default: 'false'

permissions:
actions: write

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Delete old caches
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const perPage = 100;
const maxPages = 10;
let page = 1;
let allCaches = [];
while (page <= maxPages) {
const response = await github.rest.actions.getActionsCacheList({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: perPage,
page: page,
});
allCaches = allCaches.concat(response.data.actions_caches);
if (response.data.actions_caches.length < perPage) {
break;
}
page++;
}
console.log(`Found ${allCaches.length} caches in total.`);
const cachesByKey = allCaches.reduce((acc, cache) => {
(acc[cache.key] = acc[cache.key] || []).push(cache);
return acc;
}, {});
const dryRunInput = github.event.inputs && github.event.inputs['dry-run'];
const dryRun = dryRunInput === 'true';
for (const [key, caches] of Object.entries(cachesByKey)) {
caches.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
for (let i = 1; i < caches.length; i++) {
const cacheId = caches[i].id;
if (dryRun) {
console.log(`[Dry Run] Old cache for key "${key}" (ID: ${cacheId}) would be deleted.`);
} else {
console.log(`Deleting old cache for key "${key}" (ID: ${cacheId}).`);
await github.rest.actions.deleteActionsCacheById({
owner: context.repo.owner,
repo: context.repo.repo,
cache_id: cacheId,
});
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/nightly-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- runner: 'macos-latest'
args: '--target aarch64-apple-darwin'
platform: 'macos-aarch64'
- runner: 'windows-github-lg'
- runner: 'windows-latest'
args: ''
platform: 'windows'
- runner: 'ubuntu-latest'
Expand Down

0 comments on commit 8ee44d2

Please sign in to comment.