GitLab - Run the same job on multiple images
Sometimes you want to test your code on different python versions. Using Travis CI it is very easy, but can we do that or something similar in GitLab? Let's see!
The problem: there are some tests written in python and we want to test it on different python versions.
We can easily use extends keyword, define some common logic in .test job and then inherit it in other jobs:
But... for each new version we have to define a new job. Maybe, we can do better?
GitLab has introduced a parallel keyword. Additionally, there is a matrix keyword; using this guy you
can run a job multiple times in parallel in a single pipeline, but with different variable values for each instance of the job.
But, you cannot change image keyword for example 😞. Maybe there is another way?
Have you ever heard about dind (Docker-in-Docker)? If not, here is some info about it:
Another way to configure GitLab Runner for docker support is to register a runner with the Docker executor and use the Docker image to run your job scripts. This configuration is referred to as “Docker-in-Docker.”
So... we can spawn a service (using a dind image) and then run docker run <image> commands in our jobs. Then, using parallel and matrix keywords we can construct a... matrix!
Here is an example:
This way, in a pipeline there will be three jobs and each of them will run different python versions, but the command will be the same.
Maybe in the near future GitLab will add a support (somehow 🤷) and it will be possible to change image keyword for example, but now we have to deal with it this way using dind.
Here you will find a repository with an example.
Comments
Post a Comment