How to install custom Gitlab runners with libvirt executor

Introduction

I have been recently discovering Gitlab CI and quite impressed by its capabilities. While going through Gitlab CI's, I wonder how can I run my pipelines at my lab. It would give me better build capabilities as some of my tests require VMs rather than containers. Then I started diving into on-premise Gitlab runners and managed to install a KVM based runner in my lab. Let me share my work:

I have been working with Gitlab CI only for a day while writing that article and there will be a lot to discover. Feel free to comment if there is a better way to handle a task than how I handled it

Please refer to official documentation as a reference if you face any issues. I also used the official documentation page to write the below post.

Prerequisites

We need to have KVM installed on the server that we will be using as a Gitlab runner. That server will provision VMs based on your pipeline in gitlab-ci.yml. If your server does not have KVM installed, you can use my Ansible role to install it quickly. Let me give you an example of how to use it:

You need to install the role first on your server where you will trigger ansible. You can install the role by ansible-galaxy role install yasarlaro.kvm command.

---
- name: Install KVM
  hosts: kvmserver
  become: true
  roles:
    - role: yasarlaro.kvm

Once the KVM is installed, we need to create a base image to be consumed by Gitlab CI. I prefer to use CentOS 7 but you can choose a different one based on your project.

You will hit an error with your e2fsck version. You will need to download the latest source binary of e2fsprogs from your OS distributor and install it.

[  17.0] Resizing (using virt-resize) to expand the disk to 8.0G
virt-resize: error: libguestfs error: resize2fs: e2fsck 1.42.9 
(28-Dec-2013)
/dev/sda1 has unsupported feature(s): metadata_csum
e2fsck: Get a newer version of e2fsck!

If reporting bugs, run virt-resize with debugging enabled and include the 
complete output:

To install it from on CentOS 7:

$ wget https://mirrors.edge.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v1.45.6/
$ tar -xf e2fsprogs-1.45.6.tar.xz
$ cd e2fsprogs-1.45.6
$ ./configure
$ make
$ make install

You will also need to install libguestfs-xfs on CentOS 7 to handle XFS file system with virt-builder utility. Otherwise you will hit below error:

Now it is time to install the image so each new build request will use that base image:

To see available operating systems for virt-builder utility: osinfo-query os command.

Please note that the image disk to be used is gitlab-runner-centos7.qcow2.

Install gitlab-runner binary

To install GitLab Runner:

  1. Add the official GitLab repository:

2. Install Binary

Register Gitlab runner

Now it is time to register the runner and configure it.

There is an option to use a template configuration file which will make it easier but I will edit the post later once I test it properly

Configure Gitlab Runner

Now we need to tell Github runner how to spin up a VM once needed and how to terminate it once it is not needed anymore.

First thing is to edit config.toml

As you can see here, we need some scripts to be placed under /opt/libvirt-driver path:

Let me also provide you the content of those scripts below:

Then we will need to restart the service so the new configuration can be loaded

How to use custom Gitlab runner

Now you should be able to see your custom runner registered under Gitlab Project --> Settings --> CI / CD

You can write a simple gitlab-ci.yml and push it to test your build:

Last updated

Was this helpful?