Skip to content

Commit ec43bfb

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 <norio.nomura@gmail.com>
1 parent 3075bd9 commit ec43bfb

File tree

3 files changed

+81
-27
lines changed

3 files changed

+81
-27
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 ] || mkdir -p .download
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

+20-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,14 @@ 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+
- name: normalize template path
227+
id: normalize_template_path
228+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
229+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
230+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
231+
uses: ./.github/actions/setup_cache_for_template
232232
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) }}
233+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
237234
- name: Make
238235
run: make
239236
- name: Install
@@ -322,12 +319,10 @@ jobs:
322319
- uses: actions/setup-go@v5
323320
with:
324321
go-version: 1.22.x
325-
- name: Cache ~/Library/Caches/lima/download
326-
uses: actions/cache@v4
322+
- name: Cache image used by vmnet.yaml
323+
uses: ./.github/actions/setup_cache_for_template
327324
with:
328-
path: ~/Library/Caches/lima/download
329-
# hashFiles do not seem to support symlinks
330-
key: ${{ runner.os }}-${{ hashFiles('examples/vmnet.yaml') }}
325+
template: templates/vmnet.yaml
331326
- name: Make
332327
run: make
333328
- name: Install
@@ -404,15 +399,14 @@ jobs:
404399
- uses: actions/setup-go@v5
405400
with:
406401
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
402+
- name: normalize template path
403+
id: normalize_template_path
404+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
405+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
406+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
407+
uses: ./.github/actions/setup_cache_for_template
412408
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) }}
409+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
416410
- name: Make
417411
run: make
418412
- name: Install

hack/debug-cache.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cache_dir="${HOME}/Library/Caches"
44
if [ "$(uname -s)" != "Darwin" ]; then
55
cache_dir="${HOME}/.cache"
66
fi
7-
if [ ! -e "${cache_dir}/lima" ]; then
7+
if [ ! -e "${cache_dir}/lima/download/by-url-sha256" ]; then
88
echo "No cache"
99
exit 0
1010
fi

0 commit comments

Comments
 (0)