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.

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.

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.


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 sudo systemctl status docker command to see the current status of the docker, you should see the status similar to the below

Run docker 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.


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

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

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.

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

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

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

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

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

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.

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

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:

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

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.