0Pricing
DevOps Bootcamp · Lesson

Matrix Builds for Multiple Environments

Utilize matrix strategies to run jobs across multiple versions of languages, operating systems, or configurations in parallel.

Matrix Builds for Multiple Environments is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Matrix Builds?

Welcome to a powerful feature in GitHub Actions: Matrix Builds! Imagine needing to test your code across many different environments, like various operating systems or programming language versions.

Instead of creating a separate workflow for each combination, matrix builds let you define a set of variables, and GitHub Actions will automatically run your jobs for every possible combination of those variables.

Why Use Matrix Builds?

Matrix builds are incredibly useful for:

  • Cross-Platform Testing: Ensuring your application works on Windows, macOS, and Linux.
  • Multi-Version Compatibility: Testing against different versions of Node.js, Python, or your database.
  • Parallel Execution: Running all these tests simultaneously, saving a lot of time.

They help you achieve wider test coverage efficiently.

Basic Matrix Syntax

You define a matrix within the strategy section of a job. Inside strategy.matrix, you list one or more variables, each with an array of values.

GitHub Actions then creates a job for each combination. Let's see how it looks:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14, 16, 18]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install dependencies
        run: npm ci
      - name: Run tests
        run: npm test

Example: Multiple Node.js Versions

In the previous snippet, we defined a node-version variable with three values: 14, 16, and 18.

This single job definition will trigger three separate jobs, each running on ubuntu-latest but using a different Node.js version. This ensures your project works across these versions.

Combining Multiple Matrix Variables

You're not limited to just one variable! You can combine multiple variables in your matrix. For example, you might want to test different operating systems with different Node.js versions.

Just add more key-value pairs under strategy.matrix. The total number of jobs will be the product of the number of values for each variable.

Example: OS and Node.js Matrix

Here’s how to set up a matrix to test on both Ubuntu and Windows, across Node.js versions 16 and 18.

This will create 2 (OS) * 2 (Node.js versions) = 4 separate jobs.

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

Customizing Combinations with 'include'

Sometimes you need specific combinations that aren't covered by the default Cartesian product, or you want to add extra variables for certain builds. Use include to add new matrix entries.

For instance, maybe you want to run an additional test only on Ubuntu with Node.js 14, or pass a specific environment variable to a particular OS/version combo.

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        node-version: [16, 18]
        include:
          - os: ubuntu-latest
            node-version: 14
            test-suite: 'full'

Excluding Specific Combinations

What if you have a combination that's known to fail, or isn't relevant to your project? The exclude keyword allows you to remove specific configurations from your matrix.

This helps you avoid unnecessary job runs and focus on relevant test cases.

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        node-version: [16, 18, 20]
        exclude:
          - os: windows-latest
            node-version: 16

Accessing Matrix Variables

Within your job steps, you can access the current matrix variable values using the ${{ matrix.variable_name }} syntax. This is crucial for configuring tools or running commands specific to each matrix combination.

For example, ${{ matrix.os }} dynamically sets the runner operating system, and ${{ matrix.node-version }} configures the Node.js setup action.

Practical Applications

Matrix builds are powerful for:

  • Library Development: Testing against multiple versions of a dependency.
  • Multi-Language Projects: Building/testing components written in different languages or compilers.
  • Browser Compatibility: If using a tool that supports it, testing web apps across different browsers.

They streamline complex testing scenarios into concise, maintainable workflows.

Matrix Build Challenge

Consider a GitHub Actions workflow that uses a matrix strategy. The matrix is defined as:

strategy:
  matrix:
    os: [ubuntu-latest, macos-latest]
    python-version: [3.8, 3.9]
    exclude:
      - os: macos-latest
        python-version: 3.8

How many jobs will be created by this matrix configuration?

Recap: Matrix Builds

Great job! You've learned about GitHub Actions Matrix Builds.

  • They allow you to run jobs for multiple combinations of variables (OS, language versions, etc.).
  • Defined using strategy.matrix within a job.
  • include adds specific combinations, while exclude removes them.
  • Variables are accessed via ${{ matrix.variable_name }}.

Matrix builds are essential for efficient, comprehensive testing across diverse environments. Keep practicing to master them!

Frequently asked questions

Is the “Matrix Builds for Multiple Environments” lesson free?

Yes — the full text of “Matrix Builds for Multiple Environments” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Matrix Builds for Multiple Environments”?

Utilize matrix strategies to run jobs across multiple versions of languages, operating systems, or configurations in parallel. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Matrix Builds for Multiple Environments” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Matrix Builds for Multiple Environments
  2. Caching Dependencies for Speed
  3. Reusable Workflows and Actions
  4. Conditional Execution and Job Dependencies
← Back to DevOps Bootcamp