Configure CircleCI to automatically deploy your NodeJS applications

Tom Nagle
Tom Nagle
Jun 13 · 4 min read

CircleCI is a a great way to automate your continuous integration and delivery, but you already know that, that’s why you’re here.

In this post you will learn how to:

  1. Setup a circleci user on your server
  2. Add SSH keys so CircleCI can log in to your server
  3. Add a configuration file to your NodeJS application (could also be any other code base)
  4. Add your project to CicrcleCI and start bulding

Add a circleci user without a password so you don’t have to give root access to CircleCI.

Generate a public and private key with PuTTyGen by clicking the ‘Generate’ button and moving your cursor around in the open space to generate a unique key.

When the green bar get to the end, you’ll be given a public key and the option to save a private key. Save your private key and store it somehwere safe. This key will give the holder access to your server, so be carful with it.

Change the key comment to cricleci. This will add circleci to the end of the key, making it easy to see what key belongs to who when you have multiple keys in your authorized_keys file. Copy the public key into a notepad and keep it safe, we need it for the next step.

Go back to your SSH terminal and touch a new authorized_keys file.

Add your key to the /home/circleci/.ssh/authorized_keys file and save it.

Change the owner of the directory to the circleci user.

Change the owner of your project folder to the circleci user. This means the user will be able to add and remove files from the directory.

Log in to the server with your key and the circleci user to make sure it works.

Go back to your the CircleCI dashboard and click ‘ADD PROJECT’ in the side navigation. Find your project and click ‘Set Up Project’ next to it. You’ll be shown some instructions that show you how to create a configuration file.

The following configuration will watch your master for changes, log into your server, go to your orject path and run yarn deploy. You’ll need to change the host name, project folder path and your deploy command to meet your requirements.

Add the config to /.circleci/config.yml and ggo back to the CircleCI dashboard. Click ‘Start building’ and watch CircleCI go through the steps specified in your configuration file.

Your project should fail because you haven’t given CircleCI your private key, so it can’t SSH into your server as the circleci user.

Click the ‘Project settings’ icon

Click SSH permissions

Click Add SSH Key

Go back to PuTTYGen and load your private key by clicking ‘Load’. Click ‘Conversions’ in the top options menu. Click ‘Export as OpenSSH key’ and save the key somwhere safe.

Open your key in notepad and copy the contents. Paste the contents into the ‘Add an SSH key’ dialog. You can add an optional hostname if you want to restrict the key to a hostname.

Go back to your failed build and click ‘Rerun workflow’.

Congratulations! You’ve setup CircleCI to automatically build on commits and merges into master.

Tom Nagle

Written by

Tom Nagle

I am a full stack JavaScript developer with Scrum and team lead experience. My preferred stack is Mongoose, TypeScript, Node.js, React & GraphQL.

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