From c4b0bd347b55cdc5a4f9077ca577d29e98e12e8f Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Tue, 6 Feb 2024 21:56:54 +0100 Subject: [PATCH] ci: Only save caches on main and release branches This restores caches on all branches and pull requests, but only stores them on the main branch and release branches. This prevents those caches from being evicted early when we hit the 10 GB limit quickly. --- .github/actions/cache-on-main/action.yaml | 31 +++++++++++++++++++++++ .github/workflows/build.yaml | 14 +++++----- 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 .github/actions/cache-on-main/action.yaml diff --git a/.github/actions/cache-on-main/action.yaml b/.github/actions/cache-on-main/action.yaml new file mode 100644 index 000000000..d0848bd88 --- /dev/null +++ b/.github/actions/cache-on-main/action.yaml @@ -0,0 +1,31 @@ +name: Cache on main + +description: Stores caches on main only, but restores them on all branches. + +inputs: + path: + description: Path(s) to cache + required: true + prefix: + description: Cache key prefix to be used in both primary key and restore-keys. + required: true + suffix: + description: Cache key suffix to be used only in primary key. + +runs: + using: composite + steps: + - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 + if: ${{ startsWith(github.ref, 'refs/heads/') }} + with: + path: ${{ inputs.path }} + key: ${{ runner.os }}-${{ inputs.prefix }}-${{ inputs.suffix }} + restore-keys: | + ${{ runner.os }}-${{ inputs.prefix }}- + - uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 + if: ${{ !startsWith(github.ref, 'refs/heads/') }} + with: + path: ${{ inputs.path }} + key: ${{ runner.os }}-${{ inputs.prefix }}-${{ inputs.suffix }} + restore-keys: | + ${{ runner.os }}-${{ inputs.prefix }}- diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1d8a494d2..43444a480 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -115,12 +115,11 @@ jobs: steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: Stack working files cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 + uses: ./.github/actions/cache-on-main with: path: ${{ matrix.cache }} - key: stack-${{ runner.os }}-${{ hashFiles('stack.yaml.lock') }} - restore-keys: | - stack-${{ runner.os }}- + prefix: stack + suffix: ${{ hashFiles('stack.yaml.lock') }} - name: Install dependencies if: matrix.deps run: ${{ matrix.deps }} @@ -171,15 +170,14 @@ jobs: run: | mkdir ~/.cabal - name: Cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 + uses: ./.github/actions/cache-on-main with: path: | ~/.cabal/packages ~/.cabal/store dist-newstyle - key: cabal-${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('postgrest.cabal', 'cabal.project', 'cabal.project.freeze') }} - restore-keys: | - cabal-${{ runner.os }}-${{ matrix.ghc }}- + prefix: cabal-${{ matrix.ghc }} + suffix: ${{ hashFiles('postgrest.cabal', 'cabal.project', 'cabal.project.freeze') }} - name: Install dependencies run: | cabal update