모범 사례 및 튜토리얼

CircleCI를 사용한 Node.js 애플리케이션 지속적 배포

A few months back I had written about creating a continuous deployment pipeline with Node.js and Jenkins, where the Node.js application was using 카우치베이스 in some fashion. Essentially, it took a project from GitHub, installed the dependencies, ran the tests, and then ran the application on some server, where some server was the same machine that Jenkins was on.

Jenkins is great, but it is a lot of work to get set up with and to keep going. Instead there are hosted continuous integration (CI) and continuous deployment (CD) solutions that exist, such as CircleCI.

We’re going to see how to take a Node.js project that is version controlled on GitHub and continuously deploy it to some remote server using CircleCI every time a push is made.

Before continuing, you should already have created a CircleCI account, have a remote server available, and have a Node.js project available on GitHub.

In this example, I’m using a Droplet that I’ve created on Digital Ocean and the Node.js user profile store project I had written about a while back. Feel free to use whatever you’d like.

Preparing the Remote Server for SSH Connections

The goal in continuous deployment is to be able to automatically deploy a stable project to your server seamlessly.

When using CircleCI, during the deployment step, we want our project to be copied after our testing workflow succeeds. This can happen via an SCP or a Git Checkout with a series of commands on the remote server. Whatever best meets your needs.

Since we are using Node.js, it makes to use the latter because we’ll need to install dependencies, and restart the Node.js service. It has a little more depth than just copying files over. For this reason, we’ll need CircleCI to be able to connect to our remote instance via SSH.

The safest way to do this is to create an SSH private key for CircleCI. From your server, assuming it is Linux, execute the following:

You’ll be asked a series of questions regarding the key. It is important that this SSH key does not contain a password since CircleCI does not support key passwords as of right now.

Once the key is generated, execute the following:

The above command assumes that you’ve named your key id_rsa. It will append the public key to the end of the authorized_keys 파일.

The contents of the id_rsa private key file needs to be copied to our CircleCI dashboard later in this guide.

Before designing the workflow, we need to clone the GitHub project to our server. Take the following for example:

It is important to remember where you cloned the project to because it will be used by the deployment script in our workflow. CircleCI will SSH into our server and do a git pull inside the project directory.

Designing the Workflow Steps

Before the project will be picked up by CircleCI, it needs to be altered a bit on GitHub. CircleCI, like many other CI / CD solutions, requires a configuration file to be placed in the project.

Assuming that you’re looking at the user profile store project, we need to create a directory called .circleci at the root with a file called config.yml.

Open this .circleci/config.yml file and add the following:

So what is happening in the above configuration?

We’re saying that we want to spin up a Docker container running Node 7.x to perform our tests and other workflow tests. In regards to the steps, first the project is checked out from GitHub into the container. Then we install all dependencies into the project as they are not committed into GitHub. The package.json of our project has a script called test which in theory would run our tests. Our project doesn’t have any real tests.

Provided the previous three steps were successful, a deployment happens. This command will SSH into our server, navigate to the directory of our choosing, pull changes to the project, install any potentially new dependencies, and start the forever process.

We are able to deploy our project through SSH because at this point we’re sure the previous local steps had succeeded on the container.

Commit the project changes to GitHub so we can configure the project in the CircleCI dashboard.

Configuring the Project within the CircleCI Dashboard

A few things need to be configured within CircleCI to get the CI / CD pipeline functional for the project. Within your CircleCI dashboard find the GitHub project that you wish to manage. If you’ve linked GitHub with CircleCI, there is a good chance it will show up already because of the configuration file addition.

Within the project settings, find the SSH Permissions section.

CircleCI SSH Permissions Example

You’ll want to copy the private key created earlier into this section. This will allow CircleCI to connect to your server for deployment. Remember, as of right now, the SSH key must be passwordless.

At this point, any future pushes to GitHub should result in the pipeline starting. If everything passes, you should be able to access the changes as they happen on your server running Node.js.

Where is the NoSQL Benefit?

Let’s say that your code changes involve changes to your data model. If you were using an RDBMS, you would likely have to push a SQL script with a bunch of ALTER statements every time you make a change. While possible, it definitely isn’t convenient.

Let’s get a little more specific here.

Say I wanted to include address information in each of my user profiles, something that wasn’t previously being collected. With NoSQL and a flexible JSON model, I could just add the information to my JavaScript objects being saved and complete the save. No need to create any migration scripts.

결론

You just saw how to use CircleCI for continuous deployment of a Node.js web application. We had used a user profile store application from a previous example, and deployed it to a server via SSH whenever new changes were pushed to GitHub.

There are many continuous integration and continuous deployment solutions available. If you want to host your own solution, you can check out a variation of this tutorial that uses Jenkins called, Create a Continuous Deployment Pipeline with Node.js and Jenkins.

이 기사 공유하기

작가

닉 라보이(Nic Raboy)는 모던 웹 및 모바일 개발 기술의 옹호자입니다. 그는 자바, 자바스크립트, 골랑(Golang)을 비롯해 앵귤러, 네이티브스크립트, 아파치 코르도바 같은 다양한 프레임워크 경험을 가지고 있습니다. 닉은 웹 및 모바일 개발을 더 쉽게 이해할 수 있도록 돕는 자신의 개발 경험에 대해 글을 씁니다.

1개의 응답

댓글 남기기

카우치베이스 카펠라를 시작할 준비가 되셨나요?

개발 시작하기

NoSQL을 탐색하고, 리소스를 찾아보고, 튜토리얼을 시작하려면 개발자 포털을 확인하세요.

카펠라 프리 사용하기

단 몇 번의 클릭으로 카우치베이스(Couchbase)를 직접 체험해 보세요. Capella DBaaS는 시작하기 가장 쉽고 빠른 방법입니다.

연락해

Couchbase 제품군에 대해 더 알고 싶으신가요? 저희가 도와드리겠습니다.