From eb3eae2fccb33292f8233544977b0a4e4465d7fc Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 14 Dec 2021 09:40:16 +0000 Subject: [PATCH] DEV: Cache database in GitHub actions tests (#15279) A cached database (and its uploads) will only be used if the current run has exactly the same set of migration files. Otherwise, the database will be migrated from scratch This saves approximately 75s on the core backend specs and 45s on other runs. --- .github/workflows/tests.yml | 38 ++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7f52de106f5..c26056ece29 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -24,6 +24,7 @@ jobs: PGHOST: postgres PGUSER: discourse PGPASSWORD: discourse + USES_PARALLEL_DATABASES: ${{ matrix.build_type == 'backend' && matrix.target == 'core' }} strategy: fail-fast: false @@ -102,17 +103,48 @@ jobs: if: matrix.target == 'plugins' run: bin/rake plugin:install_all_official - - name: Create database + - name: Fetch app state cache + uses: actions/cache@v2 + id: app-cache + with: + path: tmp/app-cache + key: >- # postgres version, hash of migrations, "parallel?" + ${{ runner.os }}- + ${{ hashFiles('.github/workflows/tests.yml') }}- + ${{ matrix.postgres }}- + ${{ hashFiles('db/**/*', 'plugins/**/db/**/*') }}- + ${{ env.USES_PARALLEL_DATABASES }} + + - name: Restore database from cache + if: steps.app-cache.outputs.cache-hit == 'true' + run: psql -f tmp/app-cache/cache.sql postgres + + - name: Restore uploads from cache + if: steps.app-cache.outputs.cache-hit == 'true' + run: rm -rf public/uploads && cp -r tmp/app-cache/uploads public/uploads + + - name: Create and migrate database + if: steps.app-cache.outputs.cache-hit != 'true' run: | bin/rake db:create bin/rake db:migrate - - name: Create parallel databases - if: matrix.build_type == 'backend' && matrix.target == 'core' + - name: Create and migrate parallel databases + if: >- + env.USES_PARALLEL_DATABASES == 'true' && + steps.app-cache.outputs.cache-hit != 'true' run: | bin/rake parallel:create bin/rake parallel:migrate + - name: Dump database for cache + if: steps.app-cache.outputs.cache-hit != 'true' + run: mkdir -p tmp/app-cache && pg_dumpall > tmp/app-cache/cache.sql + + - name: Dump uploads for cache + if: steps.app-cache.outputs.cache-hit != 'true' + run: rm -rf tmp/app-cache/uploads && cp -r public/uploads tmp/app-cache/uploads + - name: Core RSpec if: matrix.build_type == 'backend' && matrix.target == 'core' run: bin/turbo_rspec --verbose