· engineering · 2 min read
Gitlab parallel matrix pipelines
Excuting a job with multiple configurations in parallel using Gitlab.
Table of Contents
We use GitLab, and recently we needed to run the same pipeline, but with multiple different variables. It’s a common problem you might encounter, for example to prepare a docker Image with various different PHP versions, or create a build of an app targeting different operating systems.
Parallel Matrix Pipelines
Matrix pipelines in GitLab CI/CD are the solution, they enable us to execute jobs in parallel with multiple values for variables.
We simply need to use the parallel: matrix:
key within GitLab’s CI configuration file, .gitlab-ci.yml, to specify our array of variable values.
A Single Variable
You might want to build docker images, each configured with different versions of PHP. By defining a single variable for PHP versions in a matrix, each version can trigger a separate parallel job. Here’s how you might set this up:
In this configuration, four jobs will run simultaneously, each building a Docker image with a different PHP version.
Multiple Variables
Matrix pipelines can also combine multiple variables to generate more comprehensive job configurations. For example, you might want to test different PHP versions on both nginx and Apache servers. Adding another dimension to the matrix accomplishes this:
This setup triggers eight parallel jobs, each for a unique combination of PHP version and server type, effectively covering all permutations and ensuring comprehensive testing across configurations.
Limitations
- Maximum Permutations: There is a limit of maximum 200 maximum allowed permutations (though if you’re reaching this you might be doing something wrong!)
- Limited Values: The values must be either a string, or an array of strings.
- Duplicate values As job names are generated from variable values, you cannot use the same value twice (for two variables). For example,
8.0
in the following would entries would overwrite each other:
Conclusion
Matrix pipelines in GitLab CI/CD makes parallel execution of multiple job configurations a breeze, saving time and reducing the complexity of your gitlab-ci.yaml
and maybe even encouraging a more thorough testing process!
For a few more examples, read the full documentation.
About James Babington
A cloud architect and engineer with a wealth of experience across AWS, web development, and security, James enjoys writing about the technical challenges and solutions he's encountered, but most of all he loves it when a plan comes together and it all just works.
No comments yet. Be the first to comment!