Skip to content

Commit 89ada8a

Browse files
authored
Improving cache management (#2068)
* Using setup-python cache option. Setting a workflow for cleaning up cache * Update .github/workflows/ci.yml
1 parent ced1e44 commit 89ada8a

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: cleanup caches by a branch
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
types:
6+
- closed
7+
8+
jobs:
9+
cleanup:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Check out code
13+
uses: actions/checkout@v3
14+
15+
- name: Cleanup PR caches
16+
if: github.event_name != 'workflow_dispatch'
17+
run: |
18+
gh extension install actions/gh-actions-cache
19+
20+
REPO=${{ github.repository }}
21+
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
22+
23+
echo "Fetching list of cache key"
24+
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )
25+
26+
## Setting this to not fail the workflow while deleting cache keys.
27+
set +e
28+
echo "Deleting caches..."
29+
for cacheKey in $cacheKeysForPR
30+
do
31+
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm && echo "Deleting cache with key: $cacheKey"
32+
done
33+
echo "Done"
34+
env:
35+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Cleanup by workflow dispatch
38+
if: github.event_name == 'workflow_dispatch'
39+
run: |
40+
gh extension install actions/gh-actions-cache
41+
42+
REPO=${{ github.repository }}
43+
44+
echo "Fetching list of cache key"
45+
cacheKeysForPR=$(gh actions-cache list -R $REPO | cut -f 1 )
46+
47+
## Setting this to not fail the workflow while deleting cache keys.
48+
set +e
49+
echo "Deleting caches..."
50+
for cacheKey in $cacheKeysForPR
51+
do
52+
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm && echo "Deleting cache with key: $cacheKey"
53+
done
54+
echo "Done"
55+
env:
56+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/ci.yml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ env:
3131
# You should go up in number, if you go down (or repeat a previous value)
3232
# you might end up reusing a previous cache if it haven't been deleted already.
3333
# It applies 7 days retention policy by default.
34-
RESET_PIP_CACHE: 0
3534
RESET_EXAMPLES_CACHE: 0
3635
RESET_DOC_BUILD_CACHE: 0
3736
RESET_AUTOSUMMARY_CACHE: 0
@@ -100,21 +99,14 @@ jobs:
10099
- name: "Setup Python"
101100
uses: actions/setup-python@v4
102101
with:
102+
cache: 'pip'
103103
python-version: ${{ env.MAIN_PYTHON_VERSION }}
104104

105105
- name: "Install OS packages"
106106
run: |
107107
sudo apt update
108108
sudo apt install zip pandoc libgl1-mesa-glx xvfb texlive-latex-extra latexmk graphviz
109109
110-
- name: "Cache pip"
111-
uses: actions/cache@v3
112-
with:
113-
path: ~/.cache/pip
114-
key: Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}-${{ hashFiles('pyproject.toml') }}
115-
restore-keys: |
116-
Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}
117-
118110
- name: "Test virtual framebuffer"
119111
run: |
120112
pip install -r .ci/requirements_test_xvfb.txt
@@ -288,21 +280,15 @@ jobs:
288280
- name: "Setup Python"
289281
uses: actions/setup-python@v4
290282
with:
283+
cache: 'pip'
284+
cache-dependency-path: pyproject.toml
291285
python-version: ${{ env.MAIN_PYTHON_VERSION }}
292286

293287
- name: "Install os packages"
294288
run: |
295289
sudo apt update
296290
sudo apt install libgl1-mesa-glx xvfb
297291
298-
- name: "Cache pip"
299-
uses: actions/cache@v3
300-
with:
301-
path: ~/.cache/pip
302-
key: Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}-${{ hashFiles('pyproject.toml') }}
303-
restore-keys: |
304-
Python-v${{ env.RESET_PIP_CACHE }}-Linux-${{ env.MAIN_PYTHON_VERSION }}
305-
306292
- name: "Test virtual framebuffer"
307293
run: |
308294
pip install -r .ci/requirements_test_xvfb.txt

0 commit comments

Comments
 (0)