Speed Up Your CI With Docker (Travis-CI)
--
How to share your Docker image across your jobs with Travis-CI
If you choose to run all your CI with containers, you might want to share your built image across your jobs to save time.
This Article will use Travis-CI as an example; if you are interested in doing the same with GitHub Actions, please follow this Article.
The purpose is to create a Docker image from our source code and run our tests through that image. That way, we can speed up our test suite by running it in parallel. But how share the image created instead of creating it for each job? That is what we are looking at in this Article.
The complete working file is here.
Travis-CI comes with a handful of virtual environments to help us run our tests with the needed tools and languages set up for us, but we can choose the minimal one as we only need Docker. For more details, check out the official documentation.
In the main section of our `.travis.yml`, we start like that:
dist: focal # Ubuntu 20.04 LTSlanguage: minimal
We are not interested in building anything in the minimal virtual environment. We can skip those steps:
git:
clone: falsebefore_install: skip
install: skip
But we want to get docker services up and running.
services:
- docker
The only steps we want to get in the main section will be the before and the after script to start and stop the services needed to execute the tests. Let say, for instance. We need a database and key-value data stores.
env:
global:
- DATABASE_PASSWORD=db-password-123
- APP_NAME=acmebefore_script:
- docker network create ${APP_NAME}-bridge-docker-network - |
echo "Start Mysql" && \
docker run --rm --detach \
--name ${APP_NAME}-db \
--env MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD} \
--network=${APP_NAME}-bridge-docker-network \
mysql:latest - |
echo "Start Redis" && \
docker run --rm --detach \
--name ${APP_NAME}-redis \…