Node.js CI workflow (NaturalNode/natural)
The Node.js CI workflow from NaturalNode/natural, explained and optimized by Latchkey.
CI health: F - at risk
Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the Node.js CI workflow from the NaturalNode/natural repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
# This workflow will do a clean install of node dependencies,
# build the source code and run tests across different versions of node
# For more information see:
# https://help.github.com/actions/language-and-framework-guides/
# using-nodejs-with-github-actions
---
name: Node.js CI
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
services:
redis:
image: redis:latest
ports:
- 6379:6379
postgres:
image: postgres:latest
env:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: naturaldb
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_TABLE: naturaltable
ports:
- 5432:5432
memcached:
image: memcached:latest
ports:
- 11211:11211
mongodb:
image: mongo:latest
ports:
- 27017:27017
strategy:
matrix:
node-version: [20.x, 22.x, 24.x]
# See supported Node.js release schedule at
#https://nodejs.org/en/about/releases/
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Clean install
run: npm ci
- name: Lint
run: npm run lint
- name: Build
run: npm run build
- name: ESM smoke test
run: npm run smoke:esm
- name: Run Istanbul coverage
run: npm run coverage
# Save coverage report in Coveralls
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
The same workflow, on Latchkey
Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.
# This workflow will do a clean install of node dependencies, # build the source code and run tests across different versions of node # For more information see: # https://help.github.com/actions/language-and-framework-guides/ # using-nodejs-with-github-actions --- name: Node.js CI on: push: branches: [master] pull_request: branches: [master] concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: build: timeout-minutes: 30 runs-on: latchkey-small services: redis: image: redis:latest ports: - 6379:6379 postgres: image: postgres:latest env: POSTGRES_USER: user POSTGRES_PASSWORD: password POSTGRES_DB: naturaldb POSTGRES_HOST: localhost POSTGRES_PORT: 5432 POSTGRES_TABLE: naturaltable ports: - 5432:5432 memcached: image: memcached:latest ports: - 11211:11211 mongodb: image: mongo:latest ports: - 27017:27017 strategy: matrix: node-version: [20.x, 22.x, 24.x] # See supported Node.js release schedule at #https://nodejs.org/en/about/releases/ steps: - name: Checkout code uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: cache: 'npm' node-version: ${{ matrix.node-version }} - name: Clean install run: npm ci - name: Lint run: npm run lint - name: Build run: npm run build - name: ESM smoke test run: npm run smoke:esm - name: Run Istanbul coverage run: npm run coverage # Save coverage report in Coveralls - name: Coveralls uses: coverallsapp/github-action@master with: github-token: ${{ secrets.GITHUB_TOKEN }}
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Cache dependency installs on the setup step so they are served from cache.
- Add a job timeout so a hung step cannot burn hours of runner time.
1 third-party action is referenced by a movable tag. Pin it to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Dependency installs
This workflow runs 1 job (3 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.