Interview Prep/CI/CD/GitHub Actions

A repo needs CI on every PR: install, lint, test for Node 20 and 22. Write the workflow.

BeginnerScenarioGitHub Actions
Answer

Situation read → Standard PR validation with a matrix.

What I'd do →

name: ci
on:
  pull_request:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: [20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm test

Why → npm ci is reproducible and cache: npm caches the download cache keyed by the lockfile.

Interview tip → Always set permissions explicitly to the minimum.

All CI/CD questions

More on GitHub Actions