Getting started with CI: a simple GitHub Actions pipeline for Node.js

Continuous integration (CI) helps you catch bugs fast by building and testing every change. GitHub Actions makes CI easy because workflows live in your repository and run on GitHub-hosted runners. This short guide walks through a minimal, practical CI pipeline for a Node.js project: checkout, install, test, run across multiple Node versions, and cache dependencies to speed repeat runs.

What you’ll build

Where workflows live and the basics Workflow YAML files belong in .github/workflows in your repo. GitHub provides a Quickstart and in-depth workflow docs that explain triggers (on:), jobs, steps, and actions; these are the right references when you need specifics. (docs.github.com)

Example workflow (paste into .github/workflows/ci.yml)

name: CI

on:
  push:
    branches: [ main ]
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Use Node.js $
        uses: actions/setup-node@v6
        with:
          node-version: $
          cache: 'npm' # let setup-node handle npm cache when possible

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

Key steps explained

Using a matrix to test multiple Node versions A matrix strategy runs the job in parallel across the Node versions you list. This is a common, compact way to confirm your package works across supported runtimes without duplicating workflow YAML. Matrix usage is a standard practice documented in the workflows guide. (docs.github.com)

Caching dependencies to speed builds Caching the npm cache or other dependency files reduces runtime by avoiding repeated downloads. Use actions/cache (or setup-node’s built-in cache features) to store the appropriate directories and key them to the lockfile hash so a new cache is created whenever your dependencies change. A typical cache key uses hashFiles(‘**/package-lock.json’) and includes the runner OS and Node version to avoid wrong-cache collisions. (docs.github.com)

A few practical notes and tips

Troubleshooting

Wrap-up This minimal CI pipeline gives you reliable builds and tests on every push and PR, with simple extensions available: add linting steps, test coverage reporting, or artifacts as needed. The GitHub Actions docs and maintained actions (checkout, setup-node, cache) are the right places to check for latest configuration patterns and security notes. (docs.github.com)