Complete continuous integration with Gitlab.com

Introduction

Let me first give you some context of the world I was coming from. I had a Student Developer Pack (SDP) from Github.com. It included private repositories and a subscription to Travis CI for a whole year! That was great, because I wanted to work on my projects in private. But, after a year, my subscription ended and I couldn’t access any repository anymore. So I needed a (free) alternative solution. There is where Gitlab kicked in, but first let me describe my old situation.

Setup with Travis CI

So to test my repositories after I pushed to it, I’ve added a .travis.yml file where you can describe the phases you want to run. Without diving too deep into the details, I’ll give you some information about the application:

  • Spring boot
  • JDK 8
  • Needed to be dockerized for easy deployment

My .travis.yml looked like the following:

It will set the environment to use JDK8. You’ll have to setup the $DOCKER_EMAIL, $DOCKER_USERNAME and $DOCKER_PASSWORD variables in your Travis CI project. Ofcourse, I’m not revealing those :). I’ll describe short what this script does:

Is quite self explanatory.

It will execute this script using the Oracle JDK 8. You can also specify more JDK’s. Travis will pick that up and makes a build job for each of those JDK’s.

I’m caching the gradle directory so I wouldn’t have to initialize it each time my job ends.

Make sure I will get the docker command available during this build.

Before running a single script, make sure we are logged into the Docker registry with the provided variables.

And finally we build the application using the gradle build command. This actually gets the needed dependencies, compile and run all tests. After that succeeds, we build the docker container. And finally we push the docker container into the registry.

Alternative with the same functionality

As I already stated, my student developer pack ended and I needed an alternative. So I looked around for a while when I stumbled upon this blog post.

Gitlab to the rescue!

In order to get the project started with builds, all you have to do is enabling container registry on your project and putting a .gitlab-ci.yml file in the root of your project. Enabling your project for the container registry is easy. All you have to do is:

Select your project and hit edit project

Settings page on the repo on Gitlab

Check the Container Registry checkbox

Enabling Gitlab container registry

Last step is configuring your .gitlab-ci.yml file

Again, I will explain piece by piece what this does.

You can define as many stages as you want. These stages will all get executed.

We are defining a variable and give that a name to push it to the container registry.

This is the build stage I defined earlier. You can see this as the implementation of that stage. The image field is important. We are giving it a Docker image to execute the commands we want it to run. Actually this is almost equivalent to the Travis CI environment, but you can pick any container that is in the official Docker registry to get that as a runtime or build environment. Furthermore I’m caching the result of the build because my next stage needs that cached directory.

And last but not least I’m building the Docker container and push it to the (Gitlab.com’s) registry. So this is not the official Docker registry. Please note that there are some variables that get set by the Gitlab runners itself. Like the $CI_BUILD_TOKEN. I do not have to provide any credentials because Gitlab handles that for me. How convenient!