Gitlab and continuous integration

This guide is for setting up a CI environment using gitlab, jenkins and sonarqube.

Databases will be managed by a postgres container (official how to use an external database with gitlab).

Initial gitlab configuration

Create gitlab users

If you disabled signup, users need to be created by an admin. Gitlab doesn't allow you to set an initial password on the user creation page, because it wants to send an email with a reset link to the user. If the user doesn't have an email account, or you don't have a mail server, after you created the user you need to click on "edit" and you will be able to set an initial password.

Initial jenkins configuration

Connect jenkins to gitlab

Enforce jenkins security with roles

By installing roles we will disable anonymous access and make a difference between admins (allowed to do anything) and users (allowed to manage jobs but not the global jenkins configuration).

Create a jenkins user

You must have a gitlab project set up.

This needs to be done for every project, it can be done by any user that can configure the jenkins job.

Initial Sonar configuration

Add sonar to a jenkins job

You must have a jenkins job set up.

sonar.projectKey = {whatever, will be the project ID (seen in URLs, etc)}
sonar.projectName = {whatever}
sonar.projectVersion = {whatever}
sonar.sources = /var/jenkins_home/workspace/$JOB_NAME/{the source folder of your project structure}

Install sonar plugin

New plugins can allow to analyze more languages.

Launch docker commands in jenkins

One you got you jenkins setup, you may want to be able to launch docker commands in jenkins jobs. We will do it by replacing the jenkins image with one containing the docker client, and giving access to the host's docker daemon to jenkins container.

Security warning: since jenkins will have access to docker, everyone with job configuration access can potentially mount your entire host filesystem inside a container and modify it. This can be a big security concern. But for a simple personal install it will be probably fine.

The image switch will not affect your data if you already have a docker install: since data should be in a jenkins_home volume, it is not affected by the image switch.

Update the image

The image will not be automatically rebuilt. To update it launch docker-compose up -d --build jenkins.

Note on mounting volumes

If in your jenkins build you run a command that needs to mount a folder inside a container you may run into a problem. Suppose you want to mount the src folder of your project: jenkins will run something like docker run -v /var/jenkins_home/workspace/project_name/src/:/source_folder. but the docker command is executed on the host, which doesn't have a /var/jenkins_home directory!

To overcome that, instead of using a named jenkins_home volume mounted inside the jenkins container at /var/jenkins_home/, you need to create a /var/jenkins_home folder on the host and mount that instead.