Skip to content

Commit b37addd

Browse files
committed
feat(github-actions): cache nvm installations between runs
This commit introduces a caching mechanism for NVM installations within the GitHub Actions workflow.
1 parent f27fc33 commit b37addd

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

github-actions/setup-wsl/action.yml

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ inputs:
1111
description: Networking mode for the WSL config
1212
default: NAT
1313
required: false
14-
cache_version:
15-
description: Version that can be used to invalidate the cache
16-
default: 2
17-
required: false
1814

1915
outputs:
2016
cmd_path:
@@ -71,7 +67,6 @@ runs:
7167
7268
- name: Determining paths for common WSL usage (e.g. path to cmd, npm, git)
7369
id: wsl_paths
74-
# Note: This executes outside of WSL.
7570
shell: bash
7671
run: |
7772
cmd_path=$(which cmd.exe)
@@ -83,6 +78,7 @@ runs:
8378
git_bin=$(which git)
8479
git_bin_wsl_path=$(wsl exec wslpath -u "$git_bin")
8580
81+
echo "wsl_home=$(wsl exec wslpath -u "$HOME")" >> $GITHUB_OUTPUT
8682
echo "cmd_path=$cmd_wsl_path" >> $GITHUB_OUTPUT
8783
echo "npm_path=$npm_wls_path" >> $GITHUB_OUTPUT
8884
echo "tmp_path=$tmp_dir_wsl_path" >> $GITHUB_OUTPUT
@@ -108,33 +104,32 @@ runs:
108104
echo "repo=$repo_path" >> $GITHUB_OUTPUT
109105
echo "Repo path is: $repo_path"
110106
111-
# Temporary disable the cache action because the post step takes up to 10mins in the CLI and also there are several errors:
112-
# "C:\Program Files\Git\usr\bin\tar.exe" --posix -cf cache.tzst --exclude cache.tzst -P -C C:/a/angular-cli/angular-cli --files-from manifest.txt --force-local --use-compress-program "zstd -T0"
113-
# /usr/bin/tar: ../../../wsl_root/root/.nvm/versions/node/v22.18.0/bin/corepack: Cannot stat: Input/output error
114-
# /usr/bin/tar: ../../../wsl_root/root/.nvm/versions/node/v22.18.0/bin/pnpx: Cannot stat: Input/output error
115-
# /usr/bin/tar: ../../../wsl_root/root/.nvm/versions/node/v22.18.0/bin/npm: Cannot stat: Input/output error
116-
# /usr/bin/tar: ../../../wsl_root/root/.nvm/versions/node/v22.18.0/bin/npx: Cannot stat: Input/output error
117-
# /usr/bin/tar: ../../../wsl_root/root/.nvm/versions/node/v22.18.0/bin/pnpm: Cannot stat: Input/output error
118-
# /usr/bin/tar: Exiting with failure status due to previous errors
119-
# Warning: Failed to save: "C:\Program failed with error: The process 'C:\Program Files\Git\usr\bin\tar.exe' failed with exit code 2
120-
121-
# - name: Cache for nvm, pnpm profile caches.
122-
# uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
123-
# with:
124-
# path: |
125-
# C:\\wsl_root\\root\\.nvm\\versions
126-
# C:\\wsl_root\\root\\.local\\share\\pnpm\\store\\
127-
# key: "${{ runner.os }}-wsl-cache-v${{inputs.cache_version}}-${{hashFiles('**/.nvmrc')}}-${{hashFiles('**/pnpm-lock.yaml')}}"
128-
# restore-keys: |
129-
# ${{ runner.os }}-wsl-cache-v${{inputs.cache_version}}-
130-
- name: Setup nvm
107+
- name: Restore WSL NVM cache
108+
id: cache-nvm
109+
uses: actions/cache@v4
110+
with:
111+
path: ~/.cache/wsl_nvm_cache.tar.gz
112+
key: wsl-nvm-cache-v1-${{hashFiles('**/.nvmrc')}}
113+
114+
- name: Extract NVM inside WSL
115+
if: steps.cache-nvm.outputs.cache-hit == 'true'
116+
shell: wsl-bash {0}
117+
run: |
118+
cache_archive="${{steps.wsl_paths.outputs.wsl_home}}/.cache/wsl_nvm_cache.tar.gz"
119+
mkdir -p ~/.nvm
120+
tar -xzf "$cache_archive" -C ~/.nvm
121+
122+
- name: Download NVM inside WSL
131123
shell: wsl-bash {0}
124+
if: steps.cache-nvm.outputs.cache-hit != 'true'
132125
run: |
133126
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
134127
export NVM_DIR="$HOME/.nvm"
135128
# Note: Specify `--install` due to https://github.com/nvm-sh/nvm/issues/1985.
136129
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --install
137-
- name: Installing Node and pnpm (in WSL)
130+
131+
- name: Setup NVM inside WSL
132+
shell: wsl-bash {0}
138133
run: |
139134
# Note: this is needed to load NVM in this step otherwise nvm will not be found.
140135
export NVM_DIR="$HOME/.nvm"
@@ -144,4 +139,12 @@ runs:
144139
145140
nvm install
146141
npm i -g pnpm@10
142+
143+
- name: Archive NVM inside WSL
147144
shell: wsl-bash {0}
145+
if: steps.cache-nvm.outputs.cache-hit != 'true'
146+
run: |
147+
wsl_home="${{steps.wsl_paths.outputs.wsl_home}}"
148+
mkdir -p "${wsl_home}/.cache"
149+
cache_archive="${wsl_home}/.cache/wsl_nvm_cache.tar.gz"
150+
tar -czf "$cache_archive" -C ~/.nvm .

0 commit comments

Comments
 (0)