Setting Up Your Project for GitLab Continuous Integration
local graph
Chapter 14 — Setting Up Your Project for GitLab Continuous Integration
Section titled “Chapter 14 — Setting Up Your Project for GitLab Continuous Integration”Continuous Integration (CI) is one of the most important pillars of Extreme Programming (XP). Continuous Integration has been one of GitLab’s most popular features since it was built in version 8. It is very popular with independent developers and open source projects and is currently gaining popularity in other market segments.
Getting started is easy. As we showed you earlier, in Chapter 10, Create Your Product, Verify, and Package it (in the Release and Configure sections), Auto DevOps is switched on by default, so that when adding code to a project, a deployment pipeline is automatically set up in which various jobs are running. These jobs will be run by a GitLab Runner, which you will have to set up. This is completely configurable to the wishes of the developer. The results of the jobs are collected and showed as passed or failed and are part of the logic in the pipeline. Based on the result, other automation in the pipeline can be triggered**.** The basis for this functionality is in the .gitlab-ci.yml file. If this file is present in a project, it will be parsed and different pipelines and jobs will start running.
In this chapter, we will be covering the following topics:
- Pipelines
- Jobs
- Creating .gitlab-ci.yml
- Configuring a Runner
To follow along with the instructions in this chapter, please download the Git repository with the examples available at GitHub: https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter14.
GitLab CI must be enabled in your GitLab instance (see Chapter 3, Configuring GitLab Using the Web UI).
A pipeline in the software engineering world is understood as a chain of events (of processes, components, and so on) that automatically triggers and delivers input to the next element. It resembles the physical pipelines that exist in the real world.
In a CI context, a pipeline is a collection of sequential steps that integrate code from different developers. The chain of events is triggered by a commit or push to a source code repository such as GitLab. The build system (for example, Jenkins or GitLab CI) is notified of a new version, compile, and source code and runs unit testing.
Before we go any further, you should understand that without unit tests, or other automated tests, the effort of integrating pieces of code by different developers is very difficult. So, before you begin building pipelines, make sure that your developers have written test code. That way, you can make sure that the code is checked and at least adheres to certain quality checks.
If the unit tests succeed in your pipeline, the next step is to run integration tests. If they also succeed, the artefact that was built can be pushed or saved in a binary repository, or it can be directly deployed to a staging environment where the code will run.
Some pipelines can even be deployed to an acceptance or semi-production environment, where user validation of the solution built takes place. This is called Continuous Deployment (CD). Some call this Deployment to Production CD, but it depends on your definition of the concept.
Modern build software has the concept of building pipelines as part of their architecture. Products such as Jenkins have adopted it in their workflow, but cloud solutions, such as Azure DevOps (which is TFS in the cloud), have too.
You can find pipelines in GitLab for your project in the left-hand menu bar, under CI/CD, as shown in the following screenshot:

The first item in the menu is a link to an overview of your Pipelines. Here, you can see whether pipelines have passed or failed, retry pipelines, or download artifacts, as shown in the following screenshot:

The second link in the menu takes you to the list of Jobs for a project (which we will cover shortly), as shown in the following screenshot:

The third link is a feature of GitLab CI called Schedules. As you can see in the following screenshot, we have created a schedule for a run of the pipeline to be executed for the Eventmanager project:

If you click an item in the schedule list, you will see that there are many configurable items in it. For example, you can specify which branch to run on, as shown in the following screenshot:

A nice way of discovering more about your build is the chart feature in GitLab CI, where insight into the metrics of your pipelines is provided, as shown in the following screenshot:

The following screenshot shows a high-level design of a pipeline that runs in GitLab:

We have now demonstrated that GitLab CI incorporates the general concept of a build pipeline and also records several metrics about the successes or failures of the pipeline’s steps. In the following section, we will take a look at these individual steps.
Pipeline configuration begins with jobs:
- Jobs are the most fundamental element of a pipeline and are executed by GitLab Runners
- Jobs are created with constraints, which govern under what conditions they should be executed
- Jobs are top-level elements that can have an arbitrary name and must contain the script element as a minimum requirement
- There can be an unlimited number of jobs
In the pipeline overview, you’ll find several jobs. They have a status, an ID, are part of a stage, and have a name, as shown in the following screenshot:

You can create jobs by adding them to a configuration file called .gitlab-ci.yml. We will discuss this file in more depth in the following section.
An example of a pipeline containing two jobs is as follows:
job1: script: "execute-this-script-for-job1"job2: script: "execute-this-script-for-job2"The preceding example is a basic CI/CD pipeline that consists of two named jobs (job1 and job2), which execute a script section (in our example, this does nothing). In the script section, you can specify a command, script, or a chain of commands. For instance, to build a JavaScript, you can set script as npm build or run a shell unit test called unit-test.sh.
Jobs are not run on the GitLab application server, but are picked up by GitLab Runners. The runners execute the jobs in their own environment; for example, runner A can build job1 and runner B can build job2. Note that the jobs are independent from each other. The results from these jobs are aggregated on the GitLab server.
How GitLab CI interacts with your profile is largely controlled by the .gitlab-ci.yml file, which must be added to the root of your project. When you push code to your repository, GitLab will test whether it’s there and start a pipeline with jobs for that specific commit.
The format of the file is YAML Ain’t Markup Language (YAML). YAML is currently a widely used format for configuration files and is best described as a data serialization language.
We have already given you an example of a .gitlab-ci.yml file with two jobs in an earlier section. In other chapters, we used a .gitlab-ci.yml file to describe a deployment. So, what are the available possibilities of this file?
A lot of combinations are possible, but the fundamental way of working is to first define stages in the file and then add script sections. The complete reference of this file can be found here: https://docs.gitlab.com/ee/ci/yaml/README.html.
When you are creating your YAML file, it is nice to know that, after saving, it will be checked by a linter to verify the syntax of the file, as shown in the following screenshot:

The possibilities for this file are endless, so the best way to figure out what works for you is to look at open source projects on the GitLab website for inspiration, available at: https://docs.gitlab.com/ee/ci/examples/README.html.
We briefly described the concept of GitLab Runners in Chapter 1, Introducing the GitLab Architecture. Runners are essentially build environments that run on a separate machine that connects to the GitLab application’s server and asks jobs to execute. Runners help to automate product development and achieve DevOps integration.
We configure a runner on the GitLab side, and on the GitLab Runner client side. Remember that there is not a single type of runner.
There are different kind of runners, including:
- A Shell executor
- A Docker executor
- A Docker Machine and Docker Machine SSH (auto-scaling) executor
- A Parallels executor
- A VirtualBox executor
- A SSH executor
- A Kubernetes executor
For the GitLab CI interface, this does not matter. All runners look the same.
The GitLab Runner clients are available on multiple platforms, as it is a Go binary that runs on many platforms. The configuration file is called config.toml and is in the TOML format, which is less complex than the YAML format.
The specification of this format is publicly available and can be found at https://github.com/toml-lang/toml.
Features of GitLab Runner include the following:
- The ability to run multiple jobs concurrently
- The use of multiple tokens with multiple servers (even per project)
- The ability to limit the number of concurrent jobs per token
The jobs GitLab Runner can execute can do the following:
- Run on a local computer without containers or virtualization
- Run inside Docker containers
- Run inside Docker containers and execute jobs over SSH
- Run using Docker containers with autoscaling on different clouds and virtualization hypervisors
- Run by connecting to a remote SSH server, where it can be executed
Additional features include the following:
- GitLab Runner supports Bash, Windows Batch, and Windows PowerShell
- The runner binary works on GNU/Linux, macOS, and Windows (all Docker-supported platforms)
- A runner allows the customization of a job-running environment
- A runner can have an automatic configuration reload without restart
- It is to easy set up, with support for Docker, Docker SSH, Parallels, or SSH-running environments
- Runners also support the caching of Docker containers
- The Runner package supports installation as a service for GNU/Linux, macOS, and Windows
- You can enable an embedded Prometheus metrics HTTP server in a runner
You can find an overview of the registered runners in GitLab by logging in as an admin and checking the Runners menu on the left, as shown in the following screenshot:

You should now see a list of runners that have registered on your GitLab instance, as shown in the following screenshot:

As shown in the following screenshot, if you click a runner, you will see that it is possible to do the following:
- Configure a paused runner not to accept new jobs
- Designate a runner as protected
- Set a runner to pick up jobs with or without tags
- Lock a runner to projects
- Set a maximum timeout for a job
- Tag runners

On the left, you will be able to see which jobs have been recently processed by a runner, as shown in the following screenshot:

So far, you have seen how GitLab CI fits in with the GitLab product and also how GitLab Runners are registered with an instance. We have also looked at the basic features of GitLab Runners and showed you how to create a configuration file for jobs to be picked up and executed by a runner.
In this chapter, we discussed GitLab CI, the most critical feature of GitLab after offering version control. We explained how to trigger pipelines and how to design them using the .gitlab-ci.yml file. Then, we showed you the jobs that make up the pipelines. We finished with some information on how GitLab runners fit into this architecture. In the next chapter, we are going to show you more about the GitLab Runner client.
- Name one of the pillars of Extreme Programming.
- What file is used to describe jobs and pipelines?
- How does a build system know when to build software in a pipeline?
- What tests are necessary for reaching the integration phase?
- Which tag is used in the .gitlab-ci.yml file to execute a job?
- What is the name of the configuration file used by the runner?
- How many concurrent sessions can a runner start?
- How can you get metrics about the execution of a GitLab runner?
- gRPC [Golang] Master Class: Build Modern API and Microservices [Video] by Stephane Maarek:
- Hands-On Auto DevOps with GitLab CI [Video] by Alan Hohn: