Skip to content

Commit

Permalink
Cache node_modules (#1158)
Browse files Browse the repository at this point in the history
* Cache node_modules

* Create init-node action with caching

* Fix

* Fix

* Run yarn install in init-all

* Use init-node from init-all

* Include the NodeJS version to the cache key
  • Loading branch information
whitphx authored Oct 14, 2024
1 parent 1c7fd67 commit 0b3f267
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
5 changes: 2 additions & 3 deletions .github/actions/init-all/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ runs:
**/uv.lock
## Set up Node environment
- uses: actions/setup-node@v4
- uses: ./.github/actions/init-node
with:
node-version-file: ${{ inputs.node-version-file }}
cache: 'yarn'
node-version-file: ${{ env.node-version-file }}

# We require protoc >= 3.20, but Ubuntu 22.04 - the OS that these Github
# Actions are running as of 2023.05.03 - doesn't have recent versions
Expand Down
38 changes: 38 additions & 0 deletions .github/actions/init-node/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: 'Node Initialization Action'
description: 'Node initialization steps for CI jobs'
inputs:
node-version-file:
description: 'Node version file to setup'
required: true
runs:
using: "composite"
steps:
## Set up Node environment
- uses: actions/setup-node@v4
with:
node-version-file: ${{ inputs.node-version-file }}
cache: 'yarn'

- name: Get Node.js version
id: node-version
run: echo "NODE_VERSION=$(node -v)" >> $GITHUB_ENV
shell: bash
# Include the NodeJS version in the cache key.
# While there are several reasons not to recommend caching `node_modules` (https://github.com/actions/setup-node/issues/304#issuecomment-886241508),
# NodeJS version incompatibility is one of them (https://github.com/actions/setup-node/issues/304#issuecomment-886240594).
# So invalidating the cache based on the NodeJS version is not perfect but
# it makes things safer.
# See also https://github.com/actions/setup-node/issues/406 and https://dev.to/flydiverny/comment/1lk25.

- uses: actions/cache@v4
with:
path: '**/node_modules'
key: node_modules-${{ env.NODE_VERSION }}-${{ hashFiles('yarn.lock') }}

# `yarn install` often takes so long time on in the Windows env.
# https://github.com/yarnpkg/yarn/issues/8242
- run: yarn config set network-timeout 600000
shell: bash

- run: yarn install --frozen-lockfile
shell: bash
44 changes: 21 additions & 23 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn install --frozen-lockfile

- name: Lint
run: |
yarn check:eslint
Expand Down Expand Up @@ -176,11 +176,12 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4


- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn install --frozen-lockfile

- name: Lint
run: |
yarn check:eslint
Expand All @@ -201,11 +202,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn install --frozen-lockfile

- run: make sharing-common
working-directory: .
# - name: Lint
Expand All @@ -228,11 +229,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn install --frozen-lockfile

- name: Lint
run: |
yarn check:eslint
Expand All @@ -258,12 +259,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn config set network-timeout 600000 # `yarn install` often takes so long time on in the Windows env.
- run: yarn install --frozen-lockfile

- run: make common
working-directory: .
- name: Lint
Expand Down Expand Up @@ -488,11 +488,10 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn install --frozen-lockfile

- name: Cache node_modules/.cache including Webpack's cache
uses: actions/cache@v4
Expand Down Expand Up @@ -695,12 +694,11 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4

- uses: ./.github/actions/init-node
with:
node-version-file: ${{ env.node-version-file }}
cache: 'yarn'
- run: yarn config set network-timeout 300000 # https://github.com/yarnpkg/yarn/issues/8242
- run: yarn install --frozen-lockfile

- run: yarn playwright install
- run: make common
- name: Lint
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ all: init mountable sharing sharing-editor


.PHONY: init
init: git_submodules venv node_modules
init: git_submodules venv yarn_install

VENV := ./.venv
NODE_MODULES := ./node_modules
Expand Down

0 comments on commit 0b3f267

Please sign in to comment.