Configure GitLab Runner

How to Configure GitLab Runner on your own

GitLab is an emerging tool that empowers one of the pillars of DevOps called the CICD Pipeline. Just like we have executors in Jenkins, GitLab has Runners that will act as an application on demand and executes the commands and instructions defined in the GitLab Pipeline scripts (.gitLab-ci.yaml) at the root of the project. Since GitLab is a very popular VCS tool, it is contained to implement CI on Commit to any branch or in the event of Merge Request. So, GitLab Runners can be configured to execute any sort of Project Repository CI Pipeline. In this article, we will discuss How to Configure GitLab CI runner on your own.

How GitLab Runners works?

GitLab Runner is open-source software that can run as binary on your operating system. Gitlab Runner is written in Go language and whichever Operating System that companies Go Language can able to be installed with GitLab Runner. But Most of the Operating System supports GitLab.

  • Basically, the root directory of your repository will have a CICD pipeline file called .gitlab-ci.yml.
  • This will have the Instructions on how the Pipeline should run.
  • And when the event is triggered or any change occurred in the particular branch, It will invoke the Pipeline
  • Then this Pipeline will start the Jobs and these jobs will invoke the GitLab Runners to execute.
  • Then the result of the Job will return from the GitLab Runner.
How GitLab Runners works?
How GitLab Runners works?

Once you install the Runner, you will need to Register it into the GitLab. This will enable communication between GitLab and the Runner. You can install GitLab Runner on Docker Container.

After the Registration, you will need to choose the Executor. Executors are basically giving you an environment to run the Jobs in CICD Pipeline. You can install GitLab runner on any Linux operating system and choose Docker Executor. This will create a Docker Container as Executor.

GitLab Runner Permissions are determined by who is going to access it. That will give us three different GitLab Runners

  • Shared Runners – used for all the projects
  • Group Runners – used for Groups
  • Specific Runners – Used for Specific Projects

We must consider when we are registering the GitLab runner on the GitLab Instance.

Tagging is another useful future where you name the runner with tags and once the GitLab Pipeline script (.gitlab-ci.yml) file got the mentions of the particular tag, the job will use the mentioned executor.

We can configure the GitLab runner in terms of logging and cache settings, memory CPU settings, and more. These settings can be done in the file called config.toml. This will be available after the installation of GitLab Runner. GitLab runners can be monitored using Prometheus.

How to Install GitLab Runner

Just like any other application GitLab Runner can be installed using following ways.

  • GitLab Official Repositories RPM/deb packages
  • Using Binaries
  • Using Containers.

Let’s see how to install GitLab CI Runner one by one.

Installing from GitLab Official Repository Manager

From the official GitLab Repo, it is very easy to install and configure the GitLab Runner, For the same, follow the instaruction.

STEP 1: Run the GitLab Shell Script using CURL to add the Repository.

For Debian/Ubuntu/Mint

$ curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

For RHEL/CentOS/Fedora

$ curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash

STEP 2: Install from the repository Manager.

For Debian/Ubuntu/Mint

$ sudo -E apt-get install gitlab-runner

For RHEL/CentOS/Fedora

$ sudo -E yum install gitlab-runner

Installing the GitLab from Binaries

Installing from Binary is little tricky as it involves little permission works. But, just follow the steps to install GitLab Runner using Binary

STEP 1: Download the Binary

# Linux x86-64
$ sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
# Linux arm
$ sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
# Linux x86
$ sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386"

STEP 2: Assign the permission to the binary

$ sudo chmod +x /usr/local/bin/gitlab-runner

STEP 3: Create User to run GitLab Runner

$ sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

STEP 4: Run the Binary as a service using the above user

$ sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
$ sudo gitlab-runner start

Install GitLab Runner on Docker Container

Running the GitLab Runner is easy and yet suggested best way. To do so, Run the following command

$ docker run -d --name gitlab-runner --restart always \
     -v /srv/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:latest

How to Configure GitLab Instance for the Runner.

To get GitLab Runner Configured on the GitLab Instance, you need to get the configuration details that can be added to the runner for authentication. To do so,

  • Go to GitLab instance and select any of the projects you want to configure.
  • Then in the Settings, Click CICD Then in the list of CICD Settings Expand Runner.
  • There you can see, Specific Runner and Shared Runner in the list.
  • Shared Runners are provided by GitLab.com which is cloud-based autoscale-able Runners.
  • But we need a Specific Runner, In which Look for the “Set Up a Runner Manually
GitLab Runner Configuration
GitLab Runner Configuration

Then just copy the URL and Registration token and keep it with you for the next process.

How to Configure GitLab Runner

You just need to run register command from the binary or the executor you just installed on your operating system.

Example, if you are running the GitLab Runner on linux, just run

$ sudo gitlab-runner register

Or if it is Docker Container, Jun this command on the host

$ docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

This will prompt you some questions and just give the answers to register yourself.

Or, to register as one line, use the following command or parameters.

$ sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "<Give you token from GitLab>" \
  --executor "docker" \
  --docker-image alpine:latest \
  --description "docker-runner" \
  --tag-list "docker, CICD" \
  --run-untagged="true" \
  --locked="false" \
  --access-level="not_protected"

In the above Just use the Copied URL and Registration token we saw in the Setting page of GitLab Instance.

That’s all, this will Register your GitLab Runner to the GitLab Instance.

Conclusion.

Nowadays, making the process simpler and yet powerful is the ultimate goal of matured automation. In that, we the Automating the CICD Pipeline in GitLab is very easy with GitLab Runners. In this article, we discussed How to Configure the GitLab Runner on your own. So, our next article will discuss how to create a CICD pipeline in GitLab. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

Leave a Reply