Skip to content

Commit

Permalink
Fix missing path in cache (#69)
Browse files Browse the repository at this point in the history
* Fix missing path in cache

* Minimal cache experiment
  • Loading branch information
mosteo authored Jun 27, 2024
1 parent f4c4b88 commit 57e1c1a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 17 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/cache-minimal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Test minimal save/restore of a cache

name: Minimal cache test

on:
push:
branches:
- disabled

jobs:
minimal-cache:
runs-on: ubuntu-latest

steps:
- name: Create fake data
run: |
mkdir data1
mkdir data2
touch data1/thedata.txt
touch data2/thedata.txt
- name: Create unique key
id: cache-key
run: |
echo "key=cache test $(date -u)" >> $GITHUB_OUTPUT
- name: Verify cache miss
id: miss
uses: actions/cache/restore@v4
with:
path: |
data1
data2
key: ${{ steps.cache-key.outputs.key }}
lookup-only: true

- name: Save cache
uses: actions/cache/save@v4
with:
path: |
data1
data2
key: ${{ steps.cache-key.outputs.key }}

- name: Query cache hit 1
id: hit1
uses: actions/cache/restore@v4
with:
path: |
data1
data2
key: ${{ steps.cache-key.outputs.key }}
fail-on-cache-miss: false
lookup-only: true

# NOTE inverted path order
- name: Query cache hit 2
id: hit2
uses: actions/cache/restore@v4
with:
path: |
data2
data1
key: ${{ steps.cache-key.outputs.key }}
fail-on-cache-miss: false
lookup-only: true

- name: Print both cache hit and miss
run: |
echo "Cache miss? ${{ steps.miss.outputs.cache-hit }}"
echo "Cache hit 1? ${{ steps.hit1.outputs.cache-hit }}"
echo "Cache hit 2? ${{ steps.hit2.outputs.cache-hit }}"
5 changes: 4 additions & 1 deletion .github/workflows/selftest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

name: Selftest

on: pull_request
on:
pull_request:
# branches:
# - disabled

jobs:
setup-alire:
Expand Down
14 changes: 9 additions & 5 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ name: Test cache

on:
pull_request:
# branches:
# - disabled

jobs:
sanity-check:
test-cache:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os:
- macos-latest
- ubuntu-latest
- windows-latest
config:
- version: '2.0.1'
branch: ''
Expand All @@ -35,7 +40,7 @@ jobs:
# This might hit cache

- name: Check action itself
- name: Check action itself (attempt 1)
id: attempt_1
uses: alire-project/setup-alire@v3-next
with:
Expand All @@ -44,7 +49,7 @@ jobs:

# Next attemp should hit cache given the previous run

- name: Check action itself
- name: Check action itself (attempt 2)
if: steps.attempt_1.outputs.cache_hit != 'true'
id: attempt_2
uses: alire-project/setup-alire@v3-next
Expand Down Expand Up @@ -72,7 +77,6 @@ jobs:
alr exec -- gprbuild --version
# Verify proper builds

- run: alr -n version | grep "os:" | grep LINUX
if: matrix.os == 'ubuntu-latest'
shell: bash
Expand Down
51 changes: 40 additions & 11 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ inputs:
outputs:
cache_hit:
description: Whether a cached installation was reused
value: ${{ steps.cache-alr.outputs.cache-hit }}
value: ${{ steps.cache-output.outputs.cache_hit }}

runs:
using: "composite"
Expand All @@ -46,34 +46,48 @@ runs:
echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
fi
- name: Print cache key
- name: Generate cache key
id: cache-key
shell: bash
run: |
echo "key=alr[${{ steps.find-hash.outputs.version }}][${{ inputs.toolchain }}][${{ runner.os }}][${{ steps.find-hash.outputs.hash }}]" >> $GITHUB_OUTPUT
echo "key=alr[${{ steps.find-hash.outputs.version }}][${{ inputs.toolchain }}][${{ runner.os }}][${{ steps.find-hash.outputs.hash }}][f]" >> $GITHUB_OUTPUT
# The last value in square brackets is to make the key unique for debugging

- name: Reuse cached installation
if: ${{ inputs.cache == 'true' && inputs.toolchain_dir == '' }}
if: ${{ inputs.cache == 'true' }}
id: cache-alr
uses: actions/cache/restore@v3
uses: actions/cache/restore@v4
with:
path: |
~/.cache/alire
~/.config/alire
~/.local/share/alire
./alire_install
~/AppData/Local/alire
key: ${{steps.cache-key.outputs.key}}
# .cache contains msys64 install on Windows
# .config contains the toolchain at the default location, besides index config
# ./alire_install contains alr itself
# ~/AppData is used with Alire 2.0 onwards
# ~/AppData and ./local/share/alire is used with Alire 2.0 onwards
# Note that '~' is recommended on Windows too: https://github.com/actions/cache
# THESE MUST BE EXACTLY THE SAME IN SAME ORDER IN ALL CACHE-RELATED STEPS
key: ${{steps.cache-key.outputs.key}}

- name: Check cache output
shell: bash
run: |
echo Cache hit result: [${{steps.cache-alr.outputs.cache-hit}}] cache-key: ${{steps.cache-key.outputs.key}}
# In case of miss, give an explicit 'false' which actions/cache doesn't provide
- name: Set cache_hit to false if needed
id: cache-output
shell: bash
run: |
if [[ "${{inputs.cache}}" == "true" && "${{steps.cache-alr.outputs.cache-hit}}" == "true" ]]; then
echo "cache_hit=true" >> $GITHUB_OUTPUT
else
echo "cache_hit=false" >> $GITHUB_OUTPUT
fi
# Ascertain if we need to install a toolchain for building from sources
- name: Find GNAT
shell: bash
Expand Down Expand Up @@ -170,14 +184,29 @@ runs:
# it's not saved until workflow completion and by then it's too late.
# When cache was hit, attempting to save will fail and emit a warning, so avoid it.
- name: Cache install
if: ${{ inputs.cache == 'true' && inputs.toolchain_dir == '' && steps.cache-alr.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v3
if: ${{ inputs.cache == 'true' && steps.cache-alr.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v4
with:
path: |
~/.cache/alire
~/.config/alire
~/.local/share/alire
./alire_install
~/AppData/Local/alire
key: ${{ steps.cache-alr.outputs.cache-primary-key }}
# Note that '~' is recommended on Windows too: https://github.com/actions/cache
key: ${{ steps.cache-key.outputs.key }}

# Verify cache was saved properly
- name: Cache verify
if: ${{ inputs.cache == 'true' && steps.cache-alr.outputs.cache-hit != 'true' }}
uses: actions/cache/restore@v4
with:
path: |
~/.cache/alire
~/.config/alire
~/.local/share/alire
./alire_install
~/AppData/Local/alire
key: ${{steps.cache-key.outputs.key}}
lookup-only: true
fail-on-cache-miss: false

0 comments on commit 57e1c1a

Please sign in to comment.