← Back to Projects

Jenkins CI/CD Pipeline for Node.js

Automated build, test, and deployment pipeline for Node.js applications with Docker Hub integration

Tools: Jenkins, Docker, Node.js, Docker Hub, Git, CI/CD, Linux.

Overview

The goal of this project is to create a complete Jenkins CI/CD pipeline by authoring a Jenkinsfile that automates the build, test, and deployment process for a Node.js application. The pipeline containerizes the application using Docker and publishes images to Docker Hub, enabling seamless deployment to development environments or Kubernetes clusters.

Project Objectives:

Step-by-Step Implementation

Jenkins Thumbnail

1Install Docker

The first step is to install Docker on your system. Docker installation instructions for various operating systems can be found at https://docs.docker.com/engine/install.

This implementation uses Ubuntu 22.04.1 LTS.

Verify Docker installation and service status

systemctl status docker
Docker service status verification

2Run Jenkins Container on Docker

Create a dedicated directory for Jenkins data persistence and configure appropriate permissions:

sudo mkdir -p /var/jenkins_home
sudo chown -R 1000:1000 /var/jenkins_home/

Run Jenkins container with persistent volume

docker run -dt --restart always \ -p 8080:8080 -p 50000:50000 \ -v /var/jenkins_home:/var/jenkins_home \ --name jenkins jenkins/jenkins:lts

This command pulls the latest Jenkins LTS image, maps the Jenkins volume to the host for data persistence, and uses the --restart always flag to ensure the container automatically restarts if it stops.

Confirm Jenkins container is running with: docker ps
Jenkins container running confirmation

3Configure Jenkins with Required Plugins and Tools

Initial Setup:

Retrieve the initial administrator password and access Jenkins through your browser at http://localhost:8080:

To get Jenkins initial admin password:

cat /var/jenkins_home/secrets/initialAdminPassword
Jenkins unlock screen

Install the suggested plugins (takes approximately 2 minutes):

Jenkins plugin installation

Create the first admin user and complete the initial setup:

Create Jenkins admin user
Welcome to Jenkins dashboard

Install Additional Plugins:

Three additional plugins are required for this CI/CD pipeline:

Navigate to: Manage Jenkins → Manage Plugins → Available Plugins

Search and install NodeJS plugin

Search for each plugin and click "Download now and install after restart".

Configure NodeJS in Global Tool Configuration:

Navigate to: Manage Jenkins → Global Tool Configuration

Add a NodeJS installation named "nodejs" (case-sensitive, as it will be referenced in the Jenkinsfile):

NodeJS global tool configuration

Configure Docker Hub Credentials:

  1. Create a Docker Hub account at https://hub.docker.com/signup
  2. Create a repository on Docker Hub for your project
  3. In Jenkins, navigate to: Manage Jenkins → Manage Credentials → Global Credentials → Add Credentials
Configure Docker Hub credentials

Enter your Docker Hub username and password, and set the credential ID as "dockerhub" (this will be referenced in the Jenkinsfile).

4Install Docker Client on Jenkins

To enable Jenkins to build Docker images, we need to install the Docker client inside the Jenkins container. This is accomplished using a custom Dockerfile available at https://github.com/valentineanagbogu/docker-client.git

Dockerfile for Jenkins with Docker client

The Dockerfile builds a new Jenkins image with Docker client installed:

Clone the repository

git clone git@github.com:valentineanagbogu/docker-client.git
cd docker-client
# Build the custom Jenkins image
docker build -t jenkins-docker .
Building custom Jenkins image

Replace the existing Jenkins container with the new image that includes the Docker client. Since the Jenkins home directory is persisted on the host, all plugins and configurations will be preserved:

# Verify the new image is built
docker image ls
# Stop and remove the current Jenkins container
docker stop jenkins
docker rm jenkins
List Docker images
Stop and remove Jenkins container

Run the new Jenkins container with Docker socket mounted:

# Start new Jenkins container with Docker client
docker run -dt --restart always \ -p 8080:8080 -p 50000:50000 \ -v /var/jenkins_home:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ --name jenkins jenkins-docker

# Verify Jenkins is running
docker ps
Verify new Jenkins container is running
Security Note: Mounting the Docker socket (/var/run/docker.sock) gives the Jenkins container full access to the Docker daemon. For production environments, consider using Docker-in-Docker (DinD) or implementing proper security measures.

5Configure Jenkinsfile to Build and Test Node.js Application

The Jenkinsfile defines the complete CI/CD pipeline workflow. It is stored in the project repository at misc/Jenkinsfile in the jenkins-nodejs repository.

Jenkinsfile configuration

Pipeline Stages:

  1. Preparation Stage: Checkout source code and capture Git commit ID for image tagging
  2. Test Stage: Install NodeJS dependencies and run automated tests (npm install and npm test)
  3. Docker Build/Push Stage: Authenticate with Docker Hub, build the Docker image, and push to the registry

Create Jenkins Pipeline Job:

On the Jenkins Dashboard, create a new pipeline item:

Create new Jenkins pipeline

Configure the pipeline to pull from Git and specify the repository URL:

Configure pipeline Git repository

Set the script path to misc/Jenkinsfile:

Set Jenkinsfile script path

Build and Verify:

Save the configuration and trigger a build. The pipeline executes successfully:

Successful pipeline build

Review the console output to see detailed build logs:

Console output part 1
Console output part 2

Verify the Docker image was successfully pushed to Docker Hub:

Image pushed to Docker Hub

6Deploy and Test the Application

Pull the built image from Docker Hub and deploy the containerized application:

# Login to Docker Hub
docker login

# Pull the image from Docker Hub (use your specific tag)
docker pull valentineanagbogu/jenkins-nodejs:8e841b7

# Verify the image is downloaded
docker image ls

# Run the container
docker run -dt -p 3000:3000 \ --name node-js-app \ valentineanagbogu/jenkins-nodejs:8e841b7

# Test the application
curl http://localhost:3000
Test application with curl

Access the application in a web browser:

Application running in browser
Success! The Node.js application is now successfully deployed from the Docker image built by the Jenkins CI/CD pipeline and published to Docker Hub.

Key Achievements

Technologies Used

Jenkins Docker Node.js Docker Hub Git Jenkinsfile CI/CD Ubuntu Linux Bash npm

Repository

View Project on GitHub →

Conclusion

This project demonstrates a complete implementation of CI/CD principles using Jenkins, one of the most widely-used automation servers in the industry. The automated pipeline eliminates manual build and deployment steps, reduces the risk of human error, and accelerates software delivery cycles.

By containerizing the application with Docker and publishing images to Docker Hub, the solution enables consistent deployments across different environments. The use of Jenkinsfile as Pipeline as Code ensures the CI/CD configuration is version-controlled alongside the application code.

The skills demonstrated in this project are fundamental to modern DevOps practices and directly applicable to enterprise software delivery workflows, microservices architectures, and cloud-native application development.

← Back to Projects Get in Touch