DevOps / GitHub Actions Interview Questions
How do you use matrix builds in GitHub Actions to test across multiple environments?
A matrix strategy tells GitHub Actions to spawn multiple parallel job instances from a single job definition, varying one or more parameters across those instances. This is ideal for testing against several language versions, operating systems, or configuration combinations without duplicating YAML.
jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] node: ['18', '20', '22'] fail-fast: false # continue other matrix jobs if one fails steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} - run: npm test
This definition spawns 3 × 3 = 9 parallel jobs, one for each OS/Node combination. Matrix values are referenced with ${{ matrix.<variable> }} anywhere in the job definition — including runs-on:, step inputs, and environment variables.
Key options:
fail-fast: false— by default, if any matrix job fails all remaining ones are cancelled. Set tofalseto let every combination finish regardless.include:— add extra combinations or inject extra variables into specific cells. For example, add a code-coverage flag only on Node 20/Ubuntu.exclude:— remove specific combinations from the matrix (e.g. skip macOS on an older Node version).max-parallel:— cap the number of concurrent jobs to avoid exhausting runner capacity.
Matrices can also be generated dynamically at runtime by having a prior job output a JSON array and referencing it with fromJSON(needs.setup.outputs.matrix).
More Related questions...