Skip to content

Commit cd30aaa

Browse files
committed
test.yml: use image cache with parameters created from template
Use `actions/cache@v4` with following params: path: ".download/by-url-sha256/$(echo $location | sha256sum | cut -d' ' -f1)" key: "image-$digest" enableCrossOsArchive: true To make the image cache cross-platform, the cache directory is specified using a relative path from the working directory, and the platform-specific cache directories are accessed via symbolic links. This should reduce the cache size used by the CI. Signed-off-by: Norio Nomura <[email protected]> setup_cache_for_template: support url in template parameter Signed-off-by: Norio Nomura <[email protected]>
1 parent d7669be commit cd30aaa

File tree

3 files changed

+114
-33
lines changed

3 files changed

+114
-33
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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: "create cache parameters from template"
24+
if: always()
25+
id: cache-params-from-template
26+
run: |
27+
set -eux
28+
arch="${{ inputs.arch }}"
29+
template="${{ inputs.template }}"
30+
case "$template" in
31+
https://*)
32+
tmp_yaml=$(mktemp -d)/template.yaml
33+
curl -sSLf "$template" > $tmp_yaml || exit 1
34+
template=$tmp_yaml
35+
;;
36+
*)
37+
test -f "$template" || exit 1
38+
;;
39+
esac
40+
41+
# detect arch from template if not provided
42+
arch="${arch:-$(yq '.arch // ""' "$template")}"
43+
arch="${arch:-$(uname -m)}"
44+
# normalize arch. amd64 -> x86_64, arm64 -> aarch64
45+
case "$arch" in
46+
amd64) arch=x86_64 ;;
47+
arm64) arch=aarch64 ;;
48+
esac
49+
50+
# extract digest and location from template using arch
51+
digest="$(yq ".images | map(select(.arch == \"$arch\")) | .[0].digest // \"\"" "$template")"
52+
location="$(yq ".images | map(select(.arch == \"$arch\")) | .[0].location // \"\"" "$template")"
53+
test -n "$location" || exit 1
54+
55+
# path to cache
56+
if command -v sha256sum > /dev/null; then
57+
sha256="$(echo -n "$location" | sha256sum | cut -d' ' -f1)"
58+
elif command -v shasum > /dev/null; then
59+
sha256="$(echo -n "$location" | shasum -a 256 | cut -d' ' -f1)"
60+
else
61+
echo "sha256sum or shasum not found" >&2
62+
exit 1
63+
fi
64+
echo "path=.download/by-url-sha256/$sha256" >> "$GITHUB_OUTPUT"
65+
66+
# key for cache
67+
key="${digest:+image-$digest}"
68+
# fallback to os and hash of template file if digest not found
69+
key="${key:-${{ runner.os }}-${{ hashFiles(inputs.template) }}}"
70+
echo "key=$key" >> "$GITHUB_OUTPUT"
71+
shell: bash
72+
73+
- name: "Cache ${{ steps.cache-params-from-template.outputs.path }}"
74+
# avoid using `~` in path that will be expanded to platform specific home directory
75+
uses: actions/cache@v4
76+
with:
77+
path: ${{ steps.cache-params-from-template.outputs.path }}
78+
key: ${{ steps.cache-params-from-template.outputs.key }}
79+
enableCrossOsArchive: true
80+
81+
- name: "Create symbolic link named ${{ steps.detect-platform.outputs.download-dir }} pointing to .download"
82+
run: |
83+
set -eux
84+
[ -d .download ] || mkdir -p .download
85+
path_to_cache=${{ steps.detect-platform.outputs.download-dir }}
86+
mkdir -p $(dirname $path_to_cache)
87+
ln -sfn $PWD/.download $path_to_cache
88+
shell: bash

.github/workflows/test.yml

Lines changed: 25 additions & 32 deletions
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.23.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
@@ -230,15 +228,14 @@ jobs:
230228
- uses: actions/setup-go@v5
231229
with:
232230
go-version: 1.23.x
233-
- id: path_for_hashFiles
234-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
235-
run: echo "NORMALIZED=$(realpath --relative-to=$PWD examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
236-
- uses: actions/cache@v4
237-
with:
238-
path: ~/.cache/lima/download
239-
# hashFiles do not seem to support symlinks
240-
# TODO: more fine-grained cache
241-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
231+
- name: normalize template path
232+
id: normalize_template_path
233+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
234+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
235+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
236+
uses: ./.github/actions/setup_cache_for_template
237+
with:
238+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
242239
- name: Make
243240
run: make
244241
- name: Install
@@ -331,12 +328,10 @@ jobs:
331328
- uses: actions/setup-go@v5
332329
with:
333330
go-version: 1.23.x
334-
- name: Cache ~/Library/Caches/lima/download
335-
uses: actions/cache@v4
331+
- name: Cache image used by vmnet.yaml
332+
uses: ./.github/actions/setup_cache_for_template
336333
with:
337-
path: ~/Library/Caches/lima/download
338-
# hashFiles do not seem to support symlinks
339-
key: ${{ runner.os }}-${{ hashFiles('examples/vmnet.yaml') }}
334+
template: templates/vmnet.yaml
340335
- name: Make
341336
run: make
342337
- name: Install
@@ -386,11 +381,10 @@ jobs:
386381
- uses: actions/setup-go@v5
387382
with:
388383
go-version: 1.23.x
389-
- name: Cache ~/Library/Caches/lima/download
390-
uses: actions/cache@v4
384+
- name: Cache image used by ${{ matrix.oldver }}/examples/ubuntu-lts.yaml
385+
uses: ./.github/actions/setup_cache_for_template
391386
with:
392-
path: ~/Library/Caches/lima/download
393-
key: ${{ runner.os }}-upgrade-${{ matrix.oldver }}
387+
template: https://raw.githubusercontent.com/lima-vm/lima/${{ matrix.oldver }}/examples/ubuntu-lts.yaml
394388
- name: Install test dependencies
395389
run: brew install qemu bash coreutils
396390
- name: Test
@@ -420,15 +414,14 @@ jobs:
420414
- uses: actions/setup-go@v5
421415
with:
422416
go-version: 1.23.x
423-
- id: path_for_hashFiles
424-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
425-
run: echo "NORMALIZED=$(realpath examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
426-
- name: Cache ~/Library/Caches/lima/download
427-
uses: actions/cache@v4
428-
with:
429-
path: ~/Library/Caches/lima/download
430-
# hashFiles do not seem to support symlinks
431-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
417+
- name: normalize template path
418+
id: normalize_template_path
419+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
420+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
421+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
422+
uses: ./.github/actions/setup_cache_for_template
423+
with:
424+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
432425
- name: Make
433426
run: make
434427
- name: Install

hack/debug-cache.sh

Lines changed: 1 addition & 1 deletion
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)