Skip to content

Lesson 2 of 9 · 24m

Expressions, Matrices, and Job Outputs Without Guesswork

How expression evaluation really works, how to build maintainable matrices, and how to pass data between jobs without shell parsing hacks.

Most Actions YAML bugs are type bugs wearing a shell costume. You think you passed false, but the shell received the string false, which is truthy in an if statement. You think an output is JSON, but the next job receives one long string. Expressions are simple once you respect where evaluation happens.

Expressions evaluate before the shell

${{ }} is not shell interpolation. GitHub evaluates it and substitutes the resulting text into the step before the shell runs. That distinction matters for quoting, control flow, and security.

- name: Mixed evaluation layers
  run: |
    if [ "${{ github.event_name }}" = "push" ]; then
      echo "substituted first, then interpreted by bash"
    fi

This works, but it mixes two evaluation layers. Prefer expression control flow such as if: github.ref == 'refs/heads/main'; use shell logic for shell concerns.

Matrices are a fan-out compiler

A matrix expands one job definition into multiple job instances. Keep the matrix small, explicit, and meaningful; every axis multiplies cost.

strategy:
  fail-fast: false
  matrix:
    os: [ubuntu-latest, windows-latest]
    node: [20, 22]
runs-on: ${{ matrix.os }}
steps:
  - uses: actions/setup-node@v4
    with:
      node-version: ${{ matrix.node }}
  - run: npm ci && npm test

fail-fast: false is often right for compatibility matrices. Otherwise the first red Node version cancels every other leg and you lose the diagnostic value of the matrix.

Job outputs are typed as strings

A job exposes outputs through $GITHUB_OUTPUT. They are strings. If you need structured data, encode JSON deliberately and parse it deliberately.

outputs:
  changed: ${{ steps.filter.outputs.changed }}
steps:
  - id: filter
    run: |
      echo 'changed=true' >> "$GITHUB_OUTPUT"
      echo 'shard={"services":["api","web"]}' >> "$GITHUB_OUTPUT"
# downstream job:
# if: needs.detect.outputs.changed == 'true'

Notice the comparison uses the string 'true'. That is not pedantry; it is how the system works. For dynamic fan-out, matrix: ${{ fromJSON(needs.plan.outputs.matrix) }} lets a planning job compute the next job's matrix; emit that JSON from a real script, not shell string concatenation.

Field notes

  • echo "name=value" >> "$GITHUB_OUTPUT" is the modern output mechanism; ::set-output is dead.
  • Multiline output requires a heredoc delimiter written to $GITHUB_OUTPUT; use it for changelogs and generated notes.
  • toJSON(github) in a debug step is useful, but never dump the secrets context.
  • Matrix legs are separate jobs. A flaky leg is retried as a whole leg, not as one step inside a shared machine.

Was this lesson helpful?

Sign in to rate