|
| 1 | +name: Reusable Node.js Setup |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + node-version-file: |
| 7 | + description: 'Path to .nvmrc file' |
| 8 | + required: false |
| 9 | + default: '.nvmrc' |
| 10 | + type: string |
| 11 | + cache-key-prefix: |
| 12 | + description: 'Prefix for cache keys' |
| 13 | + required: false |
| 14 | + default: 'node_modules' |
| 15 | + type: string |
| 16 | + platform: |
| 17 | + description: 'Platform (linux, darwin, win32)' |
| 18 | + required: false |
| 19 | + default: 'linux' |
| 20 | + type: string |
| 21 | + arch: |
| 22 | + description: 'Architecture (x64, arm64)' |
| 23 | + required: false |
| 24 | + default: 'x64' |
| 25 | + type: string |
| 26 | + |
| 27 | +jobs: |
| 28 | + setup: |
| 29 | + name: Setup Node.js and Cache |
| 30 | + runs-on: ${{ inputs.platform == 'darwin' && 'macos-14' || inputs.platform == 'win32' && 'windows-latest' || 'ubuntu-latest' }} |
| 31 | + outputs: |
| 32 | + cache-hit: ${{ steps.cache-node-modules.outputs.cache-hit }} |
| 33 | + steps: |
| 34 | + - name: Checkout |
| 35 | + uses: actions/checkout@v5 |
| 36 | + |
| 37 | + - name: Setup Node.js |
| 38 | + uses: actions/setup-node@v6 |
| 39 | + with: |
| 40 | + node-version-file: ${{ inputs.node-version-file }} |
| 41 | + |
| 42 | + - name: Prepare node_modules cache key |
| 43 | + id: cache-key |
| 44 | + shell: bash |
| 45 | + run: | |
| 46 | + mkdir -p .build |
| 47 | + if [ -f "build/azure-pipelines/common/computeNodeModulesCacheKey.js" ]; then |
| 48 | + node build/azure-pipelines/common/computeNodeModulesCacheKey.js ${{ inputs.platform }} ${{ inputs.arch }} $(node -p process.arch) > .build/packagelockhash |
| 49 | + else |
| 50 | + echo "${{ hashFiles('package-lock.json') }}" > .build/packagelockhash |
| 51 | + fi |
| 52 | +
|
| 53 | + - name: Restore node_modules cache |
| 54 | + id: cache-node-modules |
| 55 | + uses: actions/cache/restore@v4 |
| 56 | + with: |
| 57 | + path: .build/node_modules_cache |
| 58 | + key: "${{ inputs.cache-key-prefix }}-${{ inputs.platform }}-${{ hashFiles('.build/packagelockhash') }}" |
| 59 | + restore-keys: | |
| 60 | + ${{ inputs.cache-key-prefix }}-${{ inputs.platform }}- |
| 61 | +
|
| 62 | + - name: Extract node_modules cache |
| 63 | + if: steps.cache-node-modules.outputs.cache-hit == 'true' |
| 64 | + shell: bash |
| 65 | + run: | |
| 66 | + if [ "${{ inputs.platform }}" = "win32" ]; then |
| 67 | + # Try 7z first, fallback to PowerShell Expand-Archive if 7z not available |
| 68 | + if command -v 7z.exe &> /dev/null; then |
| 69 | + 7z.exe x .build/node_modules_cache/cache.7z -aoa || true |
| 70 | + elif command -v powershell.exe &> /dev/null; then |
| 71 | + powershell.exe -Command "Expand-Archive -Path .build/node_modules_cache/cache.7z -DestinationPath . -Force" || true |
| 72 | + else |
| 73 | + echo "Warning: No extraction tool found for Windows. Skipping cache extraction." |
| 74 | + fi |
| 75 | + else |
| 76 | + tar -xzf .build/node_modules_cache/cache.tgz || true |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Install dependencies |
| 80 | + if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 81 | + shell: bash |
| 82 | + run: | |
| 83 | + if [ "${{ inputs.platform }}" = "win32" ]; then |
| 84 | + # Windows-specific setup if needed |
| 85 | + npm ci |
| 86 | + else |
| 87 | + npm ci |
| 88 | + fi |
| 89 | + env: |
| 90 | + ELECTRON_SKIP_BINARY_DOWNLOAD: 1 |
| 91 | + PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1 |
| 92 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 93 | + |
| 94 | + - name: Create node_modules archive |
| 95 | + if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 96 | + shell: bash |
| 97 | + run: | |
| 98 | + if [ "${{ inputs.platform }}" = "win32" ]; then |
| 99 | + if [ -f "build/azure-pipelines/common/listNodeModules.js" ]; then |
| 100 | + node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt |
| 101 | + mkdir -p .build/node_modules_cache |
| 102 | + # Try 7z first, fallback to PowerShell Compress-Archive if 7z not available |
| 103 | + if command -v 7z.exe &> /dev/null; then |
| 104 | + 7z.exe a .build/node_modules_cache/cache.7z -mx3 `@.build/node_modules_list.txt |
| 105 | + elif command -v powershell.exe &> /dev/null; then |
| 106 | + # PowerShell fallback - note: this creates a zip, not 7z |
| 107 | + powershell.exe -Command "Compress-Archive -Path (Get-Content .build/node_modules_list.txt) -DestinationPath .build/node_modules_cache/cache.zip -Force" || echo "Warning: Failed to create archive" |
| 108 | + else |
| 109 | + echo "Warning: No compression tool found for Windows. Skipping cache creation." |
| 110 | + fi |
| 111 | + fi |
| 112 | + else |
| 113 | + if [ -f "build/azure-pipelines/common/listNodeModules.js" ]; then |
| 114 | + node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt |
| 115 | + mkdir -p .build/node_modules_cache |
| 116 | + tar -czf .build/node_modules_cache/cache.tgz --files-from .build/node_modules_list.txt |
| 117 | + fi |
| 118 | + fi |
| 119 | +
|
| 120 | + - name: Save node_modules cache |
| 121 | + if: steps.cache-node-modules.outputs.cache-hit != 'true' |
| 122 | + uses: actions/cache/save@v4 |
| 123 | + with: |
| 124 | + path: .build/node_modules_cache |
| 125 | + key: "${{ inputs.cache-key-prefix }}-${{ inputs.platform }}-${{ hashFiles('.build/packagelockhash') }}" |
| 126 | + |
0 commit comments