Skip to content

πŸ‘‹ caches

πŸ‘‹ caches #31

# Mostly for DeterminateSystems/magic-nix-cache-action
name: πŸ‘‹ caches
on:
push:
branches: [main]
pull_request:
types:
- closed
schedule:
# Every 12:42 JST
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
- cron: '42 3 * * *'
workflow_dispatch:
defaults:
run:
shell: 'bash -euxo pipefail {0}'
# Take care all jobs do not scope same reference. If then, the steps should be in one job and runs sequential
jobs:
prs:
# Basically skip to avoid rate limit with the N+1
if: github.event_name == 'workflow_dispatch'
concurrency:
group: '${{ github.workflow }}-prs' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# Intentionally cover all merged PRs to keep simple logics. See https://github.com/kachick/times_kachick/issues/311
- name: Cleanup caches in merged PRs
run: |
gh pr list --state merged --json number --jq '.[].number' --limit 2000 | \
xargs --no-run-if-empty -I '{}' gh cache list --sort size_in_bytes --order desc --json id --jq '.[].id' --limit 100 --ref 'refs/pull/{}/merge' | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}
pr:
if: github.event_name == 'pull_request'
concurrency:
group: '${{ github.workflow }}-pr-${{ github.event.pull_request.number }}' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# Intentionally cover all merged PRs to keep simple logics. See https://github.com/kachick/times_kachick/issues/311
- name: Cleanup caches in the PR
run: |
gh cache list --sort size_in_bytes --order desc --json id --jq '.[].id' --limit 100 --ref 'refs/pull/${{ github.event.pull_request.number }}/merge' | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}
main:
concurrency:
group: '${{ github.workflow }}-main' # Dont' use ${{ github.job }} here
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
# 10Gib limit is too small even if focused on default branch
- name: Cleanup caches in main branch
run: |
# We can lexicographical sort RFC3399 formatted timestamp
threshold="$(date --rfc-3339='date' --date='-4 days')"
gh cache list --limit 2000 --ref 'refs/heads/main' --sort last_accessed_at --order asc --json 'id,lastAccessedAt,sizeInBytes' --jq ".[] | select(.lastAccessedAt < \"$threshold\") | .id" | \
xargs --no-run-if-empty --max-lines=1 gh cache delete
env:
GH_TOKEN: ${{ github.token }}