CI/CD Pipeline Setup with GitLab

Shashank Gupta
Globant
Published in
6 min readOct 18, 2022
CI/CD with GitLab

CI/CD with GitLab:

This article will guide you on how to do CI/CD pipeline setup with GitLab.

Continuous integration and continuous delivery (CI/CD) is a methodology of automatically building and deploying code to provide you with greater speed and reliability. It is done in two parts: continuous integration (CI) and continuous delivery (CD). Continuous integration is the practice of continually integrating code into the main branch of your repository, automatically testing it. Continuous delivery is then getting your code to a deliverable state, so it can be deployed at the click of a button. Or, in the case of continuous deployment, automatically deploy your code if all tests pass.

In this article, we will cover the below-mentioned topics.

  • Signup on GitLab
  • Set Up a GitLab Runner
  • Set Up GitLab CI Pipeline

Assumptions:

  1. You have basic knowledge of Linux and Unix-Based Platforms.
  2. You have basic knowledge of docker & its images.

If all assumptions above are met, let’s start our GitLab project and its Pipeline setup.

Signup on GitLab:

Once we successfully sign up with the GitLab portal, we will get the below page.

Welcome page of GitLab

Now we need to create a Project and create a branch inside the Project Repository.

Here the project has been created successfully.

Set Up a GitLab Runner:

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.

The scope of runners of ALL TIERS:

Runners are available based on who you want to have access to:

  1. Shared runners are available to all groups and projects in a GitLab instance.
  2. Group runners are available to all projects and subgroups in a group.
  3. Specific runners are associated with specific projects. Typically, specific runners are used for one project at a time.

For more details, go to https://docs.gitlab.com/

Here I am going to set up Specific runners.

Before setting up the Specific Runner, I am assuming you have an Ubuntu Linux machine to set up the runner on this machine.

Step1: First Login into the Ubuntu machine through putty.

Step2: Simply download one of the binaries for your system:

Linux x86–64 sudo

#curl -L — output /usr/bin/gitlab-runner “https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"

Step3: Give it permission to execute:

#sudo chmod +x /usr/bin/gitlab-runner

Step4: Create a GitLab CI user:

#sudo useradd — comment ‘GitLab Runner’ — create-home gitlab-runner — shell /bin/bash

Step5: Install and run as service:

#sudo gitlab-runner install — user=gitlab-runner — working-directory=/home/gitlab-runner

#sudo gitlab-runner start

Note: Ensure you have /usr/local/bin in $PATH for root, or you might get a “command not found” error. Alternatively, you can install gitlab-runner in a different location, like /usr/bin.

The next step would be to register the GitLab Runner to GitLab:

To enable runners for a GitLab Project:

Step1: On the top bar, select Main menu > Groups and find your group.

Step2: On the left sidebar, select Settings > CI/CD.

Step3: Expand Runners.

GitLab Runners

In this fig, we can see there are no Specific Runners Registered.

To register a runner under Linux:

Login into the Ubuntu machine and run the following command:

sudo gitlab-runner register

After running this command it will ask below-mentioned details, provide those details, and your Runner will be ready.

a. Enter your GitLab instance URL (also known as the gitlab-ci coordinator URL).

b. Enter the token you obtained to register the runner(These details you can get from your GitLab Project > Settings > CI/CD > Runners )

c. Enter a description for the runner. You can change this value later through the GitLab user interface.

d. Enter the tags associated with the runner, separated by commas. You can change this value later through the GitLab user interface.

e. Enter any optional maintenance note for the runner.

f. Provide the runner executor. For most use cases, enter docker.

g. If you entered docker as your executor, you are asked for the default image to be used for projects that do not define one in .gitlab-ci.yml.

After providing those details we are able to see our Specific Runner has been Configured Successfully.

Now we need to set up gitlab runner as a sudo user onto the runner machine and set up docker onto a runner machine as a backend.

add gitlab-runner user into the sudoers file.

#apt-get -y update

#apt-get -y install docker

#apt-get -y install docker.io

Setup Gitlab CI Pipeline:

Now let’s Set Up the Pipeline:

For Running the Pipeline first we need to write the .gitlab-ci.yml YML file.

Explanation of this file:

Line 1: We can use ‘#’ to enter comments in the yml file.

Line 2–4: Here the process will be executed in two stages: build & deploy.

Line 6–7: We can specify the Image of the node version for running the code on docker.

Line 9–16: The job is for creating a build.

Line 18–25: The job is to do the test.

Here we can specify any name we want for the job stage name must be the same as defined in stages.

We can check pipelines after adding this yml file in the master branch.

After we commit the .gitlab-ci.yml file, the pipeline execution starts.

Commit the .gitlab-ci.yml file.

After committing our Pipeline has been executed and completed successfully, for more details click on Jobs and then click on specific job to see its logs and details.

Here, GitLab Pipeline has been executed Successfully.

Happy Learning :)

Summary:

Through this article, we are able to learn, how to create a GitLab pipeline with a specific gitlab-runner and run the pipeline.

References:

The following links were referred to as part of creating this document.

--

--