Skip to content

Commit 74f72e0

Browse files
authored
Engineering - add GitHub action to maintain node_modules cache (microsoft#254069)
1 parent 367fc5f commit 74f72e0

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed

.github/workflows/pr-node-modules.yml

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
name: Code OSS (node_modules)
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions: {}
9+
10+
jobs:
11+
linux:
12+
name: Linux
13+
runs-on: [ self-hosted, 1ES.Pool=1es-vscode-oss-ubuntu-22.04-x64 ]
14+
env:
15+
NPM_ARCH: x64
16+
VSCODE_ARCH: x64
17+
steps:
18+
- name: Checkout microsoft/vscode
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version-file: .nvmrc
25+
env:
26+
NODEJS_ORG_MIRROR: https://github.com/joaomoreno/node-mirror/releases/download
27+
28+
- name: Prepare node_modules cache key
29+
run: mkdir -p .build && node build/azure-pipelines/common/computeNodeModulesCacheKey.js linux $VSCODE_ARCH $(node -p process.arch) > .build/packagelockhash
30+
31+
- name: Restore node_modules cache
32+
id: cache-node-modules
33+
uses: actions/cache@v4
34+
with:
35+
path: .build/node_modules_cache
36+
key: "node_modules-${{ hashFiles('.build/packagelockhash') }}"
37+
38+
- name: Extract node_modules cache
39+
if: steps.cache-node-modules.outputs.cache-hit == 'true'
40+
run: tar -xzf .build/node_modules_cache/cache.tgz
41+
42+
- name: Install build dependencies
43+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
44+
working-directory: build
45+
run: |
46+
set -e
47+
48+
for i in {1..5}; do # try 5 times
49+
npm ci && break
50+
if [ $i -eq 5 ]; then
51+
echo "Npm install failed too many times" >&2
52+
exit 1
53+
fi
54+
echo "Npm install failed $i, trying again..."
55+
done
56+
env:
57+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58+
59+
- name: Install dependencies
60+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
61+
run: |
62+
set -e
63+
64+
source ./build/azure-pipelines/linux/setup-env.sh
65+
66+
for i in {1..5}; do # try 5 times
67+
npm ci && break
68+
if [ $i -eq 5 ]; then
69+
echo "Npm install failed too many times" >&2
70+
exit 1
71+
fi
72+
echo "Npm install failed $i, trying again..."
73+
done
74+
env:
75+
npm_config_arch: ${{ env.NPM_ARCH }}
76+
VSCODE_ARCH: ${{ env.VSCODE_ARCH }}
77+
ELECTRON_SKIP_BINARY_DOWNLOAD: 1
78+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80+
81+
- name: Create node_modules archive
82+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
83+
run: |
84+
set -e
85+
node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt
86+
mkdir -p .build/node_modules_cache
87+
tar -czf .build/node_modules_cache/cache.tgz --files-from .build/node_modules_list.txt
88+
89+
macOS:
90+
name: macOS
91+
runs-on: macos-14-xlarge
92+
env:
93+
NPM_ARCH: arm64
94+
VSCODE_ARCH: arm64
95+
steps:
96+
- name: Checkout microsoft/vscode
97+
uses: actions/checkout@v4
98+
99+
- name: Setup Node.js
100+
uses: actions/setup-node@v4
101+
with:
102+
node-version-file: .nvmrc
103+
env:
104+
NODEJS_ORG_MIRROR: https://github.com/joaomoreno/node-mirror/releases/download
105+
106+
- name: Prepare node_modules cache key
107+
run: mkdir -p .build && node build/azure-pipelines/common/computeNodeModulesCacheKey.js darwin $VSCODE_ARCH $(node -p process.arch) > .build/packagelockhash
108+
109+
- name: Restore node_modules cache
110+
id: cache-node-modules
111+
uses: actions/cache@v4
112+
with:
113+
path: .build/node_modules_cache
114+
key: "node_modules-${{ hashFiles('.build/packagelockhash') }}"
115+
116+
- name: Extract node_modules cache
117+
if: steps.cache-node-modules.outputs.cache-hit == 'true'
118+
run: tar -xzf .build/node_modules_cache/cache.tgz
119+
120+
- name: Install dependencies
121+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
122+
run: |
123+
set -e
124+
c++ --version
125+
xcode-select -print-path
126+
python3 -m pip install --break-system-packages setuptools
127+
128+
for i in {1..5}; do # try 5 times
129+
npm ci && break
130+
if [ $i -eq 5 ]; then
131+
echo "Npm install failed too many times" >&2
132+
exit 1
133+
fi
134+
echo "Npm install failed $i, trying again..."
135+
done
136+
env:
137+
npm_config_arch: ${{ env.NPM_ARCH }}
138+
VSCODE_ARCH: ${{ env.VSCODE_ARCH }}
139+
ELECTRON_SKIP_BINARY_DOWNLOAD: 1
140+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
141+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142+
# Avoid using dlopen to load Kerberos on macOS which can cause missing libraries
143+
# https://github.com/mongodb-js/kerberos/commit/04044d2814ad1d01e77f1ce87f26b03d86692cf2
144+
# flipped the default to support legacy linux distros which shouldn't happen
145+
# on macOS.
146+
GYP_DEFINES: "kerberos_use_rtld=false"
147+
148+
- name: Create node_modules archive
149+
if: steps.cache-node-modules.outputs.cache-hit != 'true'
150+
run: |
151+
set -e
152+
node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt
153+
mkdir -p .build/node_modules_cache
154+
tar -czf .build/node_modules_cache/cache.tgz --files-from .build/node_modules_list.txt
155+
156+
windows:
157+
name: Windows
158+
runs-on: [ self-hosted, 1ES.Pool=1es-vscode-oss-windows-2022-x64 ]
159+
env:
160+
NPM_ARCH: x64
161+
VSCODE_ARCH: x64
162+
steps:
163+
- name: Checkout microsoft/vscode
164+
uses: actions/checkout@v4
165+
166+
- name: Setup Node.js
167+
uses: actions/setup-node@v4
168+
with:
169+
node-version-file: .nvmrc
170+
env:
171+
NODEJS_ORG_MIRROR: https://github.com/joaomoreno/node-mirror/releases/download
172+
173+
- name: Prepare node_modules cache key
174+
shell: pwsh
175+
run: |
176+
mkdir .build -ea 0
177+
node build/azure-pipelines/common/computeNodeModulesCacheKey.js win32 ${{ env.VSCODE_ARCH }} $(node -p process.arch) > .build/packagelockhash
178+
179+
- name: Restore node_modules cache
180+
uses: actions/cache@v4
181+
id: node-modules-cache
182+
with:
183+
path: .build/node_modules_cache
184+
key: "node_modules-${{ hashFiles('.build/packagelockhash') }}"
185+
186+
- name: Extract node_modules cache
187+
if: steps.node-modules-cache.outputs.cache-hit == 'true'
188+
shell: pwsh
189+
run: 7z.exe x .build/node_modules_cache/cache.7z -aoa
190+
191+
- name: Install dependencies
192+
if: steps.node-modules-cache.outputs.cache-hit != 'true'
193+
shell: pwsh
194+
run: |
195+
. build/azure-pipelines/win32/exec.ps1
196+
$ErrorActionPreference = "Stop"
197+
198+
for ($i = 1; $i -le 5; $i++) {
199+
try {
200+
exec { npm ci }
201+
break
202+
}
203+
catch {
204+
if ($i -eq 5) {
205+
Write-Error "npm ci failed after 5 attempts"
206+
throw
207+
}
208+
Write-Host "npm ci failed attempt $i, retrying..."
209+
Start-Sleep -Seconds 2
210+
}
211+
}
212+
env:
213+
npm_config_arch: ${{ env.NPM_ARCH }}
214+
npm_config_foreground_scripts: "true"
215+
VSCODE_ARCH: ${{ env.VSCODE_ARCH }}
216+
ELECTRON_SKIP_BINARY_DOWNLOAD: 1
217+
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
218+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
219+
220+
- name: Create node_modules archive
221+
if: steps.node-modules-cache.outputs.cache-hit != 'true'
222+
shell: pwsh
223+
run: |
224+
. build/azure-pipelines/win32/exec.ps1
225+
$ErrorActionPreference = "Stop"
226+
exec { node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt }
227+
exec { mkdir -Force .build/node_modules_cache }
228+
exec { 7z.exe a .build/node_modules_cache/cache.7z -mx3 `@.build/node_modules_list.txt }

0 commit comments

Comments
 (0)