GitHub Actions and CI/CD AutomationLesson 5.2
Writing CI Workflows: Lint and Test
CI workflow, checkout action, setup-node, npm test, matrix strategy, caching dependencies, status checks
Writing CI Workflows: Lint and Test
The most common use of GitHub Actions is Continuous Integration (CI) — automatically running tests and code quality checks on every push and pull request. This ensures that broken code never reaches the main branch.
A Node.js CI Workflow
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm testKey Actions Explained
actions/checkout@v4 clones your repository into the runner. actions/setup-node@v4 installs Node.js. The cache: 'npm' option caches the npm dependency cache between runs, dramatically speeding up subsequent workflow executions.
Matrix Strategy
Run tests on multiple Node.js versions simultaneously:
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}This creates three parallel jobs, one for each Node.js version.
