.github/actions/node-ci/action.yml
2026-02-15 17:08:41 -08:00

82 lines
2.8 KiB
YAML

name: 'Node.js CI Setup'
description: 'Checkout repository, setup Node.js, cache node_modules, and install dependencies'
inputs:
node-version:
description: 'Node.js version to use'
required: false
default: '24.13.0'
checkout-path:
description: 'Path to checkout the repository'
required: false
default: '.'
working-directory:
description: 'Working directory for npm install (relative to checkout-path)'
required: true
flavor:
description: 'Build flavor (dev or prod)'
required: false
default: 'dev'
cache-prefix:
description: 'Prefix for cache key'
required: false
default: 'node'
checkout-submodules:
description: 'Whether to checkout submodules'
required: false
default: 'false'
checkout-ref:
description: 'Git ref to checkout (branch, tag, or SHA)'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Compute paths
id: paths
shell: bash
run: |
# Normalize working-directory: treat '.' or empty as root of checkout
if [[ "${{ inputs.working-directory }}" == "." || -z "${{ inputs.working-directory }}" ]]; then
echo "full-path=${{ inputs.checkout-path }}" >> $GITHUB_OUTPUT
else
echo "full-path=${{ inputs.checkout-path }}/${{ inputs.working-directory }}" >> $GITHUB_OUTPUT
fi
- name: Checkout
uses: actions/checkout@v4
with:
path: ${{ inputs.checkout-path }}
# When checkout-ref is empty, actions/checkout uses the default github.ref
ref: ${{ inputs.checkout-ref }}
submodules: ${{ inputs.checkout-submodules }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: '${{ steps.paths.outputs.full-path }}/package-lock.json'
- name: Cache node_modules
uses: actions/cache@v4
with:
path: ${{ steps.paths.outputs.full-path }}/node_modules
key: ${{ runner.os }}-${{ inputs.cache-prefix }}-${{ inputs.flavor }}-node-${{ inputs.node-version }}-${{ hashFiles(format('{0}/package-lock.json', steps.paths.outputs.full-path)) }}
restore-keys: |
${{ runner.os }}-${{ inputs.cache-prefix }}-${{ inputs.flavor }}-node-${{ inputs.node-version }}-
${{ runner.os }}-${{ inputs.cache-prefix }}-${{ inputs.flavor }}-
- name: Install dependencies (dev)
if: inputs.flavor == 'dev'
shell: bash
run: npm ci
working-directory: ${{ steps.paths.outputs.full-path }}
- name: Install dependencies (prod)
if: inputs.flavor == 'prod'
shell: bash
run: npm ci --omit=dev --omit=optional
working-directory: ${{ steps.paths.outputs.full-path }}