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.
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate