• Home
  • >
  • Tech News
  • >
  • Setup CI/CD with CircleCI config and practice example

This article will explain the concept of CI/CD, CircleCI config, and how to apply it to a practical case – NodeJS project. Even if that specific tooling concoction isn’t your cup of tea, the ideas we’ll go through will be helpful for whatever is in your setup.

What is CircleCI config?

Continuous integration (CI) is a strategy that encourages developers to integrate their work into a common repository’s master branch. Instead of developing features in isolation and merging them at the conclusion of a development cycle, each developer integrates code with the common repository numerous times.

CircleCI is a continuous integration and delivery platform that enables development teams to release code quickly while automating the build, test, and deploy processes.

CircleCI can be set to execute extremely complicated pipelines effectively using caching, docker layer caching, resource classes, and other features. After a GitHub or Bitbucket repository is approved and added as a project to circleci.com, every code prompts CircleCI to start tasks. After the test, CircleCI sends an email indicating success or failure.

circleci-config

CircleCI NodeJS

What is CI/CD? When to use CircleCI?

Continuous integration and continuous delivery are abbreviated as CI/CD.

Continuous Integration is the practice of automatically merging all code updates from many developers into a single common repository. Then, before each build, each integration is validated by an automated tool that checks for code style and executes tests.

Continuous Delivery complements Continuous Integration by delivering all code changes to the testing or production environment following the build step. In other words, all-new features, bug fixes, and so on will be sent to testers/users on a regular basis.

This CI/CD method greatly minimizes the cost of manual operation for developers, improving productivity and reducing deployment time.

For example:

  • A group of developers is working on a NodeJS project.
  • Developers’ code updates are posted to a Bitbucket repository.
  • CircleCI will be triggered with each commit submitted to the repository. It will download the most recent code, install dependencies, and run tests.
  • After all, tests have been passed, CircleCI will deploy code to a remote server using SSH.

Why is CircleCI config better Jenkins (and other alternatives)?

Apart from many other CI/CD tools, we solely compare CircleCI to Jenkins — the most popular CI/CD tool of all time — in this post.

CircleCI

Jenkins

The first and most obvious distinction between Jenkins and CircleCI is that you need a server to host Jenkins on, requiring administrative skills and a significant amount of effort for configuration/customization.Meanwhile, CircleCI is a cloud-native platform, which means it does not need any server setup and can be used right away.
Second, the Jenkins UI is outdated and unintuitive, whereas CircleCI’s design is incredibly user-friendly.CircleCI yml syntax is clean and easy to comprehend for developers regarding configuration.

To summarize, CircleCI is a lightweight CI/CD platform that natively supports practically every programming language. Deployments may also be made to AWS, Azure, Google Cloud, Heroku, and various other cloud hosting platforms.

How to set up CircleCI config?

Let’s have a look at this Practical scenario: Deploy a NodeJS Project to a Remote Server

Prerequisites:

  • Node.js project stored in a Bitbucket repo
  • 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 understanding of SSH
circleci

CircleCI tutorial

Set up:

1. Configure CircleCI to monitor Bitbucket commits.

  • Create a folder called .circleci at the root of your project, and then add the config.yml file to this folder.
  • Connect your Bitbucket repository to the CircleCI project.
  • CircleCI can now run the build job defined in the config.yml file.

2. Set up the remote server so that CircleCI can run the deployment process.

By running the following command on the terminal, we may generate an SSH key pair that we will use throughout this tutorial:

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

It will ask you a few questions; remember to leave the pass blank (when we wrote this article, CircleCI requires an empty passphrase).

→ The results are 2 files: id_rsa (private key) and id_rsa.pub (public key).

Check the Bitbucket repo settings to see whether the CircleCI Deploy Key has been added automatically, copy id rsa.pub and put another key there.

setup-circleci

circleci requires

Copy id_rsa and add your private key there in the CircleCI project setting.

circle-setup

CircleCI manual trigger

 

In your remote server, append your public key to authorized_keys in ~/.ssh by executing 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: CircleCI will now fetch code from Bitbucket and conduct a build operation in their cloud whenever you push code to Bitbucket. Following that, it executes a deploy job by connecting to the remote server through SSH, then automatically running the deploy command on the remote server to grab code from Bitbucket, followed by pm2 to start/restart the process stated in the config.yml file.

Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa

Add your private key to the ssh-agent by executing this command:

eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa

CircleCI will now fetch code from Bitbucket and conduct a build operation in their cloud whenever you push code to Bitbucket. Following that, it executes a deploy job by connecting to the remote server through SSH, then automatically running the deploy command on the remote server to grab code from Bitbucket, followed by pm2 to start/restart the process stated in the config.yml file.

ssh -p your_port_number your_user@your_host 
"cd ../path/to/your/project; 
git pull; 
pm2 start hello_sts";

Wrapping Up

With CircleCI, you have effectively automated the development and deployment procedures. Your team may now work smoothly to produce the product on time without conducting manual activities.

Read More:   Update The Impedance Mismatch Test: Is Your Data Layer a Complex Mess?

This simple lesson intends to provide you with some core CircleCI information to help you get started on your DevOps journey. Dive directly into CircleCI’s docs to learn its capabilities. With so many options available for you to explore, the possibilities are limitless:

  • Excellent CircleCI Docker support.
  • Workflows are used to orchestrate jobs.
  • SSH debugging and caching
  • A visual dashboard can help you learn more about your repository.
  • Deploy your application to AWS, Azure, Google Cloud, Heroku, and other cloud platforms.

If you have any concerns, feel free to fill in the contact form below to get a consultation. Or explore more services from our website.

Read More:   5 Common Risks in Infrastructure-as-Code Templates – InApps Technology 2022
Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...