This article explains how to use GitHub Actions matrix strategy to run jobs across multiple environments efficiently.
GitHub Actions matrix strategy lets you run the same job across multiple environments—OS versions, language runtimes, or Docker images—without duplicating workflow steps. This approach maximizes parallelism, simplifies maintenance, and accelerates your CI/CD pipeline.
Here’s a basic GitHub Actions workflow that runs two independent jobs—one on Ubuntu, one on Windows—to echo Docker info and launch the hello-world container:
Copy
Ask AI
name: Matrix Configurationon: push: workflow_dispatch:jobs: deploy-on-ubuntu: runs-on: ubuntu-latest steps: - name: Echo Docker details run: docker info - name: Run image run: docker run hello-world deploy-on-windows: runs-on: windows-latest steps: - name: Echo Docker details run: docker info - name: Run image run: docker run hello-world
This configuration triggers on every push or manual dispatch and executes both jobs in parallel.
Both jobs complete successfully:
Inspecting the Windows job shows Docker client/server details:
And the hello-world output confirms success:
Copy
Ask AI
Run docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world6fff548006e037: Pulling fs layer365b066fa8d: Pulling fs layer6f16e5b22025: Pulling fs layer...Hello from Docker!This message shows that your installation appears to be working correctly.
Let’s test two Docker images—hello-world and alpine—across three platforms: ubuntu-latest, ubuntu-20.04, and windows-latest. We’ll leverage the matrix variables in both runs-on and our Docker commands:
GitHub Actions will now run 3 OS × 2 images = 6 jobs in parallel, improving CI throughput.After committing these changes, your matrix workflow appears like this:
By default, any failing combination halts the workflow and marks it as failed. For example, alpine isn’t compatible with Windows, so that job errors out:
Copy
Ask AI
Run docker run alpineUnable to find image 'alpine:latest' locallylatest: Pulling from library/alpinedocker: no matching manifest for windows/amd64 in the manifest list entries.See 'docker run --help'.Error: Process completed with exit code 1
A single matrix failure by default stops all running jobs. Use strategy.fail-fast: false or exclude incompatible combinations with matrix.exclude.
In a future article, we’ll cover advanced filtering with exclude and conditional execution.