Skip to main content

Running Matrix builds with GitLab CI

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

We recently started experimenting with the GitLab CI Runners as we are looking to replace our "old" Jenkins v1.x set up with something new. And since over the last few weeks we had some issues with the Jenkins GitLab plugin we thought it might a good idea to take a deeper look into the GitLab CI Runners. One the plus side the GitLab CI Runners are configured via a YAML file in a similar fashion as you would configure Travis CI which we use to build our open-source components. Since on Travis we rely a lot on the so-called matrix builds to run the unit tests for different PHP versions I was wondering how we could solve the problem with GitLab CI. At first glance GitLab CI does not have a matrix build command but it comes with a feature called Anchors which kind of act as a template that can be merged in a job configuration.

First we need to define a hidden job - any job name prefixed with a dot and followed by an ampersand and the name of the anchor. In the code below we create a hidden job called "job_template" which can be accessed by the anchor name "job_definition" later on:

.job_template: &job_definition
script:
- ./vendor/bin/phpunit

In this case the template is rather simple, it simply runs phpunit. To merge in the job template into one of the defined jobs use the << operator and refer to the defined anchor name "job_definition".

php7:
<<: *job_definition

To make sure the PHP 7.0 build runs on a PHP 7.0 box and the PHP 7.1 build runs on a box with PHP 7.1 installed we make use of tags. Each shared GitLab CI Runner contains a list of tags which identify the components available on that box. Thus the whole .gitlab-ci.yml file looks like this:

before_script:
- composer.phar install

.job_template: &job_definition
script:
- ./vendor/bin/phpunit

php7:
<<: *job_definition
tags:
- php7

php71:
<<: *job_definition
tags:
- php71