diff --git a/.github/workflows/cache-minimal.yml b/.github/workflows/cache-minimal.yml new file mode 100644 index 00000000..5fc0332f --- /dev/null +++ b/.github/workflows/cache-minimal.yml @@ -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 }}" diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml index 9bd1e0de..12e9218f 100644 --- a/.github/workflows/selftest.yml +++ b/.github/workflows/selftest.yml @@ -4,7 +4,10 @@ name: Selftest -on: pull_request +on: + pull_request: + # branches: + # - disabled jobs: setup-alire: diff --git a/.github/workflows/test-cache.yml b/.github/workflows/test-cache.yml index 7c791d18..044ae0ae 100644 --- a/.github/workflows/test-cache.yml +++ b/.github/workflows/test-cache.yml @@ -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: '' @@ -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: @@ -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 @@ -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 diff --git a/action.yml b/action.yml index 91305636..4a2180d4 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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 @@ -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