Skip to content

Commit 402ba1c

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 54e9244 commit 402ba1c

File tree

3 files changed

+114
-33
lines changed

3 files changed

+114
-33
lines changed
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

+25-32
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
@@ -227,15 +225,14 @@ jobs:
227225
- uses: actions/setup-go@v5
228226
with:
229227
go-version: 1.22.x
230-
- id: path_for_hashFiles
231-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
232-
run: echo "NORMALIZED=$(realpath --relative-to=$PWD examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
233-
- uses: actions/cache@v4
234-
with:
235-
path: ~/.cache/lima/download
236-
# hashFiles do not seem to support symlinks
237-
# TODO: more fine-grained cache
238-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
228+
- name: normalize template path
229+
id: normalize_template_path
230+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
231+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
232+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
233+
uses: ./.github/actions/setup_cache_for_template
234+
with:
235+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
239236
- name: Make
240237
run: make
241238
- name: Install
@@ -328,12 +325,10 @@ jobs:
328325
- uses: actions/setup-go@v5
329326
with:
330327
go-version: 1.22.x
331-
- name: Cache ~/Library/Caches/lima/download
332-
uses: actions/cache@v4
328+
- name: Cache image used by vmnet.yaml
329+
uses: ./.github/actions/setup_cache_for_template
333330
with:
334-
path: ~/Library/Caches/lima/download
335-
# hashFiles do not seem to support symlinks
336-
key: ${{ runner.os }}-${{ hashFiles('examples/vmnet.yaml') }}
331+
template: templates/vmnet.yaml
337332
- name: Make
338333
run: make
339334
- name: Install
@@ -380,11 +375,10 @@ jobs:
380375
- uses: actions/setup-go@v5
381376
with:
382377
go-version: 1.22.x
383-
- name: Cache ~/Library/Caches/lima/download
384-
uses: actions/cache@v4
378+
- name: Cache image used by ${{ matrix.oldver }}/examples/ubuntu-lts.yaml
379+
uses: ./.github/actions/setup_cache_for_template
385380
with:
386-
path: ~/Library/Caches/lima/download
387-
key: ${{ runner.os }}-upgrade-${{ matrix.oldver }}
381+
template: https://raw.githubusercontent.com/lima-vm/lima/${{ matrix.oldver }}/examples/ubuntu-lts.yaml
388382
- name: Install test dependencies
389383
run: brew install qemu bash coreutils
390384
- name: Test
@@ -414,15 +408,14 @@ jobs:
414408
- uses: actions/setup-go@v5
415409
with:
416410
go-version: 1.22.x
417-
- id: path_for_hashFiles
418-
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
419-
run: echo "NORMALIZED=$(realpath examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
420-
- name: Cache ~/Library/Caches/lima/download
421-
uses: actions/cache@v4
422-
with:
423-
path: ~/Library/Caches/lima/download
424-
# hashFiles do not seem to support symlinks
425-
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
411+
- name: normalize template path
412+
id: normalize_template_path
413+
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
414+
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
415+
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
416+
uses: ./.github/actions/setup_cache_for_template
417+
with:
418+
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
426419
- name: Make
427420
run: make
428421
- 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)