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:
- Setup a circleci user on your server
- Add SSH keys so CircleCI can log in to your server
- Add a configuration file to your NodeJS application (could also be any other code base)
- 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.
adduser circleci --disabled-password
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.
chown -R circleci:circleci /home/circleci/
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.
chown -R circleci:circleci /var/www/MY_PROJECT_FOLDER
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.