跳到主要内容

Node.js CI

Complete continuous integration workflow for a Node.js project, including installation, build, testing, caching, and multi-version matrix.

name: Node.js CI

on:
push:
branches:
- main
- 'feature/**'
pull_request:
branches:
- main

concurrency:
max: 1
exceed-action: QUEUE

permissions:
repository: read

env:
NODE_VERSION_DEFAULT: '20'

jobs:
test:
name: Node.js ${{ matrix.node-version }} Test
runs-on: {ubuntu-24,x64,small}
strategy:
matrix:
node-version: [18, 20, 22]
fail-fast: false
max-parallel: 3

steps:
- name: Checkout code
uses: checkout

- name: Setup Node.js ${{ matrix.node-version }}
uses: setup-node
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Code style check
run: npm run lint

- name: Build
run: npm run build

- name: Unit test
run: npm test

- name: Integration test
run: npm run test:integration

- name: Test coverage report
if: ${{ always }}
run: |
echo "## Coverage Report - Node.js ${{ matrix.node-version }}" >> $ATOMGIT_STEP_SUMMARY
echo "| Metric | Value |" >> $ATOMGIT_STEP_SUMMARY
echo "|------|----|" >> $ATOMGIT_STEP_SUMMARY
npm run coverage:report >> $ATOMGIT_STEP_SUMMARY

build-and-package:
name: Build and package
needs: test
runs-on: {ubuntu-24,x64,medium}
if: ${{ atomgit.ref == 'refs/heads/main' && success }}

steps:
- name: Checkout code
uses: checkout

- name: Setup Node.js
uses: setup-node
with:
node-version: ${{ env.NODE_VERSION_DEFAULT }}
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build production package
run: npm run build:prod

- name: Upload artifact
uses: upload-artifact
with:
name: dist-package
path: dist/
retention-days: 7