From abcb21c69a4f914fc533ea5bab1191325c69fd52 Mon Sep 17 00:00:00 2001 From: Andrei Dziahel Date: Wed, 22 Nov 2023 00:56:23 +0100 Subject: [PATCH] ci: cache Nix store with cache-nix-action (#2992) * ci: try nix-community/cache-nix-action * ci: add cache-id param to setup-nix action * ci: tidy up cache keys for non-nix jobs * ci: merge-nix-caches-linux job * ci: merge caches other way around * ci: reduce number of caches Should prevent disk overflow * ci: change cache id prefix for merge * ci: comment out cache merging job * ci: revert cache id prefix * ci: setup-nix: use latest cache-nix-action Among others, makes action logs look more tidy (see https://github.com/nix-community/cache-nix-action/commit/17d19d3d8be918757589635bc1a6830be0b129d2) * ci: use test-pg cache key for loadtest --- .github/actions/setup-nix/action.yaml | 14 ++++++++-- .github/workflows/ci.yaml | 39 ++++++++++++++++++++++++--- .github/workflows/loadtest.yaml | 2 ++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.github/actions/setup-nix/action.yaml b/.github/actions/setup-nix/action.yaml index b8f35b3ee..3f08d7b64 100644 --- a/.github/actions/setup-nix/action.yaml +++ b/.github/actions/setup-nix/action.yaml @@ -7,13 +7,23 @@ inputs: description: Token to pass to cachix tools: description: Tools to install with nix-env -iA + cache-id: + description: Cache id to use for cache-nix-action + default: "default" runs: using: composite steps: - - uses: cachix/install-nix-action@v23 + - uses: nixbuild/nix-quick-install-action@v26 with: - install_url: https://releases.nixos.org/nix/nix-2.13.3/install + nix_version: '2.13.6' + - name: Restore and cache Nix store + uses: nix-community/cache-nix-action@v4.0.3 + with: + key: cache-nix-${{ runner.os }}-id-${{ inputs.cache-id }}-${{ hashFiles('nix/**/*.nix') }} + restore-keys: | + cache-nix-${{ runner.os }}-common- + restore-key-hit: true - uses: cachix/cachix-action@v12 with: name: postgrest diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8c5915912..fa9d670cc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,6 +22,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: style + cache-id: style - name: Run linter (check locally with `nix-shell --run postgrest-lint`) run: postgrest-lint - name: Run style check (auto-format with `nix-shell --run postgrest-style`) @@ -42,6 +43,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: tests + cache-id: test-pg - name: Run coverage (IO tests and Spec tests against PostgreSQL 15) run: postgrest-coverage @@ -77,6 +79,9 @@ jobs: uses: ./.github/actions/setup-nix with: tools: tests withTools + # It seems like they are installing the same set of derivations, so we can assign them the same cache id. + # This would decrease the amount of caches dowloaded on merge cache step and will prevent disk space issues. + cache-id: test-pg - name: Run spec tests if: always() @@ -96,6 +101,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: memory + cache-id: test-memory - name: Run memory tests run: postgrest-test-memory @@ -109,6 +115,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: tests + cache-id: static-nix - name: Build static executable run: nix-build -A postgrestStatic @@ -130,6 +137,32 @@ jobs: path: postgrest-docker.tar.gz if-no-files-found: error + # TODO: Enable this again in a PR by PostgREST admins, because regular users don't have permission to delete cache entries, which this job does. + # + # merge-nix-caches-linux: + # name: "Merge Nix caches (Linux)" + # needs: [Test-Nix, Test-Pg-Nix, Test-Memory-Nix, Build-Static-Nix, Lint-Style] + # runs-on: ubuntu-latest + # strategy: + # max-parallel: 1 + # matrix: + # cache-id: ['static-nix', 'test-pg', 'style', 'test-memory'] + # steps: + # - uses: actions/checkout@v4 + # - uses: nixbuild/nix-quick-install-action@v26 + # with: + # nix_version: '2.13.6' + # - name: Restore and cache Nix store + # uses: nix-community/cache-nix-action@v4 + # with: + # key: cache-nix-${{ runner.os }}-common-${{ hashFiles('nix/**/*.nix') }} + # extra-restore-keys: | + # cache-nix-${{ runner.os }}-cid- + # purge: true + # purge-keys: | + # cache-nix-${{ runner.os }}-cid- + # cache-nix-${{ runner.os }}-common- + # purge-created-max-age: 0 Build-Macos-Nix: name: Build MacOS (Nix) @@ -180,7 +213,7 @@ jobs: uses: actions/cache@v3 with: path: ${{ matrix.cache }} - key: ${{ runner.os }}-${{ hashFiles('stack.yaml.lock') }} + key: cache-stack-${{ runner.os }}-${{ hashFiles('stack.yaml.lock') }} - name: Install dependencies if: ${{ matrix.deps }} run: ${{ matrix.deps }} @@ -236,9 +269,9 @@ jobs: uses: actions/cache@v3 with: path: ~/.cabal - key: ${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/cabal.project') }} + key: cache-cabal-${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/cabal.project') }} restore-keys: | - ${{ runner.os }}-${{ matrix.ghc }}- + cache-cabal-${{ runner.os }}-${{ matrix.ghc }}- - name: Install dependencies run: | cabal update diff --git a/.github/workflows/loadtest.yaml b/.github/workflows/loadtest.yaml index 6625d21a7..9e9f3c513 100644 --- a/.github/workflows/loadtest.yaml +++ b/.github/workflows/loadtest.yaml @@ -23,6 +23,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: loadtest + cache-id: test-pg - uses: actions-ecosystem/action-get-latest-tag@v1 id: get-latest-tag with: @@ -54,6 +55,7 @@ jobs: uses: ./.github/actions/setup-nix with: tools: loadtest + cache-id: test-pg - name: Run loadtest run: | postgrest-loadtest-against ${{ steps.get-latest-tag.outputs.tag }}