Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Travis-CI: docker image as build environment

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?

like image 567
Vahid Avatar asked Jun 30 '16 20:06

Vahid


People also ask

Can you use Docker as a development environment?

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.

Does Travis CI use Docker?

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.

Can Containerd build images?

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.

Which Travis CI infrastructure environment runs full virtual machines?

Our default infrastructure is an Ubuntu Linux ( os: linux ) virtual machine running on AMD64 architecture ( arch: amd64 ), on Google Compute Engine.


1 Answers

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/

like image 180
Norman B. Lancaster Avatar answered Oct 08 '22 10:10

Norman B. Lancaster