From b91a8a533a2237d7a6932c4a434cfb688860c399 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Tue, 28 Feb 2023 18:40:33 +0100 Subject: [PATCH] Timed expiration of npm cache Similar to what is added in https://github.com/actions/setup-node/pull/702, prevents the npm cache from growing indefinitely while still providing a general fallback --- .github/workflows/reusable-base.yml | 13 +++++++++---- .github/workflows/reusable-sync.yml | 13 +++++++++---- .github/workflows/type-check.yml | 13 +++++++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.github/workflows/reusable-base.yml b/.github/workflows/reusable-base.yml index c174765..2d516be 100644 --- a/.github/workflows/reusable-base.yml +++ b/.github/workflows/reusable-base.yml @@ -77,23 +77,28 @@ jobs: if: runner.os != 'Windows' run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT + # New prefix generated every 120 days, to ensure npm cache doesn't grow too large + - name: Get npm cache timed expiration + id: npm-timed-expiration + run: echo "prefix=$(( $(date +%s) / 60 / 60 / 24 / 120 ))" >> $GITHUB_OUTPUT + - name: Restore cache using package-lock.json hash uses: actions/cache@v3 if: ${{ !inputs.no-lockfile }} with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package-lock.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - name: Restore cache using package.json hash uses: actions/cache@v3 if: ${{ inputs.no-lockfile }} with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - run: npm ci --no-audit --no-fund if: ${{ !inputs.no-lockfile }} diff --git a/.github/workflows/reusable-sync.yml b/.github/workflows/reusable-sync.yml index a5453d0..64792b9 100644 --- a/.github/workflows/reusable-sync.yml +++ b/.github/workflows/reusable-sync.yml @@ -120,23 +120,28 @@ jobs: if: (inputs.npm-pre-sync-script || inputs.npm-post-sync-script) && runner.os != 'Windows' run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT + # New prefix generated every 120 days, to ensure npm cache doesn't grow too large + - name: Get npm cache timed expiration + id: npm-timed-expiration + run: echo "prefix=$(( $(date +%s) / 60 / 60 / 24 / 120 ))" >> $GITHUB_OUTPUT + - name: Restore cache using package-lock.json hash uses: actions/cache@v3 if: (inputs.npm-pre-sync-script || inputs.npm-post-sync-script) && !inputs.no-lockfile with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package-lock.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - name: Restore cache using package.json hash uses: actions/cache@v3 if: (inputs.npm-pre-sync-script || inputs.npm-post-sync-script) && inputs.no-lockfile with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - run: npm ci --no-audit --no-fund if: (inputs.npm-pre-sync-script || inputs.npm-post-sync-script) && !inputs.no-lockfile diff --git a/.github/workflows/type-check.yml b/.github/workflows/type-check.yml index c6ae162..bdcddbe 100644 --- a/.github/workflows/type-check.yml +++ b/.github/workflows/type-check.yml @@ -83,23 +83,28 @@ jobs: if: runner.os != 'Windows' run: echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT + # New prefix generated every 120 days, to ensure npm cache doesn't grow too large + - name: Get npm cache timed expiration + id: npm-timed-expiration + run: echo "prefix=$(( $(date +%s) / 60 / 60 / 24 / 120 ))" >> $GITHUB_OUTPUT + - name: Restore cache using package-lock.json hash uses: actions/cache@v3 if: ${{ !inputs.no-lockfile }} with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package-lock.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - name: Restore cache using package.json hash uses: actions/cache@v3 if: ${{ inputs.no-lockfile }} with: path: ${{ steps.npm-cache-dir-windows.outputs.dir || steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-${{ hashFiles('package.json') }} + key: ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}-${{ hashFiles('package.json') }} restore-keys: | - ${{ runner.os }}-node- + ${{ runner.os }}-node-${{ steps.npm-timed-expiration.outputs.prefix }}- - run: npm ci --no-audit --no-fund if: ${{ !inputs.no-lockfile }}