Project #1 : Automated CI/CD pipeline for NodeJS application

Flamy MattewadaFlamy Mattewada
5 min read

In this project, we will use Jenkins, Docker, and AWS to build an automated CI/CD pipeline for a simple NodeJS application. Every time you push new code to your GitHub repository, this pipeline will automatically build, test, and deploy the changes to your AWS EC2 instance.

Prerequisites:

  1. An account in AWS and GitHub.

  2. Knowledge on AWS EC2 instance, Git, Jenkins and Docker.

Step 1: Create an AWS EC2 instance.

Login to your AWS account and go to console then search for EC2 service click on Launch instance. Here I am using ubuntu AMI, instance type as t2.micro, used a key pair to securely connect to our instance and Network settings as per the screenshot below.

No alt text provided for this image

No alt text provided for this image

I have set the inbound rules for the security group as below.

No alt text provided for this image

Step 2: Install Jenkins.

Connect or SSH into the EC2 Instance and enter below commands to install Jenkins. Install all the updates

sudo apt update

Java-built open-source Continuous Integration (CI) and CD platform. Hence we install java first.

sudo apt install openjdk-17-jre

To know the version of java, type the command "java -version"

java -version
Openjdk version "17.0.7" 2023-04018
OpenJDK Runtime Environment (build 17.0.7+7-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 17.0.7+7-Debian-1deb11u1, mixed mode, sharing) 17.0.7+7-Debian-1deb11u1)

Fetching Jenkins from the below url to your EC2 instance.

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee 

 /usr/share/keyrings/jenkins-keyring.asc > /dev/null

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \

 https://pkg.jenkins.io/debian-stable binary/ | sudo tee \

 /etc/apt/sources.list.d/jenkins.list > /dev/null\

To upgrade remaining packages

sudo apt-get update

To install Jenkins

sudo apt-get install jenkins

To enable Jenkins

sudo systemctl enable jenkins

To start Jenkins

sudo systemctl start jenkins

Unlocking Jenkins

When you first access a new Jenkins instance, you are asked to unlock it using an automatically-generated password.

  1. Browse to http://public_IP_address_of_EC2_instance:8080 (or whichever port you configured for Jenkins when installing it) and wait until the Unlock Jenkins page appears. If you are browsing in local machine, use http://localhost:8080

No alt text provided for this image

You can see the initial password by running the “systemctl status Jenkins”. (Check the screenshot below).

No alt text provided for this image

OR

You can get the initial password at the location : /var/lib/jenkins/secrets/initialAdminPassword

No alt text provided for this image

Once you enter the initial password. Click on install plugins and you will get below screen. Fill in the details and click on save and continue.

No alt text provided for this image

Jenkins will be ready and you will see home page as below.

No alt text provided for this image

Step 3: GitHub and Jenkins Integration:

Select New Item from the top left side of the screen. We can select as per our project requirement. Here we are choosing Freestyle project.

No alt text provided for this image

No alt text provided for this image

Step 4. Set up credentials:

Enter ssh-keygen to generate public and private keys. It has fetched all the files of git repository to the location : /var/lib/jenkins/workspace/myapp-pipeline. Lets check in our EC2 instance

No alt text provided for this image

No alt text provided for this image

No alt text provided for this image

No alt text provided for this image

No alt text provided for this image

No alt text provided for this image

Now this indicates our Jenkins is able to access the git repository successfully. It It has fetched all the files of git repository to the location : /var/lib/jenkins/workspace/myapp-pipeline. Lets check in our EC2 instance

cd /var/lib/jenkins/workspace/myapp-pipeline
ls

No alt text provided for this image

Step 4: Verify the code given by Developer.

Now all the files are in the expected location. Its time to install required libraries and check if code runs on our instance. We will get the list of required libraries list in the README.md file.

No alt text provided for this image

Installing nodejs to run our nodejs application.

sudo apt install nodejs

sudo apt install npm

sudo npm install

node app.js

No alt text provided for this image

Now open your browser and enter http://public_IP_of_EC2_instance:8000

My public IP is : 52.207.220.192 so I will enter : http:// 52.207.220.192:8000

No alt text provided for this image

No alt text provided for this image

I am able to see the output of nodejs application.

Great!! So the code given by the developer is running absolutely fine. Now we will containerize the application using docker.

Step5 :Containerization:

Installing Docker on EC2 Instance.

sudo apt install docker.io
sudo usermod -a -G docker jenkins
sudo systemctl restart jenkins 
sudo systemctl status jankins

No alt text provided for this image

Lets see what is in the Dockerfile

No alt text provided for this image

Before we setup this in Jenkins, lets check manually if we can containerize the application.

No alt text provided for this image

No alt text provided for this image

Docker Image has been built. Now lets containerize.

No alt text provided for this image

Application is perfectly containerized and the output is absolutely fine. Lets setup webhooks and integrate with Jenkins.

Step 6 : Automation

Install github-integrations plugin.

No alt text provided for this image

In the Github repo, go to settings of the repo and click on webhooks. Update Payload URL with the Jenkinslink/github-webhook/ Check the screenshot below:

No alt text provided for this image

Build Success

No alt text provided for this image

We have successful output:

No alt text provided for this image

Lets check if the automation works. I will delete the existing container, otherwise it throws an error.

Change the code and commit to git repo. As soon as the changes updated in the git the Jenkins will initiate build.

No alt text provided for this image

No alt text provided for this image

Thats all for Today!

Thank you :)/

0
Subscribe to my newsletter

Read articles from Flamy Mattewada directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Flamy Mattewada
Flamy Mattewada