From 279febe26b0df49439a74cca2b68941ea17545df Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sun, 12 May 2024 12:57:02 +0200 Subject: [PATCH] ci: Split cabal and stack work caches from regular cache This is a first step to split up the cabal and stack caches in separate pieces. Here we split the work folder, which just contains the postgrest-specific build artifacts, into a separate cache. More fine-grained caching should give us better cache hits and much fewer upload size in the regular case, improving CI performance. Since the work file caches are very small (about 30-40 MB) they are cached for PRs, too. This will allow the majority of PRs, which only change source code files, but no dependencies, to still have cached their build files for additional commits. --- .cirrus.yml | 16 +++++++++++++-- .github/actions/cache-on-main/action.yaml | 10 +++++++--- .github/workflows/build.yaml | 24 ++++++++++++++++------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index f42645212..193eb897f 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -19,8 +19,20 @@ build_task: stack_cache: folders: /.stack - fingerprint_script: cat postgrest.cabal stack.yaml.lock - reupload_on_changes: false + fingerprint_script: + - echo $CIRRUS_OS + - stack --version + - md5sum postgrest.cabal + - md5sum stack.yaml.lock + + stack_work_cache: + folders: .stack-work + fingerprint_script: + - echo $CIRRUS_OS + - stack --version + - md5sum postgrest.cabal + - md5sum stack.yaml.lock + - find main src -type f -iname '*.hs' -exec md5sum "{}" + build_script: | stack build -j 1 --local-bin-path . --copy-bins --stack-yaml stack-21.7.yaml diff --git a/.github/actions/cache-on-main/action.yaml b/.github/actions/cache-on-main/action.yaml index d0848bd88..a7dc0a94e 100644 --- a/.github/actions/cache-on-main/action.yaml +++ b/.github/actions/cache-on-main/action.yaml @@ -1,29 +1,33 @@ name: Cache on main -description: Stores caches on main only, but restores them on all branches. +description: Stores caches on main and release branches only, but restores them on all branches. inputs: path: description: Path(s) to cache required: true + save-prs: + description: Whether to additionally store the cache in a pull request, too. Should only be used for very small caches. + default: false 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. + required: true runs: using: composite steps: - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2 - if: ${{ startsWith(github.ref, 'refs/heads/') }} + if: ${{ startsWith(github.ref, 'refs/heads/') || (inputs.save-prs && github.head_ref) }} 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/') }} + if: ${{ !startsWith(github.ref, 'refs/heads/') && !(inputs.save-prs && github.head_ref) }} with: path: ${{ inputs.path }} key: ${{ runner.os }}-${{ inputs.prefix }}-${{ inputs.suffix }} diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 43444a480..ece2caa41 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -91,14 +91,12 @@ jobs: runs-on: ubuntu-22.04 cache: | ~/.stack - .stack-work # no artifact for Linux, because we use the static build - name: MacOS runs-on: macos-12 cache: | ~/.stack - .stack-work artifact: postgrest-macos-x64 - name: Windows @@ -106,7 +104,6 @@ jobs: cache: | ~\AppData\Roaming\stack ~\AppData\Local\Programs\stack - .stack-work deps: Add-Content $env:GITHUB_PATH $env:PGBIN artifact: postgrest-windows-x64 @@ -114,12 +111,19 @@ jobs: runs-on: ${{ matrix.runs-on }} steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - - name: Stack working files cache + - name: Cache ~/.stack uses: ./.github/actions/cache-on-main with: path: ${{ matrix.cache }} prefix: stack - suffix: ${{ hashFiles('stack.yaml.lock') }} + suffix: ${{ hashFiles('postgrest.cabal', 'stack.yaml.lock') }} + - name: Cache .stack-work + uses: ./.github/actions/cache-on-main + with: + path: .stack-work + save-prs: true + prefix: stack-work-${{ hashFiles('postgrest.cabal', 'stack.yaml.lock') }} + suffix: ${{ hashFiles('main/**/*.hs', 'src/**/*.hs') }} - name: Install dependencies if: matrix.deps run: ${{ matrix.deps }} @@ -169,15 +173,21 @@ jobs: - name: Fix caching run: | mkdir ~/.cabal - - name: Cache + - name: Cache .cabal uses: ./.github/actions/cache-on-main with: path: | ~/.cabal/packages ~/.cabal/store - dist-newstyle prefix: cabal-${{ matrix.ghc }} suffix: ${{ hashFiles('postgrest.cabal', 'cabal.project', 'cabal.project.freeze') }} + - name: Cache dist-newstyle + uses: ./.github/actions/cache-on-main + with: + path: dist-newstyle + save-prs: true + prefix: cabal-${{ matrix.ghc }}-dist-newstyle-${{ hashFiles('postgrest.cabal', 'cabal.project', 'cabal.project.freeze') }} + suffix: ${{ hashFiles('**/*.hs') }} - name: Install dependencies run: | cabal update