GitHub Actions: Jobs, Steps, Matrix, and Reusable Building Blocks

Jobs, steps, matrix builds, conditional execution, reusable workflows, composite actions, and service containers in GitHub Actions.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Jobs, steps, and conditionals
Check out repository code
- uses: actions/checkout@v4

# Fetch the repo in a workflow job.

Set up Node.js
- uses: actions/setup-node@v4
  with:
    node-version: 20

# Install a specific Node.js version for the job.

Run a step only on main
- name: Publish
  if: github.ref == 'refs/heads/main'
  run: npm run publish

# Use expressions with `if` to gate steps.

Run a job only for tags
jobs:
  release:
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest

# Gate a full job on a tag-based release.

Allow a non-blocking step
- name: Optional audit
  continue-on-error: true
  run: npm audit

# Continue the job even if a step fails.

## Matrix, reuse, and services
Test across multiple Node versions
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node: [18, 20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test

# Use a strategy matrix for version coverage.

Run on multiple operating systems
strategy:
  matrix:
    os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

# Validate portability across Linux, macOS, and Windows.

Start a PostgreSQL service container
services:
  postgres:
    image: postgres:16
    env:
      POSTGRES_PASSWORD: postgres
    ports:
      - 5432:5432
    options: >-
      --health-cmd "pg_isready -U postgres"
      --health-interval 10s
      --health-timeout 5s
      --health-retries 5

# Run integration tests against a database service.

Call a reusable workflow
jobs:
  ci:
    uses: my-org/my-repo/.github/workflows/reusable-ci.yml@main
    with:
      node-version: 20
    secrets: inherit

# Centralize shared CI logic in one workflow file.

Composite action metadata
name: setup-project
runs:
  using: "composite"
  steps:
    - run: npm ci
      shell: bash
    - run: npm run build
      shell: bash

# Bundle repeated shell steps into a reusable local action.

Recommended next

No recommendations yet.