From e2b60248cd556735ba0df521816efde95b5e5ba2 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Tue, 27 Feb 2024 13:59:38 +1100 Subject: [PATCH] CI: Actually use the cache dependencies Previously the GHAs would store the cache after building the dependencies but never restore them. Did a thorough investigation of the issue and added proper comments about how it works and why. --- .github/workflows/haskell.yml | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/.github/workflows/haskell.yml b/.github/workflows/haskell.yml index 100f223b50..edf9149eff 100644 --- a/.github/workflows/haskell.yml +++ b/.github/workflows/haskell.yml @@ -66,9 +66,12 @@ jobs: echo "# cabal.project.local" cat cabal.project.local + # A dry run `build all` operation does *NOT* downlaod anything, it just looks at the package + # indices to generate an install plan. - name: Build dry run run: cabal build all --enable-tests --dry-run --minimize-conflict-set + # From the install plan we generate a dependency list. - name: Record dependencies id: record-deps run: | @@ -77,17 +80,37 @@ jobs: ${{ (runner.os == 'Windows' && '$env:PATH=("C:\msys64\mingw64\bin;{0}" -f $env:PATH)') || '' }} cat dist-newstyle/cache/plan.json | jq -r '."install-plan"[].id' | sort | uniq > dependencies.txt + # From the dependency list we restore the cached dependencies. + # We use the hash of `dependencies.txt` as part of the cache key because that will be stable + # until the `index-state` values in the `cabal.project` file changes. + - name: Restore cached dependencies + uses: actions/cache/restore@v4 + id: cache + with: + path: | + ${{ steps.setup-haskell.outputs.cabal-store }} + dist-newstyle + key: cache-${{ env.CABAL_CACHE_VERSION }}-${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('dependencies.txt') }} + restore-keys: | + ${{ runner.os }}-ghc-${{ env.GHC_VERSION }}-cabal-${{ env.CABAL_VERSION }}- + cache-${{ env.CABAL_CACHE_VERSION }}-${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('dependencies.txt') }} + + # Now we install the dependencies. If the cache was found and restored in the previous step, + # this should be a no-op, but if the cache key was not found we need to build stuff so we can + # cache it for the next step. - name: Install dependencies run: cabal build all --enable-tests --only-dependencies -j --ghc-option=-j4 + # Always store the cabal cache. - name: Cache Cabal store - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: | ${{ steps.setup-haskell.outputs.cabal-store }} dist-newstyle key: cache-${{ env.CABAL_CACHE_VERSION }}-${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('dependencies.txt') }} + # Now we build. - name: Build [testing] run: cabal build all --enable-tests -j --ghc-option=-j4