Skip to content

Commit 8fb5b8a

Browse files
Optimizing build cache step (#1395)
While looking for ways to optimize/shave off time from our builds, I was brought back to [this ticket](https://app.asana.com/1/90955849329269/inbox/1211294560690016/item/1212009520590921/story/1212029899178754). This caching step didn't make any sense to me so I delved into it. Essentially, this step was creating a cache key whose name was a mix between a hash of our package-lock file and a hash of all of our (70k+) mdx files (which would take 2-3 min to process). This key would be used to pull up a cached version of the content we have in ~/.npm and /.next/cache during the github runner process. The content in those folders are just downloaded packages, compliled js/ts files, and other things for nextjs optimization. If a single mdx file changed, it would just invalidate the cache (even though the cache was totally good to use), and cause us to run npm install again, so we never really had the proper benefit of this cache in the first place. This PR simplifies and narrows the scope of the files we actually care about caching the changes for. Included changes in both the build-preview process and the deploy-udr step.
2 parents 24bdf40 + a77c3b9 commit 8fb5b8a

File tree

10 files changed

+23
-39
lines changed

10 files changed

+23
-39
lines changed

.github/workflows/algolia-prod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
cache: 'npm'
2323
node-version-file: 'package.json'
2424

25-
- run: npm i
25+
- run: npm ci
2626
- run: npm run prebuild -- --get-real-file-changed-metadata --build-algolia-index
2727
- run: npm run algolia
2828
env:

.github/workflows/build-pr-preview.yml

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: Build Preview
22
run-name: 'Build Preview for "${{ github.event.pull_request.title }}" (#${{ github.event.pull_request.number }})'
33

44
on:
5+
# 1: Change this to `pull_request` to see changes while testing this file in a PR.
6+
# 2: This is because `pull_request_target` only works from the context of the base branch
7+
# https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target
8+
# 3: Change this back to `pull_request_target` before merge for security reasons
59
pull_request_target:
610
types: [opened, synchronize]
711
# Hello Security 👋, we are checking to make sure forked repo PR changed paths are only in content/** inside the job security-check.
@@ -105,20 +109,11 @@ jobs:
105109
cache: 'npm'
106110
node-version-file: 'package.json'
107111

108-
- name: Generate cache manifest
109-
run: git ls-files -z -- '*.js' '*.mjs' '*.ts' '*.tsx' '*.mdx' '*.json' | xargs -0 git hash-object > cache-key.txt
110-
111-
- name: Use cache
112+
- name: Cache Next.js build
112113
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
113114
with:
114-
path: |
115-
~/.npm
116-
${{ github.workspace }}/.next/cache
117-
# Generate a new cache whenever packages or source files change.
118-
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('cache-key.txt') }}
119-
# If source files changed but packages didn't, rebuild from a prior cache.
120-
restore-keys: |
121-
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
115+
path: ${{ github.workspace }}/.next/cache
116+
key: ${{ runner.os }}-nextjs-build-${{ hashFiles('package-lock.json', 'next.config.js') }}
122117

123118
- name: Install Vercel CLI
124119
run: npm i --global vercel@latest
@@ -187,17 +182,6 @@ jobs:
187182
with:
188183
repository: hashicorp/dev-portal
189184
path: ./unified-docs-frontend-preview
190-
- name: Use cache
191-
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
192-
with:
193-
path: |
194-
~/.npm
195-
${{ github.workspace }}/unified-docs-frontend-preview/.next/cache
196-
# Generate a new cache whenever packages or source files change.
197-
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx', '**/*.mdx') }}
198-
# If source files changed but packages didn't, rebuild from a prior cache.
199-
restore-keys: |
200-
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
201185

202186
- name: Setup Node.js
203187
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
@@ -206,6 +190,12 @@ jobs:
206190
cache-dependency-path: 'unified-docs-frontend-preview/package-lock.json'
207191
node-version-file: 'unified-docs-frontend-preview/package.json'
208192

193+
- name: Cache Next.js build
194+
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
195+
with:
196+
path: ${{ github.workspace }}/unified-docs-frontend-preview/.next/cache
197+
key: ${{ runner.os }}-nextjs-build-devportal-${{ hashFiles('unified-docs-frontend-preview/package-lock.json', 'unified-docs-frontend-preview/next.config.js') }}
198+
209199
- name: Setup Vercel CLI
210200
run: npm i --global vercel@latest
211201

.github/workflows/copy-cloud-docs-for-tfe.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Generate version-metadata for workflow
5353
working-directory: '${{github.workspace}}/new-docs-pr'
5454
run: |
55-
npm i
55+
npm ci
5656
npm run prebuild -- --only-build-version-metadata
5757
5858
- name: Create the new TFE version folder for RELEASE PR

.github/workflows/deploy-udr.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,11 @@ jobs:
2727
cache: 'npm'
2828
node-version-file: 'package.json'
2929

30-
- name: Use cache
30+
- name: Cache Next.js build
3131
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
3232
with:
33-
path: |
34-
~/.npm
35-
${{ github.workspace }}/.next/cache
36-
# Generate a new cache whenever packages or source files change.
37-
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.mjs', '**/*.ts', '**/*.tsx', '**/*.mdx') }}
38-
# If source files changed but packages didn't, rebuild from a prior cache.
39-
restore-keys: |
40-
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
33+
path: ${{ github.workspace }}/.next/cache
34+
key: ${{ runner.os }}-nextjs-build-${{ hashFiles('package-lock.json', 'next.config.js') }}
4135

4236
- name: Install Vercel CLI
4337
run: npm install --global vercel@latest

.github/workflows/sync-docs-for-tfe.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
- name: Generate version-metadata for workflow
8484
working-directory: '${{github.workspace}}/new-docs'
8585
run: |
86-
npm i
86+
npm ci
8787
npm run prebuild -- --only-build-version-metadata
8888
8989
- name: Checkout HCPTF-diff for new docs version DIFF PR

.github/workflows/test-and-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
cache: 'npm'
2929
node-version-file: 'package.json'
3030

31-
- run: npm i
31+
- run: npm ci
3232
- run: npm run test
3333

3434
lint:
@@ -46,7 +46,7 @@ jobs:
4646
node-version-file: 'package.json'
4747

4848
- name: Install Dependencies
49-
run: npm i
49+
run: npm ci
5050

5151
- name: Run lint check
5252
run: npm run lint
-1.01 KB
Binary file not shown.
-267 Bytes
Binary file not shown.
113 Bytes
Binary file not shown.

scripts/utils/batch-promises.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export async function batchPromises(
2020
batchName,
2121
arrayToBatch,
2222
asyncMapFn,
23-
{ batchSize = 16, loggingEnabled = true } = {
24-
batchSize: 16,
23+
{ batchSize = 64, loggingEnabled = true } = {
24+
batchSize: 64,
2525
loggingEnabled: true,
2626
},
2727
) {

0 commit comments

Comments
 (0)