Skip to content

Commit

Permalink
feat: cache test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Oct 13, 2024
1 parent 9b61e79 commit 7b9b0d8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/testcache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Test cache

on:
pull_request:
workflow_dispatch:

jobs:
test-cache:
strategy:
fail-fast: false
matrix:
os:
- macos-13 # x64
- macos-latest # arm64
- ubuntu-latest
- windows-latest

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v4

# This might hit cache

- name: Check action itself (attempt 1)
id: attempt_1
uses: ./
with:
crates: gprbuild hello
prefix: alire_prefix

# We want to later on test the pristine prefix restored from cache
- name: Clean prefix for next attempt
if: steps.attempt_1.outputs.cache_hit != 'true'
shell: bash
run: rm -rf alire_prefix

# Next attemp should hit cache given the previous run

- name: Check action itself (attempt 2)
if: steps.attempt_1.outputs.cache_hit != 'true'
id: attempt_2
uses: ./
with:
crates: gprbuild hello
prefix: alire_prefix

- name: Diagnose cache use
shell: bash
run: |
echo "Caching attempt 1: ${{steps.attempt_1.outputs.cache_hit}}"
echo "Caching attempt 2: ${{steps.attempt_2.outputs.cache_hit}}"
# Fail if no cache was hit
- if: (steps.attempt_1.outputs.cache_hit != 'true') && (steps.attempt_2.outputs.cache_hit != 'true')
uses: actions/github-script@v3
with:
script: |
core.setFailed('FAIL: No cache hit observed')
- name: Run check
shell: bash
run: |
which gprbuild && \
gprbuild --version && \
hello | grep "Hello, world!"
45 changes: 42 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ inputs:
required: false
default: true

outputs:
cache_hit:
description: Whether the cache was hit
value: ${{ steps.cache-output.outputs.cache_hit }}

runs:
using: "composite"
steps:
Expand Down Expand Up @@ -75,7 +80,7 @@ runs:
id: cache-key
shell: bash
run: |
echo "key=[1][alr-install][alr=${{steps.alr-version.outputs.version}}][${{runner.os}}][${{runner.arch}}][${{inputs.prefix}}][${{steps.find-versions.outputs.milestones}}]" >> $GITHUB_OUTPUT
echo "key=[2][alr-install][alr=${{steps.alr-version.outputs.version}}][${{runner.os}}][${{runner.arch}}][${{inputs.prefix}}][${{steps.find-versions.outputs.milestones}}]" >> $GITHUB_OUTPUT
- name: Reuse cached installation
if: inputs.cache == 'true'
Expand All @@ -86,11 +91,22 @@ runs:
${{inputs.prefix}}
key: ${{steps.cache-key.outputs.key}}

# In case of miss, give an explicit 'false' which actions/cache doesn't provide
- name: Set cache_hit output
id: cache-output
shell: bash
run: |
if [[ "${{inputs.cache}}" == "true" && "${{steps.cache-install.outputs.cache-hit}}" == "true" ]]; then
echo "cache_hit=true" >> $GITHUB_OUTPUT
else
echo "cache_hit=false" >> $GITHUB_OUTPUT
fi
- name: Diagnose cache usage
shell: bash
run: |
echo "alr-install cache requested: ${{inputs.cache}}"
echo "alr-install cache hit: ${{steps.cache-install.outputs.cache-hit}}"
echo "alr-install cache hit: ${{steps.cache-output.outputs.cache_hit}}"
echo "alr-install cache key: ${{steps.cache-key.outputs.key}}"
- name: Run `alr install`
Expand Down Expand Up @@ -120,4 +136,27 @@ runs:
- name: Cleanup
if: inputs.clean == 'true' && steps.find-alr.outputs.available != 'true'
shell: bash
run: rm -rf alire_install
run: rm -rf alire_install

# Save cache early so we can verify its proper working in a test workflow. Otherwise
# 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 save
if: ${{ inputs.cache == 'true' && steps.cache-install.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v4
with:
path: |
${{inputs.prefix}}
key: ${{steps.cache-key.outputs.key}}

# Verify cache was saved properly
- name: Cache verify
if: ${{ inputs.cache == 'true' && steps.cache-install.outputs.cache-hit != 'true' }}
uses: actions/cache/restore@v4
with:
path: |
${{inputs.prefix}}
key: ${{steps.cache-key.outputs.key}}
lookup-only: true
fail-on-cache-miss: false
# Even if the cache is not found, we don't want to fail here for clients

0 comments on commit 7b9b0d8

Please sign in to comment.