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

Compose job logic cleanly and gate execution with expressions.

Check out repository code

Fetch the repo in a workflow job.

yamlANYcheckoutactions
yaml
- uses: actions/checkout@v4
Notes

Most build, test, and release workflows start with a checkout step.

Set up Node.js

Install a specific Node.js version for the job.

yamlANYsetup-nodenode
yaml
- uses: actions/setup-node@v4
  with:
    node-version: 20
Notes

Use official setup actions for language runtimes whenever possible.

Run a step only on main

Use expressions with `if` to gate steps.

yamlANYifconditional
yaml
- name: Publish
  if: github.ref == 'refs/heads/main'
  run: npm run publish
Notes

Conditionals help keep a single workflow flexible across branches and events.

Run a job only for tags

Gate a full job on a tag-based release.

yamlANYiftagsrelease
yaml
jobs:
  release:
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
Notes

Use job-level conditions when the whole job should be skipped.

Allow a non-blocking step

Continue the job even if a step fails.

yamlANYcontinue-on-error
yaml
- name: Optional audit
  continue-on-error: true
  run: npm audit
Notes

Useful for advisory checks that should not fail the pipeline yet.

Matrix, reuse, and services

Scale workflows across versions, platforms, and reusable components.

Test across multiple Node versions

Use a strategy matrix for version coverage.

yamlANYmatrixnodestrategy
yaml
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
Notes

Matrix builds are the standard way to validate against multiple versions or OSes.

Run on multiple operating systems

Validate portability across Linux, macOS, and Windows.

yamlANYmatrixos
yaml
strategy:
  matrix:
    os: [ubuntu-latest, macos-latest, windows-latest]

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

Great for CLI tools and cross-platform libraries.

Start a PostgreSQL service container

Run integration tests against a database service.

yamlANYservicespostgresintegration-tests
yaml
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
Notes

Service containers make it easy to run databases and dependent services inside CI.

Call a reusable workflow

Centralize shared CI logic in one workflow file.

yamlANYreusable-workflowusessecrets
yaml
jobs:
  ci:
    uses: my-org/my-repo/.github/workflows/reusable-ci.yml@main
    with:
      node-version: 20
    secrets: inherit
Notes

Reusable workflows reduce duplication across repos and teams.

Composite action metadata

Bundle repeated shell steps into a reusable local action.

yamlANYcomposite-actionaction.yml
yaml
name: setup-project
runs:
  using: "composite"
  steps:
    - run: npm ci
      shell: bash
    - run: npm run build
      shell: bash
Notes

Composite actions are useful for local step reuse without creating a full JavaScript or Docker action.

Recommended next

No recommendations yet.