How to setup CI/CD with CircleCI and deploy your NodeJS project to a remote server

Coda
Coda
Nov 1 · 4 min read

In this article, I will share with you about the concept of CI/CD and apply it into a practical scenario.

Table of Contents:


What is CI/CD and why should I use CI/CD

CI/CD stands for continuous integration and continuous delivery.

  • Continuous Integration is the process in which all code changes from multiple developers are automatically merged into a single shared repository. Then each integration is verified by an automated tool that check for code style and run test before build.
  • Continuous Delivery expands upon Continuous Integration by deploying all code changes to a testing environments and/or production environment after the build stage. In order words, all new features, bug fixes, … will be delivered to the hand of testers/users frequently.

→ This practice of CI/CD significantly reduces the cost of manual operation for developers, hence improve productivity and shorten deployment time.

Practical scenario:

  • A group of developers working on a NodeJS project
  • All code changes from developers are pushed to a Bitbucket repository
  • With each commit pushed to the repository, CircleCI will be triggered. It will checkout the latest code, install dependencies and run tests
  • After all tests are passed, CircleCI deploy code to a remote server via SSH

Why choose CircleCI over Jenkins (and other alternatives)

Beside many other CI/CD tools, in this article, I only compare CircleCI to Jenkins — the most popular CI/CD tool of all time.

First of all, and probably the most distinct difference between Jenkins and CircleCI is that you need a server to host Jenkins there, which requires administration knowledge and a considerable amount of time for configuration/customization. Meanwhile, CircleCI is a cloud-native platform, which means you do not need to setup any server, it just run out of the box.

Secondly, Jenkins interface is rather old and unintuitive, whereas CircleCI’s design is very friendly. When it comes to configuration, with CircleCI, the yml syntax is clear and easy for developer to read.

In conclusion, CircleCI is a lightweight CI/CD platform which supports almost every programming languages out of the box. Developers can also deploy to AWS, Azure, Google Cloud, Heroku and many other cloud hosting services.


Demo

Prerequisites:

  • a Node.js project stored in a Bitbucket repo
  • a CircleCI account
  • a remote server that you can connect via SSH. On this server, you must install git, Node.js, and pm2 (recommended tool to manage Node.js processes)
  • basic knowledge of SSH

Setup

1. configure CircleCI to track commits on Bitbucket repo

  • create a folder named .circleci at the root of your project, add config.yml file to this folder with the following content
  • add your Bitbucket repo to CircleCI project

→ now CircleCI is able to perform the build job specified in the config.yml file

2. configure your remote server so that CircleCI can perform the deploy job

generate an SSH key pair which we will be using throughout this tutorial by execute this command on terminal:

ssh-keygen -t rsa -C “my_email@gmail.com”

you will be asked a few questions, remember to let the passphrase empty.

→ the result are 2 files: id_rsa (private key) and id_rsa.pub (public key)

  • in Bitbucket repo settings, check if CircleCI Deploy Key has been automatically added, then copy id_rsa.pub and add another key there
  • in CircleCI project setting, copy id_rsa and add your private key there
  • in your remote server, append your public key to authorized_keys in ~/.ssh by execute this command:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

  • in your remote server, copy your private key into ~/.ssh folder and modify ~/.ssh/config file to include:

Host bitbucket.org

HostName bitbucket.org

IdentityFile ~/.ssh/id_rsa

  • add your private key to the ssh-agent by execute command
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa

→ Now every time you push code to bitbucket, CircleCI will pull code from there and perform a build job on their cloud. After that, it performs a deploy job by connecting to remote server via SSH, then automatically run command on remote server to pull code from bitbucket, then pm2 to start/restart process as specified in the config.yml file

Coda

Written by

Coda

A bored programmer who loves philosophy and psychology.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade