Learn GitHub
$ gh ghautomation.ghactionsadvanced

Advanced Actions: Matrices, Reuse & Identity

5 learning objectives Β· 6 key terms Β· 2 commands Β· 1 diagrams Β· 5 quiz questions

$objectives

Advanced Actions: Matrices, Reuse & Identity

Automation & Security

Module 4 of 5 Β· lesson 18 in this module

What you'll be able to do

  • Fan jobs out across versions and platforms with matrix strategies
  • Share logic with reusable workflows and composite actions
  • Scope permissions per job with least-privilege tokens
  • Authenticate to clouds passwordlessly with OIDC
  • Cache dependencies to cut build times safely
$concept

The idea, in plain language

Basic workflows run one job on one runner; real pipelines fan out and share. A matrix strategy multiplies one job definition across versions, operating systems, or configurations β€” test Node 18/20/22 on ubuntu, macOS and Windows from a dozen lines. Reusable workflows (workflow_call) and composite actions extract shared steps so every repository stops copy-pasting the same lint-and-test sequence.

Security scales with structure: the default GITHUB_TOKEN should be scoped to the minimum permissions each job needs, and OpenID Connect lets workflows assume cloud roles with no stored secrets at all β€” short-lived identity instead of long-lived keys. Caching (actions/cache, or built-in npm/pip caching in setup actions) trades a little configuration for dramatically faster builds, keyed so stale caches never poison results.

$key terms

Key terms

matrix strategy
Runs one job definition across combinations of versions, OSes, or variables.
reusable workflow
A workflow invoked by other workflows via workflow_call, with inputs and secrets.
composite action
A packaged group of steps addressable as a single uses: reference.
permissions block
Per-job token scoping β€” least privilege for every workflow run.
OIDC
OpenID Connect: short-lived cloud identity for workflows, replacing stored secrets.
cache key
The hash (often of lockfiles) deciding cache hits β€” restore-keys provide fallbacks.
$steps

How it works, step by step

  1. 1
    Multiply jobs with a matrix

    Declare axes once; the runner fans out to every combination, each visible as a separate check on the pull request.

    matrixbash
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        node: [18, 20]
    runs-on: ${{ matrix.os }}
  2. 2
    Extract shared workflows

    Move repeated jobs into a reusable workflow with declared inputs and secrets, then call it with uses: org/repo/.github/workflows/ci.yml@main.

    reusebash
    jobs:
      call-ci:
        uses: myorg/shared/.github/workflows/ci.yml@v1
        with: { node: 20 }
        secrets: inherit
  3. 3
    Scope every token

    Set top-level permissions: {} (nothing) and grant per job only what it uses β€” contents: read, pull-requests: write β€” so a compromised step can do little.

    permissionsbash
    permissions: {}
    jobs:
      test:
        permissions: { contents: read }
  4. 4
    Authenticate without secrets

    Grant id-token: write, then let the cloud provider trust the repository identity β€” temporary credentials minted per run, nothing stored.

    oidcbash
    permissions:
      id-token: write
      contents: read
    # then: aws-actions/configure-aws-credentials (no keys)
  5. 5
    Cache dependencies

    Use the setup action's built-in cache (cache: npm) or actions/cache keyed on lockfiles β€” restore-keys fall back gracefully when exact keys miss.

    cachebash
    - uses: actions/setup-node@v4
      with: { node-version: 20, cache: npm }
$commands

Commands worth memorising

Every block below is copy-ready β€” use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what β€œworking” looks like.

watch a matrix runbash
gh run watch
Output
βœ“ test (ubuntu-latest, 18)
βœ“ test (ubuntu-latest, 20)
βœ“ test (windows-latest, 18)
βœ“ test (windows-latest, 20)

Each matrix combination reports as its own check on the pull request.

re-run only failuresbash
gh run rerun --failed
Output
βœ“ Re-ran failed jobs

Retries just the red matrix legs instead of the whole run.

$diagrams

Workflow diagrams

Diagram 1One definition, many runners

A dozen lines of YAML becomes a full compatibility grid on every pull request.

Reading the diagram
  1. Declare axes, inputs, and minimum permissions once.
  2. Every pull request fans out, shares logic, and stays scoped.
  3. Failures re-run per leg; caches warm across runs.
$example

Worked example

A library tested on two Node versions with cached installs and minimal token scope.

matrix CI, hardenedyaml
permissions: {}

jobs:
  test:
    permissions: { contents: read }
    strategy:
      matrix: { node: [18, 20] }
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: ${{ matrix.node }}, cache: npm }
      - run: npm ci && npm test
Output
Two green checks (Node 18, Node 20) with warm caches and a read-only token.
1Top-level permissions: {} denies everything by default; the job grants only contents: read.
2The matrix fans one definition into two runners, each installing with lockfile-keyed caching.
3Both legs report separately, so a version-specific failure is obvious, not averaged away.
$use cases

Where you'll use it

β–ΈTesting libraries across Node/Python versions and operating systems.
β–ΈSharing one CI definition across dozens of repositories.
β–ΈDeploying to clouds with short-lived OIDC identity instead of stored keys.
β–ΈCutting minutes off installs with lockfile-keyed caching.
$mistakes

Common mistakes

  • Leaving the default write-all token on every job.
  • Storing cloud credentials as long-lived repository secrets when OIDC fits.
  • Caching node_modules by branch name and wondering why installs never refresh.
  • Copy-pasting workflow YAML across repos instead of extracting reusables.
$best practices

Best practices

  • Start every workflow with permissions: {} and grant per job.
  • Prefer OIDC over stored secrets for cloud authentication.
  • Key caches on lockfiles with restore-key fallbacks.
  • Extract repeated jobs into versioned reusable workflows.
  • Re-run failures per leg with gh run rerun --failed.
$exercise

Hands-on exercise

Harden a workflow

Take a basic CI workflow to least-privilege, cached, matrix form.

  1. Add permissions: {} at the top and minimal per-job grants until it goes green.
  2. Add a second runtime version via a matrix and confirm both legs report.
  3. Enable dependency caching and measure the install-time difference.
  4. Extract a repeated job into a reusable workflow called from the original.
  5. If you deploy anywhere, replace a stored credential with OIDC.

πŸ’‘ Hint: The workflow run logs show the effective permissions when debugging scope errors.

Done when: A green matrix run, faster installs, minimal scopes, and no copied YAML.

$quiz

Checkpoint quiz

0 of 5 answered Β· 0 correct

  1. Multiple choiceQ1

    What does a matrix strategy do?

  2. Multiple choiceQ2

    Why start workflows with permissions: {}?

  3. Multiple choiceQ3

    What does OIDC replace in cloud deployments?

  4. True or falseQ4

    True or false: reusable workflows eliminate copied YAML by letting workflows call shared definitions with inputs and secrets.

  5. TroubleshootingQ5

    Installs are slow on every run despite caching configured. What is most likely wrong?

$summary

Module summary

  • Matrices fan one job across versions and platforms as separate checks.
  • Reusable workflows and composite actions end copy-paste CI.
  • Least-privilege tokens (permissions: {} plus grants) contain breaches.
  • OIDC replaces stored cloud secrets with per-run identity.
  • Lockfile-keyed caches make installs fast without staleness.

Next up: Pipelines are smart; next is developing inside the cloud itself, with environments that boot per branch.

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.