Skip to content

Commit

Permalink
Minimal cache experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Jun 27, 2024
1 parent 9f17612 commit b8287de
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 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
7 changes: 6 additions & 1 deletion .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:
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 Down
33 changes: 24 additions & 9 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 @@ -50,7 +50,7 @@ runs:
id: cache-key
shell: bash
run: |
echo "key=alr[${{ steps.find-hash.outputs.version }}][${{ inputs.toolchain }}][${{ runner.os }}][${{ steps.find-hash.outputs.hash }}][c]" >> $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
Expand All @@ -61,21 +61,33 @@ runs:
path: |
~/.cache/alire
~/.config/alire
./alire_install
~/.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 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 @@ -181,17 +193,20 @@ runs:
~/.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: unused # Forced to provide it
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: true
fail-on-cache-miss: false

0 comments on commit b8287de

Please sign in to comment.