Build Matrix
A build matrix is made up by several multiple jobs that run in parallel.
This can be useful in many cases, but the two primary reasons to use a build matrix are:
- Reducing the overall build execution time
- Running tests against different versions of runtimes or dependencies
The examples on this page focus on the latter use case.
There are two ways to define a matrix in the .travis.yml
file:
- Using the Matrix Expansion feature
- Listing individual job configs
Both features can be combined.
Matrix Expansion #
Certain keys are defined as matrix expansion keys that take arrays of values, creating an additional job per value. If several matrix expansion keys are given, this multiplies the number of jobs created.
For example, the following configuration produces a build matrix that expands
to 8 individual (2 * 2 * 2) jobs, combining each value from the three
matrix expansion keys rvm
, gemfile
, and env
.
List individual jobs #
In addition, jobs can be specified by adding entries to the key jobs.include
.
For example, if not all of those combinations of the matrix expansion above are relevant, jobs can be specified individually like so:
Build matrixes are currently limited to a maximum of 200 jobs for both private and public repositories. If you are on an open-source plan, please remember that Travis CI provides this service free of charge to the community. So please only specify the matrix you actually need.
You can also have a look at the Language section in our Travis CI Build Config Reference.
Exclude Jobs #
The build matrix expansion sometimes produced unwanted combinations. In that
case it can be convenient to exclude certain combinations using the key
jobs.exclude
, instead of listing all jobs individually.
For example, this would exclude two jobs from the build matrix:
If the jobs you want to exclude from the build matrix share the same matrix parameters, you can specify only those and omit the varying parts.
Suppose you have:
This results in a 3×3×4 build matrix. To exclude all jobs which have rvm
value 2.0.0
and
gemfile
value Gemfile
, you can write:
Which is equivalent to:
Exclude Jobs with the env value #
When excluding jobs with env
values, the value must match
exactly.
For example,
defines a 3×4 matrix, because the env
value does not match with
any job defined in the matrix.
To exclude all Ruby 1.9.3 jobs with DB=mongodb
set, write:
Explicitly Including Jobs #
It is also possible to include entries into the matrix with jobs.include
:
This adds a particular job to the build matrix which has already been populated.
This is useful if you want to only test the latest version of a dependency together with the latest version of the runtime.
You can use this method to create a build matrix containing only specific combinations. For example,
creates a build matrix with 3 jobs, which runs test suite for each version of Python.
Explicitly included jobs inherit the first value in the array #
The jobs that are explicitly included inherit the first value of the expansion keys defined.
In this example with a 3-job Python build matrix, each job in jobs.include
has the python
value set to '3.8'
.
You can explicitly set the Python version for a specific entry:
Explicitly included jobs with only one element in the build matrix #
As a special case, if your build matrix has only one element and you have explicitly included jobs, matrix expansion is not done, and the explicit jobs completely define your build. For example:
If you need a (sole) job from the matrix in such case, add a blank job entry with curly brackets to the build instruction (as that instruction would inherit all values from the matrix):
Allow Rows to Fail #
You can define rows that are allowed to fail in the build matrix. Allowed failures are items in your build matrix that are allowed to fail without causing the entire build to fail. This lets you add in experimental and preparatory builds to test against versions or configurations that you are not ready to officially support.
Define allowed failures in the build matrix as key/value pairs:
Match Jobs with allow_failures #
When matching jobs against the definitions given in allow_failures
, all
conditions in allow_failures
must be met exactly, and
all the keys in allow_failures
element must exist in the
top level of the build matrix (i.e., not in jobs.include
).
Examples #
Consider
Here, no job is allowed to fail because no job has the env
value
SECRET_VAR1=SECRET1 SECRET_VAR2=SECRET2
.
Next,
Without the top-level env
, no job will be allowed to fail.
Use Fast Finish #
If some rows in the build matrix are allowed to fail, the build won’t be marked as finished until they have completed.
To mark the build as finished as soon as possible, add fast_finish: true
to the jobs
section of your .travis.yml
like this:
Now, the build result will be determined as soon as all the required jobs finish, based on these results, while the rest of the allow_failures
jobs continue to run.
Use Different Programming Languages per Job #
You can also use the jobs.include
feature to have different languages for each job in your build. For example,
This creates a build with 3 jobs as follows:
- A Python 3.8 job
- A Node.js 12 job
- A Java OpenJDK 8 job
Job Names #
Jobs listed in jobs.include
can be named by using the key name
, like so:
This name will appear on the build matrix UI and can be convenient in order to quickly identify jobs in a large matrix.
Jobs generated through the Matrix Expansion feature cannot be named.
Job Uniqueness and Duplicate Jobs #
Jobs need to be unique, and duplicate jobs are dropped during the Build Config Validation process.
For example, this config would result in only one job using the YAML anchors and aliases:
In rare circumstances it can still be desirable to execute multiple jobs with the same config. In such cases, job uniqueness can be achieved by specifying any additional key, e.g., a job name: