Automatic Deployment Setup with CircleCI (Ubuntu)
What will you learn in this guide?
In this guide, you will learn how to automatically distribute codes sent to GitHub to the Ubuntu server using CircleCI.
We will establish an end-to-end CI/CD pipeline with a simple application based on Node.js.
What is CI/CD and Why CircleCI?
CI/CD automates the testing and deployment of code.
oaicite:0
stands out with its fast installation and simple pipeline structure.
**
oaicite:1
**integration is strong.
Prerequisites
Before you start you should have the following ready:
- A VPS with Ubuntu 18.04 (example:
tr1-node01) - GitHub account
- CircleCI account
- An environment with Node.js and npm installed
- Basic Git knowledge
Creating a Local Node.js Project
In this step, we prepare a sample Node.js application.
mkdir circleci-test
cd circleci-test
npm init -y
- These commands create the project directory and start the npm configuration.
Simple HTTP Server
const http = require('http');
http.createServer((req, res) => {
res.write('Hello World!');
res.end();
}).listen(8080, '0.0.0.0');
- This code starts a simple web server on port 8080.
CircleCI Configuration File
- CircleCI reads transactions from the .circleci/config.yml file.
mkdir .circleci
nano .circleci/config.yml
version: 2.1
jobs:
deploy:
docker:
- image: arvindr226/alpine-ssh
steps:
- checkout
- run: ssh -oStrictHostKeyChecking=no $USER@$IP "./deploy.sh"
workflows:
deploy-workflow:
jobs:
- deploy:
filters:
branches:
only: main
- This configuration only distributes the codes coming to the main branch.
Submitting the Project to GitHub
git init
git add .
git commit -m "ilk commit"
git branch -M main
git remote add origin https://github.com/kullanici_adi/circleci-test
git push -u origin main
- These steps will push the project to the GitHub repository.
Creating a Deployment User on the Server
- A separate user is used for security.
sudo useradd -m -d /home/circleci -s /bin/bash circleci
sudo ufw allow 8080
- These commands create the circleci user and open the port.
Generating SSH Key
- Required for CircleCI to connect to the server.
ssh-keygen -m PEM -t rsa -f ~/.ssh/circleci
-
This command generates a CircleCI compatible SSH key.
-
The public key is added to authorized_keys on the server.
CircleCI Project Settings
-
In the CircleCI panel:
-
Add the project from GitHub
-
Select Use Existing Config
-
Enter private key as SSH Key
-
Add Environment Variables:
-
USER = circleci
-
IP = server_ip_address
deploy.sh File on the server
- This file works in every deployment.
#!/bin/bash
cd ~/circleci-test
git pull origin main
npm install
pm2 restart app
chmod +x deploy.sh
- This script pulls the code, installs the dependencies and restarts the application.
Deployment Testing
- Make a small change to the code:
res.write('Foo Bar!');
git add .
git commit -m "deploy testi"
git push origin main
- CircleCI pipeline runs automatically after push.
Frequently Asked Questions (FAQ)
** Why does CircleCI use SSH?** Provides secure and automatic access to the server.
2. Is deploy.sh mandatory? No, but it simplifies the process.
3. Why was sudo privilege not granted? To reduce security risk.
4. Is pm2 required? No, but it is recommended for Node.js.
Result
Now every code pushed to GitHub is automatically deployed to your Ubuntu server.
You can safely scale your projects by using this structure in the GenixNode infrastructure.

