Skip to content

Commit 75fb359

Browse files
twz123github-actions[bot]
authored andcommitted
Manually prune old entries from Go cache
The Go cache has fixed trim settings. It updates mtimes hourly and trims daily, deleting anything older than five days. This is too conservative for CI caches. We only want to retain caches used in the current run. We can achieve this by setting the mtime of all cache files to a time between one hour and five days ago. After the build, all used cache entries will have a newer mtime. Everything else can be trimmed. Signed-off-by: Tom Wieczorek <[email protected]> (cherry picked from commit 5d8587e)
1 parent a888f28 commit 75fb359

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

.github/workflows/build-k0s.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
pkg/assets/zz_generated_offsets_${{ inputs.target-os }}.go
7070
7171
- name: "Cache :: GOCACHE"
72+
id: cache-gocache
7273
uses: actions/cache@v4
7374
with:
7475
key: build-k0s-${{ inputs.target-os }}-${{ inputs.target-arch }}-gocache-${{ github.ref_name }}-${{ github.sha }}
@@ -77,6 +78,12 @@ jobs:
7778
path: |
7879
build/cache/go/build
7980
81+
- name: "Cache :: GOCACHE :: Prepare"
82+
if: steps.cache-gocache.outputs.cache-hit
83+
run: |
84+
touch -t "$(TZ=UTC+24 date +%Y%m%d%H%M.%S)" build/cache/_cache_sentinel
85+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -exec touch -r build/cache/_cache_sentinel {} +
86+
8087
- name: "Cache :: GOMODCACHE"
8188
uses: actions/cache@v4
8289
with:
@@ -119,3 +126,8 @@ jobs:
119126
with:
120127
name: ipv6-test-image-list-${{ inputs.target-os }}-${{ inputs.target-arch }}
121128
path: ipv6-test-images.txt
129+
130+
- name: "Cache :: GOCACHE :: Trim"
131+
if: steps.cache-gocache.outputs.cache-hit
132+
run: |
133+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -not -newer build/cache/_cache_sentinel -delete

.github/workflows/go.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ jobs:
209209
cache: false
210210

211211
- name: Cache GOCACHE
212+
id: cache-gocache
212213
uses: actions/cache@v4
213214
with:
214215
key: unittests-k0s-${{ matrix.name }}-gocache-${{ github.ref_name }}-${{ github.sha }}
@@ -225,7 +226,21 @@ jobs:
225226
enableCrossOsArchive: true
226227

227228
- name: Run unit tests
228-
run: make check-unit $UNITTEST_EXTRA_ARGS
229+
env:
230+
CACHE_HIT_GOCACHE: "${{ steps.cache-gocache.outputs.cache-hit }}"
231+
run: |
232+
if [ -n "$CACHE_HIT_GOCACHE" ]; then
233+
echo Preparing Go cache
234+
touch -t "$(TZ=UTC+24 date +%Y%m%d%H%M.%S)" build/cache/_cache_sentinel
235+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -exec touch -r build/cache/_cache_sentinel {} +
236+
fi
237+
238+
make check-unit $UNITTEST_EXTRA_ARGS
239+
240+
if [ -n "$CACHE_HIT_GOCACHE" ]; then
241+
echo Trimming Go cache
242+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -not -newer build/cache/_cache_sentinel -delete
243+
fi
229244
230245
smoketests:
231246
strategy:
@@ -317,6 +332,7 @@ jobs:
317332
embedded-bins/Makefile.variables
318333
319334
- name: Cache GOCACHE
335+
id: cache-gocache
320336
uses: actions/cache@v4
321337
with:
322338
key: ${{ runner.os }}-smoketest-arm-gocache-arm-${{ github.ref_name }}-${{ github.sha }}
@@ -325,6 +341,12 @@ jobs:
325341
path: |
326342
build/cache/go/build
327343
344+
- name: Cache GOCACHE - Prepare
345+
if: steps.cache-gocache.outputs.cache-hit
346+
run: |
347+
touch -t "$(TZ=UTC+24 date +%Y%m%d%H%M.%S)" build/cache/_cache_sentinel
348+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -exec touch -r build/cache/_cache_sentinel {} +
349+
328350
- name: Cache GOMODCACHE
329351
uses: actions/cache@v4
330352
with:
@@ -369,6 +391,11 @@ jobs:
369391
name: airgap-image-bundle-linux-arm.tar
370392
path: airgap-image-bundle-linux-arm.tar
371393

394+
- name: Cache GOCACHE - Trim
395+
if: steps.cache-gocache.outputs.cache-hit
396+
run: |
397+
find build/cache/go/build -type f \( -name '*-a' -o -name '*-d' \) -not -newer build/cache/_cache_sentinel -delete
398+
372399
# TODO We probably want to separate the smoketest into a separate callable workflow which we can call from the build step
373400
# This way we could actually fully parallelize the build and smoketest steps. Currently we are limited by the fact that
374401
# smoke-test step only start after both arm and armv7 builds have finished.

.github/workflows/lint.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
cache: false
6969

7070
- name: "Cache :: golangci-lint"
71+
id: cache-golangci
7172
uses: actions/cache@v4
7273
with:
7374
key: lint-${{ matrix.target-os }}-amd64-golangci-${{ github.ref_name }}-${{ github.sha }}
@@ -86,7 +87,21 @@ jobs:
8687
enableCrossOsArchive: true
8788

8889
- name: Run linter
89-
run: make lint-go $MAKE_EXTRA_ARGS
90+
env:
91+
CACHE_HIT_GOLANGCI: "${{ steps.cache-golangci.outputs.cache-hit }}"
92+
run: |
93+
if [ -n "$CACHE_HIT_GOLANGCI" ]; then
94+
echo Preparing golangci-lint cache
95+
touch -t "$(TZ=UTC+24 date +%Y%m%d%H%M.%S)" build/cache/_cache_sentinel
96+
find build/cache/go/build build/cache/golangci-lint -type f \( -name '*-a' -o -name '*-d' \) -exec touch -r build/cache/_cache_sentinel {} +
97+
fi
98+
99+
make lint-go $MAKE_EXTRA_ARGS
100+
101+
if [ -n "$CACHE_HIT_GOLANGCI" ]; then
102+
echo Trimming golangci-lint cache
103+
find build/cache/go/build build/cache/golangci-lint -type f \( -name '*-a' -o -name '*-d' \) -not -newer build/cache/_cache_sentinel -delete
104+
fi
90105
91106
lint-codegen:
92107
name: Lint generated code

0 commit comments

Comments
 (0)