Skip to content

Commit

Permalink
remove caches older than 30 days
Browse files Browse the repository at this point in the history
  • Loading branch information
nawazkh committed Oct 7, 2024
1 parent ec7e9b0 commit 63ae05a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/remove_old_caches.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Cache Cleanup

on:
workflow_dispatch: # Manually trigger the workflow
schedule:
- cron: '0 0 * * 0' # Run every Sunday at midnight (UTC)

jobs:
cleanup:
name: Cleanup Old Caches
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v4

- name: Install gh CLI
run: |
sudo apt-get update
sudo apt-get install gh
- name: Install gh-actions-cache extension
run: |
gh extension install actions/gh-actions-cache
- name: List all caches
run: |
gh actions-cache list
- name: Delete caches older than 30 days
env:
MAX_CACHE_AGE_DAYS: 30
run: |
echo "Fetching list of caches..."
# List caches
caches=$(gh actions-cache list --json id,createdAt)
echo "Caches found: $(echo "$caches" | jq '. | length')"
# Get current time
current_time=$(date +%s)
# Loop through all caches and delete those older than MAX_CACHE_AGE_DAYS
for cache_id in $(echo "$caches" | jq -r '.[] | select((('"$current_time"' - (.createdAt | fromdateiso8601 | mktime)) / 86400) > '"$MAX_CACHE_AGE_DAYS"') | .id'); do
echo "Deleting cache with ID: $cache_id"
gh actions-cache delete $cache_id --confirm
echo "Deleted cache $cache_id"
done

0 comments on commit 63ae05a

Please sign in to comment.