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:
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:
---
- name: Install KVM
hosts: kvmserver
become: true
roles:
- role: yasarlaro.kvmOnce 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.
Now it is time to install the image so each new build request will use that base image:
Please note that the image disk to be used is gitlab-runner-centos7.qcow2.
Install gitlab-runner binary
To install GitLab Runner:
Add the official GitLab repository:
2. Install Binary
Register Gitlab runner
Now it is time to register the runner and configure it.
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?