- How to shard a large test suite using the
parallelkeyword. - How to run jobs across different environments or versions with
parallel:matrix.
1. Sharding Test Suites with parallel
Running a monolithic test job can lead to long feedback loops. For example, a 100-test RSpec suite might take ~20 minutes in a single job. You can reduce this by splitting tests into N segments:
test job. Each instance runs roughly 20 tests, cutting overall runtime by ~80%.
GitLab automatically sets
$CI_NODE_INDEX (starting at 1) and $CI_NODE_TOTAL for each parallel job. Use them to customize test split logic.2. Running Jobs Across Multiple Dimensions with parallel:matrix
When you need to validate or deploy across different environments—like OS types or runtime versions—parallel:matrix provides a clean, DRY approach. Instead of duplicating job definitions, define variable axes in a matrix.
2.1. Base Deployment Job
Here’s a simple deployment job that prints Node.js and npm versions:2.2. Example 1: Varying Runner Machines
To target different runner machines without duplicating the job:
Ensure each runner has the appropriate
tags configured to match $RUNNER_MACHINE. Otherwise, jobs may remain pending.2.3. Example 2: Combining Runners and Node.js Versions
You can further expand the matrix to cover multiple Node.js versions:

3. Matrix Configuration Overview
| Variable | Values |
|---|---|
| RUNNER_MACHINE | saas-linux-small-amd64, saas-linux-medium-amd64, windows, macos |
| NODE_VERSION | 20-alpine3.18, 18-alpine3.18, 21-alpine3.18 |
4. Conclusion
By usingparallel and parallel:matrix, you can:
- Drastically reduce CI feedback loops by sharding tests.
- Validate or deploy against multiple environments without redundant job definitions.