Skip to content

Commit d839954

Browse files
committed
test.yml: use a digest of image in template as key for cache
This should reduce the cache size used by the CI. Signed-off-by: Norio Nomura <[email protected]>
1 parent 3075bd9 commit d839954

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: 'setup cache for template'
2+
description: 'setup cache for template'
3+
inputs:
4+
arch:
5+
description: arch to setup cache for
6+
required: false
7+
template:
8+
description: template yaml file
9+
required: true
10+
runs:
11+
using: "composite"
12+
steps:
13+
- name: "detect platform for download directory"
14+
id: detect-platform
15+
run: |
16+
if [[ "$(uname)" == "Darwin" ]]; then
17+
download_dir=~/Library/Caches/lima/download
18+
else
19+
download_dir=~/.cache/lima/download
20+
fi
21+
echo "download-dir=$download_dir" >> "$GITHUB_OUTPUT"
22+
shell: bash
23+
- name: "extract key from template"
24+
if: always()
25+
id: extract-key-from-template
26+
run: |
27+
set -eux
28+
arch="${{ inputs.arch }}"
29+
template="${{ inputs.template }}"
30+
test -f "$template" || exit 1
31+
# detect arch from template if not provided
32+
arch="${arch:-$(yq '.arch // ""' "$template")}"
33+
arch="${arch:-$(uname -m)}"
34+
# normalize arch. amd64 -> x86_64, arm64 -> aarch64
35+
case "$arch" in
36+
amd64) arch=x86_64 ;;
37+
arm64) arch=aarch64 ;;
38+
esac
39+
# extract digest from template using arch
40+
digest="$(yq ".images | map(select(.arch == \"$arch\")) | .[0].digest // \"\"" "$template")"
41+
# fallback to os and hash of template file if digest not found
42+
key="${digest:+image-$digest}"
43+
key="${key:-${{ runner.os }}-${{ hashFiles(inputs.template) }}}"
44+
echo "key=$key" >> "$GITHUB_OUTPUT"
45+
shell: bash
46+
- name: "Cache .download"
47+
# avoid using `~` in path that will be expanded to platform specific home directory
48+
uses: actions/cache@v4
49+
with:
50+
path: .download
51+
key: ${{ steps.extract-key-from-template.outputs.key }}
52+
enableCrossOsArchive: true
53+
- name: "Create symbolic link named ${{ steps.detect-platform.outputs.download-dir }} pointing to .download"
54+
run: |
55+
set -eux
56+
[ -d .download/by-url-sha256 ] || mkdir -p .download/by-url-sha256
57+
path_to_cache=${{ steps.detect-platform.outputs.download-dir }}
58+
mkdir -p $(dirname $path_to_cache)
59+
ln -sfn $PWD/.download $path_to_cache
60+
shell: bash

.github/workflows/test.yml

+18-26
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,10 @@ jobs:
157157
- uses: actions/setup-go@v5
158158
with:
159159
go-version: 1.22.x
160-
- name: Cache ~/Library/Caches/lima/download
161-
uses: actions/cache@v4
160+
- name: Cache image used by default.yaml
161+
uses: ./.github/actions/setup_cache_for_template
162162
with:
163-
path: ~/Library/Caches/lima/download
164-
# hashFiles do not seem to support symlinks
165-
key: ${{ runner.os }}-${{ hashFiles('templates/default.yaml') }}
163+
template: templates/default.yaml
166164
- name: Unit tests
167165
run: go test -v ./...
168166
- name: Make
@@ -225,15 +223,13 @@ jobs:
225223
- uses: actions/setup-go@v5
226224
with:
227225
go-version: 1.22.x
228-
- id: path_for_hashFiles
229-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
230-
run: echo "NORMALIZED=$(realpath --relative-to=$PWD examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
231-
- uses: actions/cache@v4
226+
- id: normalize_template_path
227+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
228+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
229+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
230+
uses: ./.github/actions/setup_cache_for_template
232231
with:
233-
path: ~/.cache/lima/download
234-
# hashFiles do not seem to support symlinks
235-
# TODO: more fine-grained cache
236-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
232+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
237233
- name: Make
238234
run: make
239235
- name: Install
@@ -322,12 +318,10 @@ jobs:
322318
- uses: actions/setup-go@v5
323319
with:
324320
go-version: 1.22.x
325-
- name: Cache ~/Library/Caches/lima/download
326-
uses: actions/cache@v4
321+
- name: Cache image used by vmnet.yaml
322+
uses: ./.github/actions/setup_cache_for_template
327323
with:
328-
path: ~/Library/Caches/lima/download
329-
# hashFiles do not seem to support symlinks
330-
key: ${{ runner.os }}-${{ hashFiles('examples/vmnet.yaml') }}
324+
template: templates/vmnet.yaml
331325
- name: Make
332326
run: make
333327
- name: Install
@@ -404,15 +398,13 @@ jobs:
404398
- uses: actions/setup-go@v5
405399
with:
406400
go-version: 1.22.x
407-
- id: path_for_hashFiles
408-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
409-
run: echo "NORMALIZED=$(realpath examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
410-
- name: Cache ~/Library/Caches/lima/download
411-
uses: actions/cache@v4
401+
- id: normalize_template_path
402+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
403+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
404+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
405+
uses: ./.github/actions/setup_cache_for_template
412406
with:
413-
path: ~/Library/Caches/lima/download
414-
# hashFiles do not seem to support symlinks
415-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
407+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
416408
- name: Make
417409
run: make
418410
- name: Install

0 commit comments

Comments
 (0)