Introduction

In Today’s Software development world, automating the deployment process has become essential. In this article We’ll walk you through the steps of creating a reliable CI/CD pipeline for your Node.js application in this article. Using GitHub Actions, and then we’ll deploy your application to an AWS EC2 instance.

Step 1: Setup Node-js Project

kCab9 8YI1uib0EYZ9u328SsdNBGDaBvrvGv6qlbpeM2CpRxun2TdxGmBiDv7U6 eoGuqZnDRGvvhTVpBG75rs2fvNlNA5yR a433mbvrXfBEXWBZZreag7LKTS9XQAThf7o7bsn5sXkgaubgUQyncc

Step 2: Setting up AWS EC2

  • Launch an AWS EC2 instance using the Ubuntu Amazon Machine Image (AMI).
  • Create a PEM key pair for SSH access to your instance.
KZ1Oqepe5BmDyr3pg97bt8DxQkdcVO4ij gXjN2ZFMTXWKnovgz9xeiEgH133F1EZKRmDycp0CF7M7yvU e5Ok4v hZlZO6Qh8INVFN
XLk8WfUhsQ32L5FJ2HSqvg TIHt63xoKHZ59Pz2T7Kyqom0izPlZkJhPgFP mqJkMddxB8fPp7bazmDEHWiRrrGbQBBHVxmdX6ZV3KKR VG6SitzSsWipGPH5gD q9Y qqcR4YXtjnEN3wJjuWUcw1w
XLk8WfUhsQ32L5FJ2HSqvg TIHt63xoKHZ59Pz2T7Kyqom0izPlZkJhPgFP mqJkMddxB8fPp7bazmDEHWiRrrGbQBBHVxmdX6ZV3KKR VG6SitzSsWipGPH5gD q9Y qqcR4YXtjnEN3wJjuWUcw1w
  • Configure inbound rules in the security group to allow SSH (port 22), HTTP (port 80), Custom TCP (port 3000) access.
yfL1XRt 20SKYzXsLKQtaHK7X64hNlVGdWkx8w6ADLZsGOMgeTYb vSSJKmzUxSp865OF6Ib9wgR5kuGGpxvbksHk20eLxSO8WI ZoUW27hqZlTEwLnxW6cEQBAdnBEDbfie bYkcool7LBlDWlis6g=w547 h227

Step 3: GitHub Actions Setup

  • Go to your GitHub repository and navigate to the “Actions” tab.
  • Create a new workflow and select the Node.js template.
L 752tr9EXNYFR4y7D6I8wWMwB2nKoCMPKAHFIHwMhvlWzY94xxHS7iCPk jNzvPMvy3e1SLluYi4sls1RduCqDTaVNBv2x9JiyIvcdzFCjIRuPCvJczcyImhX6b63Wix7M9TVpgtxOEcVSK2i197 g
  • Configure the workflow with the necessary GitHub Actions YAML file to automate the deployment process.
jR6VGFA

1. On: Section

This section defines when the workflow should be triggered. In this case, the workflow will be triggered whenever there is a push (commit) to the “main” branch of the repository. It means that when code changes are pushed to the “main” branch, this workflow will run

2. Runs-on: self-hosted

This line specifies the type of runner that the job will run on. In this case, it’s using a self-hosted runner, which means the workflow will run on a machine or server set up by the repository owner rather than GitHub’s own hosted runners.

3. Strategy: node-version

This section defines a matrix strategy for the job. It allows you to run the same job multiple times with different configurations. In this case, it’s using a matrix to specify the Node.js version to be used for the job. It’s set to run with Node.js version 18.x.

Step 4: Connecting to Your EC2 Instance

  • Copy the PEM key you generated earlier and use it to connect to your EC2 instance.
0LmGQIqtppGsFmyB Mxxjvf3os75bHru4gEBGA9z6npQvudZCXV5PLmRwgK8GkFXc JLX96GNZnoxfATByqTKXBjH thFwS4ungRbOlo7Bgu1 bmCLrI7xAS3j73pSkJtPcFwrW2hm9Zm BqIN 7Yw

Step 5:  Self-hosted GitHub Runner Setup

Follow these steps as shown in image to create a self-hosted GitHub runner on your EC2 instance.

QMk5BWMIz M1hSxg13ycDX92cxm2zVrooxxaC7 Nisfv2vjK0tqC1Mg0h6KR72uZ384LRb0CHHAkZa7cYR0CPjuYXaWeWxbx7JurY2VGdi4y90qQYfK V6F9tfrVy0O04T7geveTibtLS9vn9LKieJM=w204 h317
TS464d3yPo fXP7zev8N9JoH1M0RRM1Bmbk mOUT jzXoVQoL81aQw61MC JtfeU5I4jL11UflGwBhW9r1Wt yj iSXALZ2pASz5ZD 8B2gP0jjPcajB0UlpL 6 dO86XXWpEiWuIKdT7JFdNI0sw4
y1L0nSbm4bmNEtuuylf4BsN4OihlYijH2X

Note– mkdir actions-runner && cd actions-runner– change the folder name for avoiding confusion.

Step 6: Installing Node.js and Nginx

  • On your EC2 instance, run the following commands

$ sudo apt update

$ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash –

$ sudo apt-get install nodejs

$ sudo apt-get install nginx

Step 7: Running the Node.js Application

  • Install PM2 to manage the Node.js application.

npm i -g pm2

pm2

  • Start and manage the Node.js application using PM2.

sudo ./svc.sh install 

sudo ./svc.sh start

  • After this, navigate to the _work folder on your EC2 instance.
  • This folder contains all the files, including your Node.js application.

cd _work

cd <your app_name>

pm2 start index.js

Step 8:  Configuring Nginx for Reverse Proxy

  • Inside the Nginx folder, navigate to sites-available and open the default file.
  • Ensure that you include the following configuration block to set up a reverse proxy for your Node.js application

location /api/ {

    proxy_pass http://localhost:3000; # Assuming your Node.js app is running on port 3000

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

And save it. And run the restart command – pm2 restart index.js –ss also paste this command in you yml file

kW8S 8TRKWvMD E5hk5SWVxslmMa0HaAZ6smEsZ0rBJzONoYO21Rpzb YQNnZbmE34ltTkpokBAyCuUQpAQpt I53ja2dbKlb8mq 80UMJvKp3DqA3fdLX6OgDnd P6tXWjO7pb3dHZImOhiTyy2v4A

This command will ensure that whenever the server restarts the runner will run, after this Restart Nginx to apply the new configuration

sudo systemctl restart nginx

And in the final copy the public ip address and paste in the browser as shown in the image below…

H6 gElRV MBhO8NSMBBIgNdKDFL3

Now whenever we do any changes in the code and push the code on your repository, the changes will reflect on the server automatically.

2nNTC9xpyBSCiK klRCYSS KX0WL2sAK14cnbkY2 dNJ6XFC R1Cl 2FCZqbAjLjZSg1XAE5U6yLstkJ2zV5AhhvuK0UWAuYahxqieVSBBPIIOV Fm6Xmt6FIwBwvkifJbaU2jPS2GVCbBf478aRjM=w386 h93

By: Divyanshu Bhati

leave a comment

Your email address will not be published. Required fields are marked *