CI/CD for Node.js application using Jenkins on EC2 — Part-2

Ankit Maheshwari
2 min readAug 3, 2023

In this article, we will host the Node.js app on an AWS EC2 instance. Furthermore, we will use the automation server Jenkins which was hosted on a separate AWS EC2 instance, which was done in part-1. So there will be two EC2 servers one is for Jenkins and the other one is for the Node.js app.

Jenkins will help us to automate the CI/CD process. For every code change from our Node.js app repository, Jenkins will get notified and it will pull the changes into our Jenkins server, install dependencies and run the integration test. If all tests pass, Jenkins is going to deploy the app to the node server. If it fails, developers will be notified.

Part — 1 of this article:

This Article will cover:

  • Setup GitHub/Bitbucket SSH Configuration
  • Adding a New SSH Key to our GitHub/Bitbucket account
  • Clone the repository

Prerequisites:

Before we start signup in GitHub or Bitbucket. and Create an application in Node.js

Setting up dependencies in the Node.js app:

Open package.json file from your project root folder and add the following dependencies into it:

npm install express --save
npm install mocha --save-dev
npm install supertest --save-dev
{
"name": "node-app",
"description": "hello my node-app",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon index.js"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"mocha": "^10.2.0",
"supertest": "^6.3.3"
}
}
  • express: Node.js framework
  • mocha: Test framework for node (You can choose another testing framework if you wish like Jasmin, Jest, Tape, etc)
  • supertest: Provide a high-level abstraction for testing HTTP

Now, create a new file in the project root called index.js and copy and paste the following code:

//importing node framework
var express = require(‘express’);

var app = express();
//Respond with "hello world" for requests that hit our root "/"
app.get(‘/’, function (req, res) {
res.send(‘hello world’);
});
//listen to port 3000 by default
app.listen(process.env.PORT || 3000);

module.exports = app;

The app is going to respond with “hello world” when requests hit our root URL (“/”).

Setup GitHub SSH Configuration

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

--

--