|
| 1 | +name: 'Node.js Setup with Cache' |
| 2 | +description: 'Sets up Node.js with dependency caching for the initial setup job' |
| 3 | +author: 'miningos-devops' |
| 4 | + |
| 5 | +inputs: |
| 6 | + node-version: |
| 7 | + description: 'Node.js version to use' |
| 8 | + required: false |
| 9 | + default: '20.x' |
| 10 | + install-dependencies: |
| 11 | + description: 'Whether to install dependencies on cache miss' |
| 12 | + required: false |
| 13 | + default: 'true' |
| 14 | + additional-packages: |
| 15 | + description: 'Additional system packages to install (space-separated)' |
| 16 | + required: false |
| 17 | + default: '' |
| 18 | + |
| 19 | +outputs: |
| 20 | + cache-key: |
| 21 | + description: 'The generated cache key for node_modules' |
| 22 | + value: ${{ steps.cache-keys.outputs.cache-key }} |
| 23 | + cache-hit: |
| 24 | + description: 'Whether the cache was hit' |
| 25 | + value: ${{ steps.cache-node-modules.outputs.cache-hit }} |
| 26 | + |
| 27 | +runs: |
| 28 | + using: composite |
| 29 | + steps: |
| 30 | + - name: Generate cache keys |
| 31 | + id: cache-keys |
| 32 | + shell: bash |
| 33 | + run: | |
| 34 | + NODE_VERSION="${{ inputs.node-version }}" |
| 35 | + BASE_KEY="node-modules-${{ runner.os }}-${NODE_VERSION}" |
| 36 | + # Stable key per deps so downstream jobs and future runs get cache hits |
| 37 | + HASH_KEY="${BASE_KEY}-${{ hashFiles('package-lock.json', 'package.json') }}" |
| 38 | + echo "cache-key=${HASH_KEY}" >> $GITHUB_OUTPUT |
| 39 | + echo "base-key=${BASE_KEY}" >> $GITHUB_OUTPUT |
| 40 | +
|
| 41 | + - name: Setup Node.js |
| 42 | + uses: actions/setup-node@v6 |
| 43 | + with: |
| 44 | + node-version: ${{ inputs.node-version }} |
| 45 | + |
| 46 | + - name: Install additional system packages |
| 47 | + if: inputs.additional-packages != '' |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + echo "Installing additional packages: ${{ inputs.additional-packages }}" |
| 51 | + sudo apt update |
| 52 | + sudo apt install -y ${{ inputs.additional-packages }} |
| 53 | +
|
| 54 | + - name: Install dependencies |
| 55 | + if: inputs.install-dependencies == 'true' |
| 56 | + shell: bash |
| 57 | + run: | |
| 58 | + if [ -f package-lock.json ]; then |
| 59 | + echo "Found package-lock.json, using npm ci for deterministic installs" |
| 60 | + npm ci |
| 61 | + else |
| 62 | + echo "No package-lock.json found, using npm install" |
| 63 | + npm install |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Cache node modules |
| 67 | + id: cache-node-modules |
| 68 | + uses: actions/cache@v4 |
| 69 | + with: |
| 70 | + path: node_modules |
| 71 | + key: ${{ steps.cache-keys.outputs.cache-key }} |
| 72 | + restore-keys: | |
| 73 | + ${{ steps.cache-keys.outputs.base-key }}- |
0 commit comments