Sachith Dassanayake Software Engineering GitHub Actions reusable workflows & matrices — From Zero to Production — Practical Guide (May 7, 2026)

GitHub Actions reusable workflows & matrices — From Zero to Production — Practical Guide (May 7, 2026)

GitHub Actions reusable workflows & matrices — From Zero to Production — Practical Guide (May 7, 2026)

GitHub Actions reusable workflows & matrices — From Zero to Production

Level: Intermediate

Date: May 7, 2026

Introduction

GitHub Actions continues to evolve as a powerful CI/CD platform, with reusable workflows and matrix strategies becoming essential tools in the modern software engineer’s toolkit. Since their stable introduction (reusable workflows became generally available around late 2021, matrices long before), these features have enabled greater DRY (Don’t Repeat Yourself) principles and scalable testing strategies.

This article takes you from a clean slate to production-ready reusable workflows combined with matrix builds, applicable to GitHub Actions versions from late 2021 onwards. We cover best practices, common pitfalls, and validation strategies.

Prerequisites

  • Familiarity with GitHub Actions basic syntax and use (yaml files in .github/workflows/).
  • A repository on GitHub with write permission (private or public).
  • GitHub Actions runners are enabled (default for most repos).
  • GitHub CLI or the web UI to inspect runs and logs.
  • Basic understanding of YAML, matrix strategies, and job concepts.

Hands-on steps

1. Create a reusable workflow

Reusable workflows are authored like regular workflows but designed to be invoked via workflow_call. They help centralise CI logic, especially when you want consistent steps across multiple repositories or workflows.

# .github/workflows/build-and-test.yml
on:
  workflow_call:
    inputs:
      node-version:
        required: true
        type: string
    secrets:
      NPM_TOKEN:
        required: true

jobs:
  build-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node-version: [14, 16, 18]
    steps:
      - uses: actions/checkout@v4
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install dependencies
        run: npm ci
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Run tests
        run: npm test

Notes: Here, the reusable workflow expects two inputs: a required version of Node.js (passed by the caller) and a secret for npm authentication. The job uses a matrix over three OSes and three Node versions, demonstrating cross-platform, multi-version CI.

2. Call the reusable workflow with matrices

GitHub Actions only supports passing inputs to reusable workflows, not matrices directly (at least before May 2026; still awaiting matrix inputs support). Therefore, the matrix must live inside the reusable workflow. However, the caller can customise inputs and secrets.

# .github/workflows/ci.yml
name: CI

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  call-build-test:
    uses: ./.github/workflows/build-and-test.yml
    with:
      node-version: '16'
    secrets:
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

In this example, ci.yml invokes build-and-test.yml, passing the Node.js version input and the npm token secret. Matrix expansion happens inside the reusable workflow.

3. When to choose reusable workflows over composite actions

Reusable workflows themselves can contain complex job matrices with OS and tool version permutations, whereas composite actions are ideal for encapsulating a sequence of steps that run inside one job context. If you have multi-job or matrix-heavy CI scenarios, reusable workflows are generally a better fit. Composite actions work well for encapsulating reusable step logic that does not require a separate job.

Common pitfalls

  • Matrix propagation limitation: You cannot pass a matrix as an input from the caller repository — matrices must be declared inside the reusable workflow. To handle variability from the caller, consider passing parameters that influence matrix values conditionally inside the reusable workflow.
  • Secret forwarding: Secrets are not automatically forwarded to reusable workflows; you must explicitly pass secrets via the secrets: map in the caller.
  • Runs-on consistency: Beware of OS images in the matrix not being available on GitHub-hosted runners in the targeted region or organisation — always check current runner availability.
  • Version tagging: When referencing reusable workflows across repositories or in your monorepo, pin to a commit SHA for stable production usage rather than a branch ref.
  • Input validation: Inputs declared in reusable workflows are not validated against arbitrary strings by default — consider adding validation steps when inputs influence the build significantly.

Validation

Proper validation of your reusable workflows requires a combination of manual and automation tests:

  • GitHub Actions UI: Use the Actions tab to monitor different matrix jobs, their logs, and step timings.
  • Dry runs: GitHub allows workflow dispatch events with inputs; use those to test inputs before committing.
  • Repository-level test workflow: Use a dedicated test branch that calls the reusable workflow with varied parameter sets ensuring all matrix permutations run at least once.
  • Lint and schema validation: Use tools like act, yamllint, and GitHub Actions schema checkers integrated in IDEs.

Checklist / TL;DR

  • Create reusable workflows using on: workflow_call and declare all matrix strategies inside.
  • Explicitly forward secrets and required inputs when calling reusable workflows.
  • Pin references with commit SHA or tags for stable production usage.
  • Choose reusable workflows over composite actions when you need cross-job or matrix parallelism.
  • Validate input parameters and test all matrix branches before deploying.
  • Monitor GitHub-hosted runner availability and update matrix OS/image naming accordingly.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Post