Using Travis CI for Qt projects
By: Mihail Mihalache
Tags: Github, Continuous Integration, Qt, Travis Ci, Cpp

Step 1: Create and publish a Qt docker container

We’ll build Qt from sources, the way we need them in terms of operating system and building options for local development, inside a docker container, which we will be able to pull from Travis-CI.

There’s no single way of doing this, this is how I managed to do it. We’ll create a folder with three files:

A license file — this is not needed for the open source license, of course.

The docker image is defined by the Dockerfile:

We’ll define some environment variables that will be used inside the container.

At line 17, notice that this copies the license file inside the docker image → this is only required if you’re using a commercial license. Then the first command is a shell script that will download the Qt sources, install Qt and do some additional house-keeping for the container:

If you need to use a license, make sure to have it accessible as defined in the Dockerfile. If not, be sure to edit the getQt.sh in order to use the open source license (see lines 52 and 53).

Now we can create the docker image:

docker build -t qt-ubuntu-5.15.2 .

And then build the container:

docker run qt-ubuntu-5.15.2

This can take about an hour, depending on your system spec. You can see the newly created container by typing:

docker ps -a

You’ll see that docker has given it a random name. We’ll rename it and commit it using our docker hub username, so that it becomes a docker image, ready to be pushed to the docker hub:

docker rename <randomly_generated_name> qt-ubuntu-5.15.2

docker commit qt-ubuntu-5.15.2 <username>/qt-ubuntu-5.15.2:v1

Important: Before publishing the container, make sure that if you’re using a commercial license you will keep your docker repository private. Because otherwise someone else could use your Qt docker container to build their software, which, by way of logic, could potentially invalidate your license (for more accurate information, please contact Qt customer support).

Now the container can be push the image to the docker hub:

docker push <username>/qt-ubuntu-5.15.2:v1

Step 2: Pull the image in Travis CI

With this docker image you can build and run the tests for your Qt repository in Travis-CI using the Qt docker image. Here’s a basic travis configuration:

Here you should replace <git_repo>with your repository name. And this assumes that you have a shell which builds your application — in this case buildWithDocker.sh.

Line 11 is where the magic happens: This takes the contents of the current directory and copies it in the specified location in the docker container:

"$PWD":/root/<git_repo>

and then runs the shell command inside the docker container:

/root/<gir_repo>/buildWithDocker.sh

DOCKER_USERNAME and DOCKER_PASSWORD are encrypted environment variables defined inside Travis CI and are loaded in lines 19 and 20 (the actual content needs to be copied from the generated encryption values from Travis CI).

In order not to download the same docker image over and over again, we can use docker cahing. At lines 12 through 16, docker saves all the images inside a cache folder, so that from now on at line 7 docker loads all archived docker containers. The content of the cache can be cleared from the settings of repository in Travis CI.

A build.sh example would be:

#!/bin/bash cd /root/<git_repo>

qmake <git_repo>.pro -spec linux-g++ CONIFG+=release && make -j$(nproc)

That’s it!

Now you should be able to successfully trigger Travis CI builds.

Keep in mind!

As a side-note, I strongly recommend not to use other people’s (i.e. unofficial / unsigned) Qt docker images for building your app. Because you never know what you get. Source code can be tampered with and you might get a malware embedded in your app or get your source code exposed.

Also, if you’re using the commercial version of the docker hub, it wouldn’t hurt to enable snyk scanning for your containers in order to see if there are any vulnerabilities in them.

Reference

How to Compile Qt from Source Code on Linux

Beyond Linux® From Scratch (System V Edition) - Qt-5.15.2