Installing and Configuring GitLab Runners
local graph
Chapter 15 — Installing and Configuring GitLab Runners
Section titled “Chapter 15 — Installing and Configuring GitLab Runners”In this chapter, we will take a look at the GitLab Runner client architecture. After studying this topic, you will understand the basic flow of control between GitLab CI and Runners. In the second part of this chapter, I will show you how to install Runner software on different operating systems. Since the Runner program is written in Golang, there are many platforms that are capable of running the client. Golang is known for its excellent multi-platform support.
In this chapter, we will cover the following topics:
- The Runner client architecture
- Creating a basic runner with the shell executor
To follow along with the instructions in this chapter, please download the Git repository with the examples available from GitHub: https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter15.
The other requirements for this chapter are as follows:
- GitLab Runner client – Linux 64-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
- Linux 32-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
- Linux ARM: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
- macOS 64-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
- FreeBSD 64-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-freebsd-amd64
- FreeBSD 32-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-freebsd-386
- Windows 32-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-386.exe
- Windows 64-bit: https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe
- cURL: https://curl.haxx.se/download.html
We took a brief look at the GitLab architecture in Chapter 1, Introducing the GitLab Architecture. It was explained that a GitLab Runner registers with a GitLab instance and waits for a job to execute. Unlike the individual components that are a part of the GitLab frontend application server, the runner has a very straightforward architecture. The communication between a runner and its GitLab host is basically one-way.
The main network communication is from the GitLab Runner to the GitLab CI, never the other way around. This is shown in the following diagram:

The behavior is best depicted by a sequence diagram like the following one:

When the GitLab Runner starts, it tries to find its coordinator by contacting the GitLab URL. When it registers itself with the registration token, it gets a special token to connect to GitLab. After a restart, it connects and waits for a job from GitLab CI. It polls GitLab in intervals, and when there is nothing to do, it will check GitLab less often to prohibit too much network traffic.
When a job is queued in GitLab CI, it will try to find an available runner. After receiving the command, it will clone the project-specific commit that triggered the job and will execute the steps defined in the .gitlab-ci.yml file. After execution, the results are sent back to GitLab.
GitLab CI has two types of runners:
- Specific Runners: As a developer, you can create your own runners and register them to a project in GitLab. Only then is the project visible to the runner.
- Shared Runners: A GitLab administrator can also designate a runner to be shared. It can then pick up jobs from several projects. Because this is potentially classed as a security breach, be careful when you’re promoting runners to be shared.
In the following screenshot, you can see the CI/CD configuration of a project in GitLab, where you can set which runner to use:

The GitLab Runner clones the repository and performs steps that are defined in the .gitlab-ci.yml file. It is possible to inject special variables that can be protected on the GitLab project level:

We’ve explained the basic architecture of the runner platform, so now we’ll install the software on different operating systems.
The GitLab Runner software is available for the following operating systems:
- Linux
- FreeBSD
- macOS
- Windows
The installation procedure is largely identical on all systems, with subtle differences. In the next section, we will show you how to install the basic Runner on several types of operating systems.
On the machine where you install the GitLab Runner software, you can run the shell executor to locally build software. This shell type of executor can be run on all platforms where the runner software is installed. Therefore, you can run Bash or a Bourne shell on a Unix-like system, or CMD or PowerShell on the Windows platform.
It isn’t a very safe way to build because it can access local resources on the system where the runner executes. More secure executors will be introduced in later chapters.
In the next section, we will demonstrate how to install the GitLab Runner software for your platform.
If you run a Linux distribution with a package management system such as
yum or apt, you can install the GitLab Runner package using that method. Alternatively, you can install the software manually. First, we will cover the installation via a package manager.
For yum-based systems, you can add the official GitLab package repository:
curl -o script.rpm.sh https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh less script.rpm.sh #(check the contents, if you are fine with it make it executable and run it) chmod +x script.rpm.sh ./script.rpm.shAfter that, you can install GitLab with this basic command (as root or with sudo):
yum install gitlab-runnerFor apt-based systems, it’s a bit more tricky to stay up to date with the latest and greatest. We can add a link to GitLab apt repositories, but unfortunately, Debian has named the package in the base repository the same as GitLab. This means that the base packages automatically take precedence. A solution to this is to pin the package to the right repository. This can be done by adding a file to /etc/apt/preferences.d:
cat <<EOF >> /etc/apt/preferences.d/pin-gitlab-runner.pref Explanation: Pin GitLab-runner package Package: gitlab-runner Pin: origin packages.gitlab.com Pin-Priority: 999 EOFAfter that, you can install the correct apt package repositories:
curl -o script.deb.sh https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh less script.deb.sh #(check the contents, if you are fine with it make it executable and run it) chmod +x script.deb.sh ./script.deb.shThe output from the preceding command is shown in the following code block. As you can see, it checks the gpg key and adds the package repository:
Detected operating system as debian/stretch. Checking for curl... Detected curl... Checking for gpg... Detected gpg... Running apt-get update... done. Installing debian-archive-keyring which is needed for installing apt-transport-https on many Debian systems. Installing apt-transport-https... done. Installing /etc/apt/sources.list.d/runner_gitlab-runner.list...done. Importing packagecloud gpg key... done. Running apt-get update... done.
The repository is setup! You can now install packages.The next step is much simpler – you just install it via apt-get:
apt-get install gitlab-runnerIn the output, you will find a message like the following, which means that you have now installed all the binaries. However, you have to register the GitLab Runner first before running it:
... gitlab-runner: Service is not running. ...The process of registering the runner is explained in more detail in the Registering a runner section.
Updating the package is done just as updating all the other packages on the system is done: you can run the apt-get update or yum update command.
When installing with apt-get or yum, package management will give you a GitLab Runner install with the necessary configuration files and init scripts for your Linux distribution. You can also choose to just download the runner binary and run it in a generic way.
Simply download one of the binaries for your CPU architecture (x86-64, x86-32, or ARM):
- x86-64 bit architecture:
curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64- x86-32 bit architecture:
curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386- ARM architecture: This is the binary for the ARM CPU architecture, in a list:
curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-armWhen you’re getting binaries this way, you have to make them executable:
chmod +x /usr/local/bin/gitlab-runnerThen, you can create a GitLab Runner user as root:
useradd --create-home gitlab-runnerYou then need to install it and run it as a service:
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runnergitlab-runner startAlthough the manual installation is a bit more work than via the package managers, it still isn’t a complex process. One advantage is that, when using the manual installation, you can’t install newer versions of the runner. The package manager maintainers will never install a development version, while you can. Updating the binary is not very hard either.
The process we are following when updating is to replace the Golang binary that was downloaded previously. It does have to be stopped, so make sure it isn’t running, otherwise the installation will fail.
Stop the service (you need root permissions, like before):
gitlab-runner stopThen, download a new binary to replace the older one:
curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386 curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64Set the execute bit on the runner binary:
chmod +x /usr/local/bin/gitlab-runnerStart the runner again:
gitlab-runner startThe manual install is much easier, but you have to manage the updates yourself. In this era of automation, it makes more sense to let the package manager that’s available for your distribution to manage it.
Just like on Linux, there are several ways to install the GitLab Runner software. Unlike on Linux, where package management is recommended by GitLab, for macOS, they recommend the manual install. The other way to install is by using the Homebrew installation method, which you saw earlier in the book (Installing Redis section of Chapter 1, Introducing the GitLab Architecture).
First, get the binary for your system (with sudo):
sudo curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64Then, just like we did previously, make the binary executable:
$ sudo chmod +x /usr/local/bin/gitlab-runnerNow that we have the binary in place, we can run the program as another user if we want:
$ cd ~$ gitlab-runner install$ gitlab-runner startThe runner will be installed and will be run after a system reboot.
A unified package manager for macOS doesn’t really exist, but the one that’s used the most is Homebrew, which can be found at https://brew.sh/. It works with formulas that contain scripts and settings to install binaries.
A Homebrew formula exists to install GitLab Runner:
brew install gitlab-runnerThe next step is to install the runner as a service (this will also start it):
brew services start gitlab-runnerThere are some drawbacks to using macOS as a runner platform. Many developers use runners on macOS to build iOS-related software. Often, UI testing is also involved. It isn’t possible to automate this. You would have to run in the background as a system service (LaunchDaemon), and then the UI isn’t reachable anymore. You can only run the runner in user mode to get access to the UI, which is why you must always log in to run the GitLab Runner.
Updating the runner is done by issuing brew upgrade gitlab-runner. For a manual install, it’s a bit more complicated.
Just like we did for Linux, we are replacing the Golang binary that was downloaded previously. It also needs to be stopped, so make sure it isn’t running, otherwise the installation will fail:
- First, we need to stop the service:
gitlab-runner stop- Like we did previously, get the binary to replace the runner’s executable:
curl -o /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64- Make the downloaded binary executable:
chmod +x /usr/local/bin/gitlab-runner- Start the GitLab Runner service again:
gitlab-runner startThe steps to install the GitLab Runner software are roughly the same on the Linux and macOS platforms. The good part about using a package manager is that the software is easier to upgrade.
Unlike GitLab itself, which doesn’t run on Windows, you can operate the GitLab Runner software on Windows machines.
There are two types of runner binaries, depending on your CPU architecture:
- 32-bit version (https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-386.exe)
- 64-bit version (https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-amd64.exe)
Download it and copy it to gitlab-runner.exe in a folder on your local drive; for example, C:-runner.
Now, you need an elevated command prompt to register and install the software:
c:\cd c:\gitlab-runnerc:\gitlab-runner\gitlab-runner.exe registerThe registration steps will be shown in the next section as they are almost universal for all platforms.
When registration succeeds, you can start the GitLab Runner:
c:\gitlab-runner\gitlab-runner.exe installc:\gitlab-runner\gitlab-runner.exe startNow that we have installed the runner software, the next step is to register with GitLab.
A GitLab Runner needs some basic information to start:
- The URL where it can find GitLab, which is called the coordinator URL.
- The special token that you can find in GitLab that’s meant to register runner instances.
- A description that will later show up in the GitLab CI.
- Tags, which you can give to the runner to make it easier to find in GitLab CI.
- The type of executor (remember that there are many types, all of which were named in the Runner client architecture section).
This basic information can be provided to the runner in two ways: interactive and non-interactive. First, we will discuss the interactive way.
The web location of the GitLab server to connect is the following:
sudo gitlab-runner registerPlease enter the gitlab-ci coordinator URL (for example, https://gitlab.com):
https://gitlab-ee.joustie.comYou also need to enter the runner registration token since it’s needed to register with GitLab:
Please enter the gitlab-ci token for this runner xxxThe runner registration token can be found on the Admin Area | Overview | Runners page or under Projects.
You want a nice description so that you can find the runner later (the default is hostname):
Please enter the gitlab-ci description for this runner [hostname] my-runnerEnter the tags that should apply to this runner (for example, javarunner). This can be changed later in GitLab:
Please enter the gitlab-ci tags for this runner (comma separated): javarunner,another-javarunnerThe most important part is determining the type of executor (for this chapter, I chose the shell executor):
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell: shellIn larger environments, deployments are frequently scripted. So, for the runner, there is also a non-interactive install available to help in the effort of automating the infrastructure. You can specify subcommands to the GitLab register argument. To find out about these options, type the following on the command line:
gitlab-runner register -hTo register a runner, using the most common options, you would do the following:
sudo gitlab-runner register \ --description "docker-runner" \ --url "https://gitlab-ee.joustie.com/" \ --registration-token "xxxx" \ --executor "docker" \ --docker-image alpine:latest \ --non-interactive \ --tag-list "docker,aws" \ --run-untagged="true" \ --locked="false" \When the runner is registered, it will show up in GitLab in the Runner list:

The registration process is basically the process of telling the runner where GitLab is situated, and initiating a key exchange to secure access to GitLab. Following a successful registration, the runner saves the configuration information in a TOML file, which looks as follows:
concurrent = 1 check_interval = 0
[[runners]] name = "runnerhost.joustie.nl" url = "https://gitlab-ee.joustie.nl" token = "801bd1f41a3bb7a42c0b6f43e9ffc8" executor = "shell" shell = "bash" [runners.cache]The shell can also be set to sh for a simple Bourne shell or powershell on Windows. On Linux, this is usually placed in /etc/gitlab-runner/config.toml.
If you are feeling adventurous, you could also install the latest release, which isn’t part of any release yet. Do this at your own risk. There could still be bugs in there:
- Linux:
- macOS:
- Windows:
- FreeBSD:
Now, we should have a runner installed and ready for use.
The easiest way to install a runner is by using a package manager running on the host operating system. Doing a manual install allows you to run development versions or patched versions easily because it has only one Golang binary.
In this chapter, we showed you the basic architecture of a GitLab Runner. Then, we showed you how to install it on several operating systems with a shell executor. On most systems, there is a more or less automated way to do this, which also manages updates and platform compatibility. There’s also a manual way of installing the software on every system. Using the manual method allows you to run developer versions of the runner easily. The registration process of the runner can be done step by step or in one command.
In the next chapter, we will deploy GitLab Runner in a Docker container and also in a more managed way in a Kubernetes cluster.
- What part of GitLab does a runner connect to?
- What additional action has to be performed on Debian-based systems to install the right package?
- In what language is the runner client written?
- What is the default description that’s given to a runner?
- What command argument is used to communicate the registration token to the gitlab register command?
- Hands-On Full Stack Development with Go, by Mina Andrawos:
- Windows 10 for Enterprise Administrators, by Jeff Stokes, Manuel Singer, and Richard Diver:
- Hands-On Continuous Integration and Delivery, by Jean-Marcel Belmont: