Up and Running with GitLab CI/CD on Alibaba Cloud

By Sai Sarath Chandra, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In this tutorial, we will learn how to set up continuous integration and continuous deployment (CI/CD) with GitLab on an Alibaba Cloud Elastic Compute Service (ECS) instance. Before we start the tutorial please log in to the console and get ready.

New to Alibaba Cloud? Sign up for a free account and try over 40 products for free worth up to $1200. Alternatively, visit Getting Started with Alibaba Cloud to learn more.

How Continuous Integration(CI) and Continuous Deployment (CD) evolved?

Without CI/CD

Once we get the requirements from the business team, the development team takes care of transforming the requirements into some form of functional code in the form of software. The whole activity is mostly performed within the development environment. The development might take months or years depending upon the requirement once this activity completes, the whole project is given to the operations team to test, deploy in the production environment.

Once the project is handed over to the operations team, this team is responsible for testing, deploying, maintaining the code in the production environment. Most of the times the production environment is not similar to the development environment. This difference results in many issues (such as dependency error, performance issues & environment issues), If the team recognizes the error is at the application level then it escalates to the development team and then the whole process cycle again. If there is an error due to the environment, then the operation team fixes it, deploy the project in finally make it available to business users.

Image for post
Image for post

Integration w/o CI/CD

This approach itself has many disadvantages:

  1. The time to market for the product is very high
  2. Teams work within their boundaries
  3. Inefficient resource utilization
  4. Time consuming process

With CI/CD

Once we get the requirements from the business team, the development team and operations team together work on deciding different configuration parameters for the environment. This collaboration ensures that the development environment is similar to the production environment or at least no issue arises when the developed project moves to the production environment.

Using different tools like Gitlab for version control and many others, there are real-time reports for both the development team and operations team to see how the project is performing.

Using different services like resource orchestration service, the operations team able to create and manage the service using a simple configuration this reduces much overhead to the operation. This process also requires each team having a basic idea of what other teams were doing within the project.

Image for post
Image for post

Integration with CI/CD

This approach has significant advantages:

  1. Faster delivery of features
  2. More stable operating environments
  3. Improved collaboration between different teams
  4. More time to innovate the product
  5. Less complexity to manage
  6. Continuous software delivery
  7. Swift resolution of problems

Setting up GitLab CI:

GitLab Community Edition (CE) is an open source end-to-end software development platform with built-in version control, issue tracking, code review, CI/CD, and more.

Prerequisites

Since we are working with Gitlab continuous integration, we need a Gitlab instance installed on an Ubuntu 16.04 server on Alibaba Cloud Elastic Compute Service (ECS) instance. GitLab recommends a server with at least 2 CPU cores and 4GB of RAM.

Follow this tutorial if you want to know how to setup Gitlab CE on Alibaba Cloud ECS instance: https://www.alibabacloud.com/blog/version-control-with-gitlab-on-alibaba-cloud_593276

Now let’s see what is GitLab CI is all about,

GitLab CI/CD is a part of GitLab, a web application with an API that stores its state in a database. It manages projects/builds and provides a friendly user interface, besides all the features of GitLab.

What Are the Advantages of GitLab CI/CD?

Integrated: GitLab CI/CD is part of GitLab.

User Friendly: GitLab CI/CD offers the same great experience as GitLab. Familiar, easy to use, and beautiful.

Scalable: Tests run distributed on separate machines of which you can add as many as you want

Faster results: Each build can be split into multiple jobs that run in parallel on multiple machines

Continuous Delivery (CD): multiple stages, manual deploys, environments, and variables

Open source: CI/CD is included with both the open source GitLab Community Edition and the proprietary GitLab Enterprise Edition

As part of Gitlab CI/CD, we also install GitLab Runner,

GitLab Runner is an application which processes build. It can be deployed separately and works with GitLab CI/CD through an API.

Using Gitlab CI Runner, we automatically check out the code and execute the test to validate the new changes. To make the testing environment independent of the development environment. We run all our tests within Docker Containers. To do this, we set up the docker environment on where the Gitlab CI runner gets installed.

If you need help in setting up the docker environment in Ubuntu on Alibaba Cloud ECS instances, follow the article below: https://www.alibabacloud.com/blog/how-to-install-and-use-docker-on-ubuntu-16-04_470612

To confirm whether docker is installed or not, login into your Alibaba Cloud ECS instances

Run the command to see the current status of the docker, you should see the status similar to the below

Run command to see all the available switches

Run ‘docker COMMAND — help’ for more information on a command.

Invoking “docker info” should produce the output similar to this

This confirms that you have properly setup Docker on your instance. Now to start the GitLab CI setup, we need to have a repository. Let’s start by cloning one from GitHub to GitLab.

Importing a GitHub Project

I will be using the following repository for demonstrating the Gitlab CI. The below is a NodeJS application.

https://github.com/do-community/hello_hapi.git

You need to create one project before importing, create a project as shown in the below

Image for post
Image for post

Click on “New Project” and select “Import Project > Repo by URL” option in the below screen.

Image for post
Image for post

Paste the Git Repository by URL provided at the start of the section and update the “Project name,” “Project description” & “Visibility Level” as required and choose Create Project.

Image for post
Image for post

Once the project is imported, you will see the screen similar to this

Image for post
Image for post

Now we will have to understand Gitlab-CI build descriptor works (.gitlab-ci.yml). The file is YAML format if you open the file in repository you will see similar to below

The above uses the latest nodeJS image while creating the docker container

These are the stages we will define while testing the code, the above definition reads to “Perform the build and once it is successful, then the test will be performed”. Jobs with in the same stage are performed in parallel. These are just the tags we will create but the actual definition will follow next. There are 3 stages that Gitlab provides “build”, “test” & “deploy”

Then we will create a cache to store the node_modules (dependencies in a node project). So that we can use these files across the stages.

This is the first job (install_dependencies) will detail the steps for installing the node dependencies. The ‘script’ tag is used to specify the actual script in the job. The “paths” specify where the output of the job will be saved.

The “test_with_lab” job will perform the test once the “install_dependencies” job is successful.

Triggering a CI Job

We will trigger a job in Gitlab by committing a new file to the repository.

Click on ‘+’ icon > New File

Image for post
Image for post

We will create a .txt file with some text and “Commit Changes.”

Image for post
Image for post

Once the commit is done you will see that small pause icon showing in the pending state.

Image for post
Image for post

Once you click on the icon, you will see the detail status of the commit

Image for post
Image for post

If you see the detail information of the job, “The job is stuck, because the project doesn’t have any runners online assigned to it”. Since we don’t have any runners configured for the job we see the job in paused state.

Image for post
Image for post

Install and Configuring the GitLab CI Runner

Download the latest version of Gitlab CI Runner from the repository to the /tmp directory of the server by executing the follow command

Run the installer from the downloaded script

The above script will help manage the Gitlab Runner packages from the same repository. We can install the Gitlab runner once the script is successfully executed.

This will install and start the Gitlab runner service.

There are two ways we can setup the Gitlab runner

  1. Shared Service
  2. Project Specific Service

We will see how we can setup the project specific service, before we do that we need to collect some information for the runner service, to do that we need to navigate to

Project -> Settings -> CI/CD -> Runners Settings -> Expand

Image for post
Image for post
Image for post
Image for post

Save the URL and the token highlighted in the above picture as we need it while setting up the Runner.

Register a Gitlab CI Runner

Login to the server and run the following command

Input the variables similar to below

Now if you go back to the portal and see the commit status that should be in running state, similar to this:

Image for post
Image for post

After some time you can see the job is succeeded with the following output:

Image for post
Image for post

Congratulations! You have configured GitLab CI on an Alibaba Cloud Elastic Compute Service (ECS) instance along with your GitLab CE. This helps developers to run and test the features easily within the version control.

Reference:https://www.alibabacloud.com/blog/up-and-running-with-gitlab-ci%2Fcd-on-alibaba-cloud_594044?spm=a2c41.12123039.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store