Skip to content
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

CI: Add reusable workflow to clear caches within a repository #43

Merged
merged 13 commits into from
Sep 9, 2022
14 changes: 4 additions & 10 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ runs:
run: python .github/assistant.py set-oldest-versions
shell: bash

- run: python -c "import time ; days = time.time() / 60 / 60 / 24 ; print(f'TIME_PERIOD={int(days / ${{ inputs.interval }})}')" >> $GITHUB_ENV
if: inputs.requires == 'latest'
shell: bash

# Note: This uses an internal pip API and may not always work
# https://github.com/actions/cache/blob/master/examples.md#multiple-oss-in-a-workflow
- name: Determine caches
env:
PWD: $(pwd)
Expand All @@ -49,13 +43,13 @@ runs:
uses: actions/cache@v3
with:
path: $PIP_CACHE_DIR
key: ${{ runner.os }}-py${{ inputs.python-version }}-pip-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}-${{ hashFiles('requirements.txt') }}
restore-keys: ${{ runner.os }}-py${{ inputs.python-version }}-pip-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}-
key: ${{ runner.os }}-py${{ inputs.python-version }}-pip-${{ inputs.offset }}-${{ hashFiles('requirements.txt') }}
restore-keys: ${{ runner.os }}-py${{ inputs.python-version }}-pip-${{ inputs.offset }}-

- name: Cache conda
uses: actions/cache@v3
if: runner.os == 'Linux'
with:
path: ~/conda_pkgs_dir
key: py${{ inputs.python-version }}-conda-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}
restore-keys: py${{ inputs.python-version }}-conda-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}
key: py${{ inputs.python-version }}-conda-${{ inputs.offset }}
restore-keys: py${{ inputs.python-version }}-conda-${{ inputs.offset }}
39 changes: 39 additions & 0 deletions .github/workflows/clear-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Clear cache flow

on:
workflow_call:
inputs:
pattern:
description: 'string to grep cache keys with'
required: true
type: string

jobs:
clear-cache:
runs-on: ubuntu-latest

steps:
- name: Get the list of cache keys
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt && cat cache_keys.txt || echo "no cache keys found with the pattern"

- name: Filter cache keys
run: |
grep -E ${{ inputs.pattern }} cache_keys.txt > cache_keys_to_remove.txt && cat cache_keys_to_remove.txt || echo "no cache keys found with the pattern"

- name: Remove caches
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo created_at last_accessed_at ref key size_in_bytes
for key in $(cat cache_keys_to_remove.txt); do
gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" --jq '.actions_caches[] | [.created_at, .last_accessed_at, .ref, .key, .size_in_bytes] | join(" ")'
done

- name: Show the list of remaining caches
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq
13 changes: 13 additions & 0 deletions .github/workflows/schedule-clear-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Clear cache weekly

on:
schedule:
# on Sundays
- cron: "0 0 * * 0"

jobs:
clear-cache:
if: ${{ github.repository_owner == 'Lightning-AI' }}
uses: ./.github/workflows/clear-cache.yml
with:
pattern: 'pip|conda'