Navigate back to the homepage

Get things done with GitLab Runners

Anatoli Nicolae
January 28th, 2020 · 2 min read

Most of projects now have some kind of automation, where it’s building a Docker image or a website using webpack. There are also a lot of free CI/CD solutions that allow you to build your projects, but most of them are limited, hard to figure out or simply not enough. Here’s where Gitlab come in help, providing a solid infrastructure to run your pipelines, using their Gitlab Runners.

What are GitLab Runners?

GitLab Runners are agents that interface with a shell, a Docker daemon, on your server or using their shared runners. That’s right, you can host the Runners yourself!

So what’s the plan?

We want to be able to use, let’s say, a local server running 2 VMs to build our projects. We’re planning to have both Windows and Linux compatibility, so we’ll run a Ubuntu Bionic box as well as a Windows Server 2019 box. Both of them will have Docker on-board, allowing us to use whatever image we want to.

How to install them?

GitLab’s documentation about this is pretty easy to understand and apply on Linux, what we’re gonna dive into is the Windows part which is not that difficult either so here’s a copy-pastable block:

1# Create GitLab runner home
2New-Item -ItemType Directory -Force -Path C:\GitLab-Runner
3
4# Exclude it from antivirus scans
5Add-MpPreference -ExclusionPath C:\GitLab-Runner
6
7# Download gitlab-runner.exe
8(New-Object Net.WebClient).DownloadFile("https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe", "C:\GitLab-Runner\gitlab-runner.exe")
9
10# Register shell executor
11Start-Process -FilePath C:\GitLab-Runner\gitlab-runner.exe -Argumentlist register,"--url https://gitlab.com/","--executor shell"
12
13# Register Docker executor
14Start-Process -FilePath C:\GitLab-Runner\gitlab-runner.exe -Argumentlist register,"--url https://gitlab.com/","--executor docker-windows","--tag-list docker,windows,server-2019","--docker-image mcr.microsoft.com/windows/servercore"
15
16# Install Service
17Start-Process -FilePath C:\GitLab-Runner\gitlab-runner.exe -Argumentlist install
18
19# Add Docker as dependency
20cmd.exe /c "sc config gitlab-runner depend= /"
21cmd.exe /c "sc config gitlab-runner depend= docker"
22
23# Reboot
24Restart-Computer

How to use them now?

To use your freshly created GitLab Runners you’ll simply have to write your own .gitlab-ci.yml, relying on Docker images and run builds inside them, or even using Visual Studio via shell-runners.

The catch to successfully use your brand new runners is to correctly tag them, or your build may fail due to platform or executor incompatibilities. The way we tagged our runners is pretty straight forward, there are some examples of what you could possibly find useful:

  • os (server-2019, bionic, xenial)
  • platform (windows, linux, mac)
  • executor (docker, docker-windows, shell, cmd, powershell)
  • specific apps (visual-studio, sql-server)
  • environment (development, testing, production)

You can now use the tags in your .gitlab-ci.yml as follows:

1job:
2 tags:
3 - bionic
4 - docker
5 - development

or

1job:
2 tags:
3 - windows
4 - visual-studio
5 - sql-server

Conclusion

Even though it requires a bit of configuration and maybe porting your old pipeline definitions over, GitLab CI/CD is a great solution compared to other tools such as Drone CI or Jenkins. You are free to build inside Docker, or build Docker images, thing that can be tricky if your project already runs on Docker (DIND is hard and awful), and everything is easier to understand thanks to pretty straight-forwards .gitlab-ci.ymls.

What do you use? How do you manage multi-platform builds? Is your pipeline clean and easy to catch up on?

More articles from hopefully.online

PowerDNS master-slave cluster

Diving deep into PowerDNS AXFR clusterization: learn how to quickly spin up a three-node cluster with AXFR notifications.

December 29th, 2019 · 2 min read

Hunting down the best home server

Building a home server: hunting down the best and cheapest parts to build a solid NAS/Homelab from scratch.

June 11th, 2019 · 3 min read
© 2019–2020 hopefully.online
Link to $https://twitter.com/HopefullyOnlineLink to $https://github.com/hopefullyonline