Skip to content

Commit

Permalink
test.yml: use a digest of image in template as key for cache
Browse files Browse the repository at this point in the history
This should reduce the cache size used by the CI.

Signed-off-by: Norio Nomura <[email protected]>
  • Loading branch information
norio-nomura committed Jul 24, 2024
1 parent 3075bd9 commit ec43bfb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
60 changes: 60 additions & 0 deletions .github/actions/setup_cache_for_template/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: 'setup cache for template'
description: 'setup cache for template'
inputs:
arch:
description: arch to setup cache for
required: false
template:
description: template yaml file
required: true
runs:
using: "composite"
steps:
- name: "detect platform for download directory"
id: detect-platform
run: |
if [[ "$(uname)" == "Darwin" ]]; then
download_dir=~/Library/Caches/lima/download
else
download_dir=~/.cache/lima/download
fi
echo "download-dir=$download_dir" >> "$GITHUB_OUTPUT"
shell: bash
- name: "extract key from template"
if: always()
id: extract-key-from-template
run: |
set -eux
arch="${{ inputs.arch }}"
template="${{ inputs.template }}"
test -f "$template" || exit 1
# detect arch from template if not provided
arch="${arch:-$(yq '.arch // ""' "$template")}"
arch="${arch:-$(uname -m)}"
# normalize arch. amd64 -> x86_64, arm64 -> aarch64
case "$arch" in
amd64) arch=x86_64 ;;
arm64) arch=aarch64 ;;
esac
# extract digest from template using arch
digest="$(yq ".images | map(select(.arch == \"$arch\")) | .[0].digest // \"\"" "$template")"
# fallback to os and hash of template file if digest not found
key="${digest:+image-$digest}"
key="${key:-${{ runner.os }}-${{ hashFiles(inputs.template) }}}"
echo "key=$key" >> "$GITHUB_OUTPUT"
shell: bash
- name: "Cache .download"
# avoid using `~` in path that will be expanded to platform specific home directory
uses: actions/cache@v4
with:
path: .download
key: ${{ steps.extract-key-from-template.outputs.key }}
enableCrossOsArchive: true
- name: "Create symbolic link named ${{ steps.detect-platform.outputs.download-dir }} pointing to .download"
run: |
set -eux
[ -d .download ] || mkdir -p .download
path_to_cache=${{ steps.detect-platform.outputs.download-dir }}
mkdir -p $(dirname $path_to_cache)
ln -sfn $PWD/.download $path_to_cache
shell: bash
46 changes: 20 additions & 26 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Cache ~/Library/Caches/lima/download
uses: actions/cache@v4
- name: Cache image used by default.yaml
uses: ./.github/actions/setup_cache_for_template
with:
path: ~/Library/Caches/lima/download
# hashFiles do not seem to support symlinks
key: ${{ runner.os }}-${{ hashFiles('templates/default.yaml') }}
template: templates/default.yaml
- name: Unit tests
run: go test -v ./...
- name: Make
Expand Down Expand Up @@ -225,15 +223,14 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: 1.22.x
- id: path_for_hashFiles
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
run: echo "NORMALIZED=$(realpath --relative-to=$PWD examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
- uses: actions/cache@v4
- name: normalize template path
id: normalize_template_path
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
uses: ./.github/actions/setup_cache_for_template
with:
path: ~/.cache/lima/download
# hashFiles do not seem to support symlinks
# TODO: more fine-grained cache
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
- name: Make
run: make
- name: Install
Expand Down Expand Up @@ -322,12 +319,10 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: 1.22.x
- name: Cache ~/Library/Caches/lima/download
uses: actions/cache@v4
- name: Cache image used by vmnet.yaml
uses: ./.github/actions/setup_cache_for_template
with:
path: ~/Library/Caches/lima/download
# hashFiles do not seem to support symlinks
key: ${{ runner.os }}-${{ hashFiles('examples/vmnet.yaml') }}
template: templates/vmnet.yaml
- name: Make
run: make
- name: Install
Expand Down Expand Up @@ -404,15 +399,14 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: 1.22.x
- id: path_for_hashFiles
# It seems that `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
run: echo "NORMALIZED=$(realpath examples/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
- name: Cache ~/Library/Caches/lima/download
uses: actions/cache@v4
- name: normalize template path
id: normalize_template_path
# `hashFiles` cannot use `..` as a path component, so generate a normalized path here.
run: echo "NORMALIZED=$(realpath templates/${{ matrix.template }})" >> "$GITHUB_OUTPUT"
- name: Cache image used by ${{ steps.normalize_template_path.outputs.NORMALIZED }}
uses: ./.github/actions/setup_cache_for_template
with:
path: ~/Library/Caches/lima/download
# hashFiles do not seem to support symlinks
key: ${{ runner.os }}-${{ hashFiles(steps.path_for_hashFiles.outputs.NORMALIZED) }}
template: ${{ steps.normalize_template_path.outputs.NORMALIZED }}
- name: Make
run: make
- name: Install
Expand Down
2 changes: 1 addition & 1 deletion hack/debug-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cache_dir="${HOME}/Library/Caches"
if [ "$(uname -s)" != "Darwin" ]; then
cache_dir="${HOME}/.cache"
fi
if [ ! -e "${cache_dir}/lima" ]; then
if [ ! -e "${cache_dir}/lima/download/by-url-sha256" ]; then
echo "No cache"
exit 0
fi
Expand Down

0 comments on commit ec43bfb

Please sign in to comment.