In Travis CI is it possible to run the build process from inside a docker container?
In GitLab CI this is the default. We can simply define the image in .gitlab-ci.yml
then all the build/test/deploy will run inside that container. However, Travis seems to have totally different view about docker usage. How can I achieve a similar behavior in Travis?
Docker is a great way to provide consistent development environments. It will allow us to run each of our services and UI in a container. We'll also set up things so that we can develop locally and start our dependencies with one docker command. The first thing we want to do is dockerize each of our applications.
Travis CI builds can run and build Docker images, and can also push images to Docker repositories or other remote storage. Then you can add - docker commands to your build as shown in the following examples. We do not currently support use of Docker on macOS.
You cannot use containerd to build container images. Linux images with containerd include the Docker binary so that you can use Docker to build and push images. However, we don't recommend using individual containers and local nodes to run commands to build images.
Our default infrastructure is an Ubuntu Linux ( os: linux ) virtual machine running on AMD64 architecture ( arch: amd64 ), on Google Compute Engine.
It turns out this is easier to do with Travis-CI than it first appears. All you have to do is write your normal build script using docker exec
calls. Doing some of the trickier third-party service integrations may require dedicated shell scripts, as in the codecov.io example below.
Example:
sudo: required
language: cpp
services:
- docker
before_install:
- docker pull user/build:latest
- docker run -it -d --name build user/build bash
- docker exec build git clone https://github.com/user/product.git
script:
- docker exec build cmake -H/product -B/_build
- docker exec build cmake --build /_build
- docker exec build cmake --build /_build --target documentation
- docker exec build cmake --build /_build --target run-tests
after_success:
- docker exec build bash /project/codecov.sh
codecov.sh:
#!/usr/bin/env bash
cd /project && \
bash <(curl -s https://codecov.io/bash) \
-f /_build/app.coverage.txt \
-t uuid-project-token \
-X gcov \
-X coveragepy \
-X search \
-X xcode \
-R /project \
-F unittests \
-Z
A real-life project using this technique can be found here: https://github.com/qbradq/tales-of-sosaria/tree/e28eb9877fd7071adae9ab03f40a82ea8317a7df
And I wrote an article about the whole process here: https://normanblancaster.wordpress.com/2017/01/31/leading-edge-c-build-environments-with-docker-and-travis-ci/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With