How to Setup the CI/CD pipeline with Jenkins and GitHub for a Node Js application

Here's a step-by-step guide to setting up the CI/CD pipeline using Jenkins and GitHub for a Node JS application, triggering code deployment for both Public and Private GitHub Repositories.

What is Jenkins?

Jenkins is an open-source automation tool used for continuous integration and delivery. We can set up a ci/cd pipeline for us. It's free and easy to use and can be installed on a server or local host. It's primarily written in Java, making it robust, scalable, and flexible to use with different platforms like Windows, Linux, and Mac.

Jenkins is the most common open-source tool used in most CI/CD pipelines with DevOps methods.


What is GitHub?

GitHub is a cloud service that uses Git and is backed by Microsoft. It gives us the power to store, maintain the codebase, and collaborate with team members Worldwide. It has a paid version, but the free version is enough for most cases. It works to link a central server where all the team members can push and pull codes, it syncs our local git Repositories with the GitHub server.

How to set up the CI/CD pipeline with Jenkins and GitHub for a Node JS application?

Now we will use the Jenkins and GitHub combination to auto-deploy a Node JS application in production. Will trigger a build when the code is pushed to GitHub and all the work will be done by Jenkins to build and run the application.

Steps for setting up CI/CD for a Node Js application

  • Installing Java
  • Install Jenkins on a Ubuntu Server
  • Accessing GitHub Public Repositories
  • Make access token for securely accessing GitHub Private Repositories
  • Triggering automated build when code is pushed to a GitHub Repo
  • Installing and building Node JS application with npm
  • Running the Node Js application with pm2
  • Setting up Apache2 reverse proxy for connecting our node js app with a domain
  • Install Jenkins on a Ubuntu Server

Here we will use a Ubuntu Server. First, you must log in as a root user using SSH and follow these commands to install Jenkins.


Installing Java

Jenkins needs Java to run, so if you don't have Java installed then install it using these commands

sudo apt update
java -version
sudo apt install default-jre
sudo apt install default-jdk
java -version


Installing Jenkins

Once you have Java installed then follow these commands to install Jenkins.

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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
sudo apt-get update
sudo apt-get install jenkins


Install Jenkins

sudo systemctl enable jenkins
Check Jenkins Status

sudo systemctl status jenkins


Allow Jenkins in ufw firewall

Set up the ufw firewall to allow Jenkins to run. By default, Jenkins runs on port 8080, so we need to allow it.

sudo ufw status
sudo ufw allow OpenSSH
sudo ufw allow 8080 #jenkins runs on 8080 b default
sudo ufw enable

Now open your server it 8080 port http://<ip>:8080/, you should see jenkins web interface


Initial setup for Jenkins

It will prompt you to enter the initial password, that you can get from the following command enter it and continue with the recommended installation.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword


Access GitHub Repo.

GitHub gives us two types of repositories, public and private. For the public, we don't need any special setup. But for private we need to generate a Personal Access Token. You can generate this by going to https://github.com/settings/tokens page and selecting the classic generate a new token, giving the token name and expiry (By Default it is valid for 90 days)

Generate and copy the token, it will be available for once to copy and store securely. We need this to access the GitHub Private repo. in Jenkins.