Deploying a Node.js Application in Jenkins through Docker

Deploying a Node.js Application in Jenkins through Docker

Introduction:

Deploying a Node.js application using Jenkins and Docker can streamline your development process, making it more efficient and automated. In this tutorial, we'll walk you through the steps to set up a Jenkins pipeline that builds and deploys your Node.js application using Docker containers.

Prerequisites:

Before you begin, make sure you have the following tools and resources ready:

  1. A Node.js application hosted on a version control system (e.g., GitHub).

  2. Docker installed on the server where Jenkins is running.

  3. A Jenkins server up and running.

  4. A basic understanding of Jenkins and Docker concepts.

Step 1: Setting Up ec2 Ubuntu machine in AWS:

Step 2: Setting Up Jenkins:

Install and set up Jenkins if you haven't already. You can download Jenkins from the official website and follow their installation instructions.

Jenkins Installation step

Step 3: Install Docker in Jenkins server:

ubuntu@ip-172-31-46-223:~$ sudo apt-get install docker.io

Step 4: Create a Freestyle project in Jenkins:

Step 5: Integratethis project with Jenkins:

In Source Code Management select git as a source

Step 6: Click Build Now:

Go inside this directory

cd /var/lib/jenkins/workspace/todo-node-app

Create Docker image

sudo usermod -a -G docker $USER
sudo docker build . -t node-app/

Restart the jenkins

sudo systemctl restart jenkins

Run the Docker Image

docker run -d --name node-todo-app -p 8000:8000 todo-node-app

Got to jenkins job, in Build Steps select execute shell and save the following command

docker build . -t node-app-todo
docker run -d --name node-app-container -p 8000:8000 node-app-todo

You will get this kind of output

Started by user Aditya Rai
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/todo-node-app
The recommended git tool is: NONE
using credential github-jenkins
 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/todo-node-app/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/AdityaRai1998/node-todo-cicd.git # timeout=10
Fetching upstream changes from https://github.com/AdityaRai1998/node-todo-cicd.git
 > git --version # timeout=10
 > git --version # 'git version 2.34.1'
using GIT_SSH to set credentials This is for github integration
Verifying host key using known hosts file
You're using 'Known hosts file' strategy to verify ssh host keys, but your known_hosts file does not exist, please go to 'Manage Jenkins' -> 'Configure Global Security' -> 'Git Host Key Verification Configuration' and configure host key verification.
 > git fetch --tags --force --progress -- https://github.com/AdityaRai1998/node-todo-cicd.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 06e984884388d106678899c88bf98f7e23d654f6 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 06e984884388d106678899c88bf98f7e23d654f6 # timeout=10
Commit message: "Update todo.ejs"
 > git rev-list --no-walk 06e984884388d106678899c88bf98f7e23d654f6 # timeout=10
[todo-node-app] $ /bin/sh -xe /tmp/jenkins5777721147702082529.sh
+ docker build . -t node-app-todo
Sending build context to Docker daemon  25.24MB

Step 1/7 : FROM node:12.2.0-alpine
 ---> f391dabf9dce
Step 2/7 : WORKDIR app
 ---> Using cache
 ---> 3895d0b75978
Step 3/7 : COPY . .
 ---> ecdc745a6d6a
Step 4/7 : RUN npm install
 ---> Running in dd96cc320e9d
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!

> ejs@2.7.4 postinstall /app/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)

npm WARN my-todolist@0.1.0 No repository field.
npm WARN my-todolist@0.1.0 No license field.

updated 291 packages and audited 291 packages in 10.957s
found 11 vulnerabilities (5 moderate, 4 high, 2 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container dd96cc320e9d
 ---> 434d9ff4fc54
Step 5/7 : RUN npm run test
 ---> Running in 3b8745d2bee3

> my-todolist@0.1.0 test /app
> mocha --recursive --exit



  Simple Calculations
This part executes once before all tests
    Test1
executes before every test
      ✓ Is returning 5 when adding 2 + 3
executes before every test
      ✓ Is returning 6 when multiplying 2 * 3
    Test2
executes before every test
      ✓ Is returning 4 when adding 2 + 3
executes before every test
      ✓ Is returning 8 when multiplying 2 * 4
This part executes once after all tests


  4 passing (12ms)

Removing intermediate container 3b8745d2bee3
 ---> 653ad7f117a7
Step 6/7 : EXPOSE 8000
 ---> Running in ebaf50e4cbf3
Removing intermediate container ebaf50e4cbf3
 ---> 2154506f2d98
Step 7/7 : CMD ["node","app.js"]
 ---> Running in 6f44898d1e9b
Removing intermediate container 6f44898d1e9b
 ---> 7efe51208260
Successfully built 7efe51208260
Successfully tagged node-app-todo:latest
+ docker run -d --name node-app-container -p 8000:8000 node-app-todo
f24533022b8a868c54efcda0a8df1bbcfbc347463e5843e6583814ce660eceeb
Finished: SUCCESS

Run the http://13.126.166.85:8000/

Create Webhook in Github

  1. Go to the project settings and select Webhooks

  1. In Payload URL section provide jenkins URL with /github-webhook/
http://13.126.166.85:8080/github-webhook/
  1. In Jenkins job configuration tab , select GitHub hook trigger for GITScm polling

  1. After committing a new changes in your Repository, Job will be runs automatically