From a4a98a09ec5ff771df44438debe2d77a8e9d074d Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Thu, 18 May 2023 18:03:09 +0200 Subject: [PATCH 1/2] ci/pr-deploy: cache node modules and gatsby output --- .github/workflows/pr-deploy.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/pr-deploy.yml b/.github/workflows/pr-deploy.yml index 270c5b2785..d20975948d 100644 --- a/.github/workflows/pr-deploy.yml +++ b/.github/workflows/pr-deploy.yml @@ -26,6 +26,7 @@ jobs: with: node-version: '16.x' registry-url: 'https://registry.npmjs.org' + cache: 'npm' - name: Restore cache uses: actions/cache@v2 with: @@ -45,6 +46,7 @@ jobs: with: node-version: '16.x' registry-url: 'https://registry.npmjs.org' + cache: 'npm' - name: Restore cache uses: actions/cache@v2 with: @@ -94,10 +96,22 @@ jobs: with: node-version: '16.x' registry-url: 'https://registry.npmjs.org' + cache: 'npm' - name: Install run: npm install + - name: Restore build cache + uses: actions/cache@v3 + with: + # Gatsby requires both .cache and public. It will refuse to use .cache/ if public/ is not present. + path: | + .cache + public + # Cache will not be used by Gatsby if the following files change: + # https://www.gatsbyjs.com/docs/build-caching/ + key: pr-gatsby-${{ hashFiles('package.json', 'gatsby-config.js', 'gatsby-node.js') }} + - name: Build run: npm run build From 6259aea126ca10736c64ea1fb97690eaf9aba883 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Thu, 18 May 2023 21:11:28 +0200 Subject: [PATCH 2/2] ci/pr-deploy: use git ref in cache key, but restore optimistically This will create different caches for each PR, and restore the most recent cache (that still matches gatsby config files) if there is not a cache entry for a particular PR. This allows to keep the cache fresh, as the cache is not re-uploaded if `key` hits, but _is_ reuploaded if restore-keys. As the most recent key is fetched from `restore-keys`, this ensures we keep refreshing the cache instead of using always the same one that could have been generated a long time ago. --- .github/workflows/pr-deploy.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-deploy.yml b/.github/workflows/pr-deploy.yml index d20975948d..c26f32b71e 100644 --- a/.github/workflows/pr-deploy.yml +++ b/.github/workflows/pr-deploy.yml @@ -110,7 +110,9 @@ jobs: public # Cache will not be used by Gatsby if the following files change: # https://www.gatsbyjs.com/docs/build-caching/ - key: pr-gatsby-${{ hashFiles('package.json', 'gatsby-config.js', 'gatsby-node.js') }} + key: pr-gatsby-${{ hashFiles('package.json', 'gatsby-config.js', 'gatsby-node.js') }}-${{ github.ref }} + restore-keys: | + pr-gatsby-${{ hashFiles('package.json', 'gatsby-config.js', 'gatsby-node.js') }}- - name: Build run: npm run build