Interview Prep/Real-World Interview/Write-a-pipeline tasks

Write a GitHub Actions workflow that runs tests on every PR and push to main.

BeginnerHands-onWrite-a-pipeline tasks
Answer
name: ci
on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test -- --ci

Key trick → cache: npm in setup-node caches downloads keyed on the lockfile, and a top-level permissions: contents: read makes the GITHUB_TOKEN least-privilege.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-pipeline tasks