Skip to content

Add CI workflow with Node.js matrix testing and package manager detection #5

Add CI workflow with Node.js matrix testing and package manager detection

Add CI workflow with Node.js matrix testing and package manager detection #5

Workflow file for this run

# GitHub Actions workflow: CI - run tests for Node/React app
# Triggers on push to main/develop and on pull requests. Detects package manager (npm/yarn/pnpm),
# caches dependencies, installs, and runs tests once (non-watch).
name: CI - Tests
on:
push:
branches: [ "main", "develop" ]
pull_request:
branches: [ "main", "develop" ]
workflow_dispatch:
jobs:
test:
name: Test on Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
permissions:
contents: read
strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Detect package manager and lockfile
id: detect
shell: bash
run: |
# Default to npm if nothing is found
if [ -f pnpm-lock.yaml ]; then
echo "manager=pnpm" >> $GITHUB_OUTPUT
echo "lockfile=pnpm-lock.yaml" >> $GITHUB_OUTPUT
echo "install=pnpm install --frozen-lockfile" >> $GITHUB_OUTPUT
elif [ -f yarn.lock ]; then
echo "manager=yarn" >> $GITHUB_OUTPUT
echo "lockfile=yarn.lock" >> $GITHUB_OUTPUT
echo "install=yarn install --frozen-lockfile" >> $GITHUB_OUTPUT
elif [ -f package-lock.json ]; then
echo "manager=npm" >> $GITHUB_OUTPUT
echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
echo "install=npm ci" >> $GITHUB_OUTPUT
else
# Fallback to npm if no lockfile found
echo "manager=npm" >> $GITHUB_OUTPUT
echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT
echo "install=npm install" >> $GITHUB_OUTPUT
fi
- name: Setup Node.js and dependency cache
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: ${{ steps.detect.outputs.manager }}
cache-dependency-path: ${{ steps.detect.outputs.lockfile }}
- name: Install dependencies
shell: bash
run: |
echo "Using package manager: ${{ steps.detect.outputs.manager }}"
echo "Running install command: ${{ steps.detect.outputs.install }}"
eval "${{ steps.detect.outputs.install }}"
- name: Run tests
env:
CI: true
shell: bash
run: |
echo "Running test script using ${{ steps.detect.outputs.manager }}"
if [ "${{ steps.detect.outputs.manager }}" = "npm" ]; then
npm test
elif [ "${{ steps.detect.outputs.manager }}" = "yarn" ]; then
yarn test
else
pnpm test
fi