CI/CD Easy Way With GitLab

As a DevOps specialist I often struggle a lot with using some DevOps tools especially for the first time and end up spending tons of time trying to make things work. I also relish using awesome tutorials and guides that have been developed by other open source content creators to make my/our work easier. In that spirit I also enjoy creating content on things I learn that I believe will be useful to someone else out there. Find my other blogs on CI/CD with Jenkins here and Microsoft Azure App Services here.

I was tasked to develop a CI/CD pipeline for our company Hepta Analytics for a project on a small budget to facilitate collaboration and ease integration of the components (backend and frontend). The backend is written in Java and Frontend in Angular. In this blog I will focus on the Java example and later will write about Angular setup. Note that this has not been tested in production and it’s just for integration and testing environment.

Major Things I Considered

  1. Cheap Hosting:-Definitely a cheap hosting because it’s a testing environment.
  2. Cheap Version Control:-We needed private repositories to host our code.
  3. Cheap Pipeline Tool:-Cheap and reliable pipeline tool that gives us control over how we want to deploy.

I choose to go with Digital Ocean because they provided a cheap droplet for only $5 per month and this would be enough for the integration environment. You can use my referral link for a $100 in credit over 60 days during Hacktoberfest!

Setting up a Private Repository

Create a GitLab account if you do not have one already and log in. You should also create a group in case you want to share this repository with other developers. Create a new private repository by clicking on the “New Project” button

CI/CD with GitLab

Provide repository information as shown below. Note on visibility level I choose private because I don’t want my code to be available to the public.

CI/CD with GitLab
CI/CD with GitLab

Initialize repository with a README and click on “Create Project”. This tutorial assumes your project code is in Java. In case you are using any other language remember to modify the “gitlab-ci.yaml” file accordingly. Push your code from your local machine to the GitLab repository we just created.

You will need to create new files such as;

  • Dockerfile: Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
  • Docker compose file: The compose file is a YAML file defining services, networks and volumes needed to run a container. We will need this to run our application.
  • Gitlab-ci.yaml file: This is a YAML file that is used by GitLab Runner to manage your project’s jobs. It outlines all the build, testing, deploy stages as required.

Configuring CI/CD Pipeline

From your repository click on set up a new CI/CD. This will prompt you to create a .gitlab-ci.yml file. You can paste your commands here or just push it directly from your local machine together with the rest of the code and configuration files.

Select CI/CD set up from your repository
Select CI/CD set up from your repository

Still from the repository, select Settings followed by CI/CD. Under variables, click expand to see the fields you need to create.

Provide the variable keys and values as shown on the screenshot below. Notice that these variables are used in the .gitlab-ci.yml file instead of having them as plain text in the file. It’s just a more secure way of handling your credentials and other important stuff.Notice that you will need to complete the next section to be able to get some of the values to the variables listed below.

CI/CD configuration variables

This guide uses SSH to log into the deployment server and copy files such as docker-compose.yml from Gitlab to run the application.

Setting Up & Configuring a Droplet on Digital Ocean

Login into digital ocean portal and click on Create then select Droplets. Select the base image of ubuntu 16.04.

Choose the smallest droplet or a bigger one depending on your budget but the small size droplet would do the work.

Select the region of your droplet depending your location and latency implications.

Select your SSH keys or create new ones or leave it blank to use username and password.Provide the name of the instance as well.

Hurry, let’s now configure the server ready for deployment. Login with the SSH key you created/ username & password.

Install docker and docker compose consecutively following these tutorial docker tuitorial docker compose

Create the .ssh directory if it doesn’t already exist.
mkdir ~/.ssh
chmod 700 ~/.ssh

Create a ssh key pair. Leave all the question/answer fields blank. Don’t set a password or alternate name
ssh-keygen -t rsa

Display the public key using cat and manually copy it by highlighting the text with your mouse and copying it. Copy everything including the `ssh-rsa` to the machine name (Ex: [email protected]). Save the public key into your authorized keys
cat ~/.ssh/id_rsa.pub
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Copy your private key contents and use it to configure DEPLOY_SERVER_PRIVATE_KEY variable in our GitLab CI settings.
cat ~/.ssh/id_rsa

Make any changes, commit and push the code to trigger the pipeline and monitor what is happening from GiitLab dashboard. To debug, you can print some messages in various steps to give you an insight as to what is happening. When the pipeline runs successfully, visit your server ip eg (126.34.54.26) followed by the port number which your application is running from. Voila our application is now running. You can view logs by running docker logs {container} from the deployment server.

All the code and configuration files you need can be found in my GitHub repository here.
Hope you learnt something cool and can reach out if you are having challenges with this setup.

Leave a Reply

Your email address will not be published. Required fields are marked *