Advanced Actions: Matrices, Reuse & Identity
5 learning objectives Β· 6 key terms Β· 2 commands Β· 1 diagrams Β· 5 quiz questions
Advanced Actions: Matrices, Reuse & Identity
Automation & SecurityWhat 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
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
- 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.
How it works, step by step
- 1Multiply jobs with a matrix
Declare axes once; the runner fans out to every combination, each visible as a separate check on the pull request.
matrixbashstrategy: matrix: os: [ubuntu-latest, windows-latest] node: [18, 20] runs-on: ${{ matrix.os }}
- 2Extract 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.
reusebashjobs: call-ci: uses: myorg/shared/.github/workflows/ci.yml@v1 with: { node: 20 } secrets: inherit
- 3Scope 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.
permissionsbashpermissions: {} jobs: test: permissions: { contents: read }
- 4Authenticate without secrets
Grant id-token: write, then let the cloud provider trust the repository identity β temporary credentials minted per run, nothing stored.
oidcbashpermissions: id-token: write contents: read # then: aws-actions/configure-aws-credentials (no keys)
- 5Cache 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 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.
gh run watch
β 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.
gh run rerun --failedβ Re-ran failed jobs
Retries just the red matrix legs instead of the whole run.
Workflow diagrams
| Technique | Problem it solves | Key syntax |
|---|---|---|
| Matrix | Version Γ OS coverage | strategy.matrix + ${{ matrix.x }} |
| Reusable workflows | Copied YAML everywhere | workflow_call with inputs/secrets |
| Least-privilege tokens | Overpowered default token | permissions: {} plus per-job grants |
| OIDC | Stored cloud secrets | id-token: write + provider trust |
| Caching | Slow dependency installs | cache keys on lockfiles |
A dozen lines of YAML becomes a full compatibility grid on every pull request.
- Declare axes, inputs, and minimum permissions once.
- Every pull request fans out, shares logic, and stays scoped.
- Failures re-run per leg; caches warm across runs.
Worked example
A library tested on two Node versions with cached installs and minimal token scope.
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
Two green checks (Node 18, Node 20) with warm caches and a read-only token.
Where you'll use it
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
- 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.
Hands-on exercise
Harden a workflow
Take a basic CI workflow to least-privilege, cached, matrix form.
- Add permissions: {} at the top and minimal per-job grants until it goes green.
- Add a second runtime version via a matrix and confirm both legs report.
- Enable dependency caching and measure the install-time difference.
- Extract a repeated job into a reusable workflow called from the original.
- 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.
Checkpoint quiz
0 of 5 answered Β· 0 correct
- Multiple choiceQ1
What does a matrix strategy do?
- Multiple choiceQ2
Why start workflows with permissions: {}?
- Multiple choiceQ3
What does OIDC replace in cloud deployments?
- True or falseQ4
True or false: reusable workflows eliminate copied YAML by letting workflows call shared definitions with inputs and secrets.
- TroubleshootingQ5
Installs are slow on every run despite caching configured. What is most likely wrong?
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.
Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
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.