How to Set Up a Continuous Deployment Pipeline with GitLab CI/CD

Automated Deployment streamlines your workflow. In this article, we’ll learn setting up a continuous deployment pipeline using GitLab CI/CD

Continuous Deployment Pipeline

In the last few articles, we learned about the basics of version control and then we enhanced our knowledge by learning about different branching models and workflows. Today in this blog post we’ll explore about continuous deployment pipeline with GitLab CI/CD. So, Let’s get started!

What is Gitlab CI / CD

Continuous integration (CI) is one of the DevOps best practices of automating the integration of code changes from multiple contributors into a single software project. This allows developers to frequently merge code changes into a central repository where builds and tests then run. Continuous Deployment (CD) moves one step further by automatically deploying code to the target environment (production or staging) after each code commits and builds.

Before you start, make sure you have a project in GitLab that you would like to use CI/CD for. And you should have maintainer or owner level access for the project.

What is GitLab Runner

GitLab Runner is a small lightweight program written in Go that runs Continuous Integration (CI) related jobs on your local machine and sends the results to GitLab for it to consider the changes. It is a single executable binary that can be installed on any major OS.

Before we move forward with the GitLab CI / CD you need to make sure you have the runners available to run your tasks. You can follow the official guide to install and register GitLab runner.

.gitlab-ci.yml file

The .gitlab-ci.yml is a file where you configure specific instructions for GitLab CI/CD. It is placed in the root of your repository and contains definitions of how your project should be built. Every time we push a commit to our GitLab instance, GitLab would invoke a Runner to build and test the project as instructed inside this file.

Create a Sample App

Assuming that you’ve created a GitLab Repository already, lets create a sample application and we’ll setup a CI / CD process for the application.

First of all create package.json file in root of your project

{
  "name": "sample_web_app",
  "version": "1.0.0",
  "description": "Sample Application",
  "author": "Assad",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

Now create a app.js in root of your project

const express = require('express');

const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(8080, 'localhost');
console.log('Running on http://localhost:8080');

Lastly, create a Dockerfile and add the following contents to it:

FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "app"]

Dockerfile contains the build process for this app. It involves creating a node container and installing the dependencies.

Now we need to create a .gitlab-ci.yml file. This file would contain the instructions to build our project. Every time we push a commit to our GitLab repository, GitLab would invoke a Runner to build the project.

image: node:latest
stages:
- build

install_dependencies:
stage: build
script:
- npm install

We have just one stage build and it has just npm install as a script. This is a command you would have to manually run every time a change comes to your project. And, that’s where the GitLab runner comes in and would do this for you automatically every time you make a commit.

Now assuming that you have already installed the GitLab Runner and Also have registered it with your GitLab instance we can test the GitLab CI / CD in action by making a simple commit to the application and push it to GitLab.

You can then open your project in GitLab, go to my-project → Pipeline and you will see a tag saying “passed” next to the commit you made. Subsequent commits will also have tags.

Conclusion

In this article, we learned about the basics of GitLab Continuous Integration and Continuous Deployment. We studied what is GitLab Runner and what’s the use of runners in the overall CI / CD workflow. We also created a sample project which explains how to set up continuous deployment using GitLab.