Raging Goblin

13 June 2018

Move to Gitlab with a simple Java and Maven project

Filed under: Java — Raging Goblin @ 10:20
Tags: ,

I recently decided to move all my code from Github to Gitlab. Moving the repositories was a breeze due to the import function of Gitlab. There is one single feature however that I am missing from Github. Gitlab does not have a proper release page. It is on the backlog (41766) of Gitlab, but I am afraid that it might take a little while before it lands into Gitlab. Gitlab provides a different mechanism to release your code using a build pipeline based on Docker and using git tags.

For a very simple Java (with Maven) project however, I found this a bit intimidating and therefore I decided to write a short introduction to get you going. This is the simplest of the simplest beginnings and only serves as a starter. But from hereon you can enhance your own pipeline and there are plenty resources on the web to get you moving on. Only this first start was somehow lacking in my opinion.

As said, the build pipeline of Gitlab is based on Docker and to get you going you will need a runner. Luckily when you are on gitlab.com this is already taken care of by a couple of shared runners, so no need to worry about this. To kickoff your pipeline add a file named

.gitlab-ci.yml

to the root of your project. The simplest form of this yaml file can be:

image: maven:3-jdk-8-alpine

maven_build:
   script: "mvn package -B"
   artifacts:
      paths:
      - target/your build jar.jar

When you push this to Gitlab it will automatically kickoff a build for you at every commit.

A couple of things to note here. First, there is a reference to a Maven image. As the pipeline will use Docker, it will start a fresh container where Maven will not be available. So you need to tell the system to use an image that has Maven installed. You can find images at Docker hub. I just picked one with a Java 8 and a Maven 3 installation. The second thing to note is the build job I configured. The name (maven_build) does not matter, just pick one to your liking. The build command should be familiar to any Java developer, I just added the B flag so it will run without interruptions. At the end you will have to configure your artifacts. Otherwise, you will end up with artifacts only containing the sourcecode. You reference the jar which is normally available in the target directory. You can build your Maven project locally first to find out what your artifact will be named.

When you push this file you will find download buttons under CI/CD -> Jobs where you will be able to download your artifacts.

To further suit my needs I use a bit of an enhanced version of this configuration. E.g. I only build when I push a tag, otherwise I have to remove all the intermediate builds to prevent flooding the system with useless binaries. You can find an example here: MiWakeUpLight/.gitlab-ci.yml. Note that it is not possible to filter for a branch AND for a tag, the filtering is an OR filter (though a feature request exists).

For the rest everything is very well documented, but as a simple Java developer I had to adjust my workflow a bit to get me going on Gitlab.

Advertisements

MiWakeUpLight version 1.4 at Gitlab

Filed under: MiWakUpLight — Raging Goblin @ 08:19
Tags: , ,

Due to the recent acquisition of Github, I decided to move my code to Gitlab. MiWakeUplight is now at Gitlab: https://gitlab.com/raginggoblin/MiWakeUpLight.

Blog at WordPress.com.