Skip to content

Instantly share code, notes, and snippets.

@techyugadi
Last active May 3, 2020 01:50
Show Gist options
  • Save techyugadi/abbb291f3f47cedb525486d90d8ea979 to your computer and use it in GitHub Desktop.
Save techyugadi/abbb291f3f47cedb525486d90d8ea979 to your computer and use it in GitHub Desktop.

Continuous Integration with Gitlab and Jenkins: Gitlab Setup

This is the first part of the three-part gist for setting up a basic CI environment on a single machine (e.g., a laptop), for dev / test / proof-of-concept, etc.
This CI environment consists of:

  1. Gitlab (Community Edition) as a source-code repository
  2. Jenkins for build and continuous integration Gitlab cannot be used without a mail server (Postfix). So, we also set up Postfix.

Gist Breakup

This part of the three-part gist outlines the steps to set up and configure Postfix and Gitlab CE. Here we explain:

  • Installation and Configuration of Postfix
  • Installaion and Configuration of Gitlab CE
  • Creation of Users and Accessing Gitlab through HTTP and SSH

For reference, please follow the three links below:

Platform / Environment

Here we describe the setup steps on Ubuntu 19.10 platform (Ubuntu Desktop), allocated 5GB RAM and 4 CPUs.(We actually set this up on a Ubuntu Virtual Machine on VirtualBox, running on Windows.)
A bash-like shell will be needed to run commands for the set-up.

The setup on other Linux flavors will be very similar.

Note: In our setup, the machine name (hostname) was archlab and the OS user running the commands was techie, included in the sudoers list. You will find these two string tokens in the rest of the gist. You should be able to replace these with suitable values for your environment.

Software Version

In this set-up, the following versions have been used:
Gitlab: Community Edition 12.9.3
Jenkins: 2.222.1

Postfix Setup

Gitlab sends notifications related to source code repositories, through email. On a simple (single-machine) dev-test setup, we may not need these notifications badly. But, there is one notification that is absolutely essential. When a new user is created, Gitlab sends a unique URL through email, for her to access the repository first time and set her password. For this reason, we need, at least, a local mail server on our machine, with a local mail inbox.

Gitlab CE installation, specifically requires Postfix as the mail server. So our first step is to setup Postfix, in local-only mode. That is, it is accessible only from the local machine, and uses the local file system for storing emails.

Install Postfix

Check that the following two entries are present in your /etc/hosts file by running cat /etc/hosts:

127.0.0.1	localhost
127.0.1.1	<your_hostname>
  • If not, insert the missing entries by editing the file (in sudo mode)

Run the command:
sudo apt install postfix

During installation, a screen will pop-up with option to select 'General Type of Mail Configuration' like 'No Configuration', 'Internet Site', 'Local Only', etc.

  • Choose 'Local Only'

Another screen will pop up prompting for 'Domain Name'. Actually on a single machine installation in local only mode, a default input will be shown. The _default_input happens to be the hostname, e.g., 'archlab'.

  • Select the default (or type in the hostname only, if it is not the default)

Complete the postfix installation. You may observe output like the following:

Setting up postfix (3.4.5-1ubuntu1) ...
...
Adding system user `postfix' (UID 126) ...
...
setting myhostname: archlab
...
setting destinations: $myhostname, archlab, localhost.localdomain, localhost
setting relayhost: 
...
Postfix (main.cf) is now set up with a default configuration.  If you need to 
make changes, edit /etc/postfix/main.cf ...

We will configure Postfix shortly, by updating /etc/postfix/main.cf.

Check that Postfix is up and running

Run the command: sudo systemctl status postfix

The output should indicate: postfix.service - Postfix Mail Transport Agent, and Active: active (exited) since ...

Install a couple of tools that will be needed to further test postfix: sudo apt install net-tools mailutils

Postfix runs on port 25 (by default). So, after installation of net-tools, we can run: netstat -tuplen | grep 25 | grep tcp
The output should include: tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 24790 -

The tool mailutils will be used to send some emails within the local machine.

Configure Postfix

First check that the following entries exist in the Postfix configuration file /etc/postfix/main.cf:

myhostname = <your_machine_name>
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, <your_machine_name>, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

This verifies that Postfix has been set up in local only mode. If necessary, edit the file with appropriate entries as above.

Restrict mails to be sent and received within the local machine only.
Edit the configuration file /etc/postfix/master.cf to reflect the following:

smtp      unix  -       -       y       -       -       local
relay     unix  -       -       y       -       -       local

Note the entry in the last column. By default it is set to smtp. We have to modify it to local.

Create a virtual alias map:
Create a file /etc/postfix/virtual, and add the following line:

/.*/	<your_OS_user>

That means, for the purposes of this set-up, all emails will be delivered to the mailbox belonging to <your_OS_user>. This suits our purpose, since we only need to check a few notifications like URL to reset user passwords, etc.
Obviously in production, there will be a standalone SMTP-enabled Postfix installation with dedicated user accounts and mailboxes.

Run the following command to create a virtual alias database (i.e., a file virtual.db) based on the above alias map:
sudo postmap /etc/postfix/virtual

Create a mailbox for <your_OS_user> in the local file system. Ensure that Postfix has permissions to access it.

mkdir ~/.mail
chmod 775 .mail/
sudo chgrp postfix ~/.mail

Update Postfix configuration to use the virtual alias and the corresponding mailbox. Add the following line to the end of the file /etc/postfix/main.cf:

virtual_alias_maps = regexp:/etc/postfix/virtual
home_mailbox = .mail/
mailbox_command =

Restart Postfix for the configuration changes to take effect:
sudo systemctl restart postfix

Test Postfix Functionality

In one terminal, type the following command: sudo tail -f /var/log/mail.log

In another terminal, send a test mail: echo "message" | mail -s "subject" <your_OS_user>@localhost

Check that log messages like the following are generated (on the other terminal):

Apr 17 06:29:47 archlab postfix/qmgr[23353]: 9199FC30BA: from=<your_OS_user@your_machine_name>, size=321, nrcpt=1 (queue active)
...
...
Apr 17 06:29:47 archlab postfix/local[23418]: 9199FC30BA: to=<your_OS_user@your_machine_name>, orig_to=<your_OS_user@your_machine_name>, relay=local, delay=0.05, delays=0.03/0/0/0.02, dsn=2.0.0, status=sent (delivered to maildir)

Now check your mailbox: ls -al ~/.mail/new/. There should be a file containing the mail contents.
Read the file: cat filename. The contents will be similar to the following:

Return-Path: <your_OS_user@your_machine_name>
X-Original-To: <your_OS_user>@localhost
Delivered-To: <your_OS_user@your_machine_name>
Received: by <your_machine_name> (Postfix, from userid 1000)
	...
Subject: subject
To: <your_OS_user@your_machine_name>
X-Mailer: mail (GNU Mailutils 3.6)
Message-Id: ...
Date: ...
From: your_OS_user <your_OS_user@your_machine_name>

message

For the purposes of this set-up, Postfix installation and configuration is complete.

Install Gitlab Community Edition (CE)

Install the following pre-requisite packages:
sudo apt curl openssh-server ca-certificates

There is a readily-available script that configures the Ubuntu package repos before installing Gitlab CE. Run the script:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
Some output messages are shown below:

Detected operating system as Ubuntu/eoan.
...
Running apt-get update... done.

The repository is setup! You can now install packages.

Troubleshooting: If you actually try to install Gitlab CE now, by running sudo apt install gitlab-ce, you may come across an error:
E: Unable to locate package gitlab-ce
Edit the file: /etc/apt/sources.list.d/gitlab_gitlab-ce.list
Modify the two lines:

deb https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ eoan main
deb-src https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ eoan main

to reflect the following:

deb https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ bionic main
deb-src https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu/ bionic main

And run: sudo apt update

Install Gitlab CE: Now run sudo apt install gitlab-ce
Output messages similar to the following will be seen:

...
Setting up gitlab-ce (12.9.3-ce.0) ...
It looks like GitLab has not been configured yet; skipping the upgrade script.
...
Thank you for installing GitLab!
...

Next Steps

Next we will create a few users on Gitlab, and also set up Jenkins (see second part of the gist).

References

Postfix documentation
Gitlab CE documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment