Real Time Simple Node Js Application CI/CD

Introduction

In this guide, we'll explore how to set up a Continuous Integration and Continuous Deployment (CI/CD) pipeline for a simple Node.js application. This process will help automate testing and deployment, ensuring your application is always up-to-date and functioning correctly.

Description

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They automate the process of testing and deploying code changes, allowing developers to focus on writing code rather than managing deployments. In this guide, we'll set up a real-time CI/CD pipeline for a Node.js application, making it easier to deliver updates quickly and reliably.

Steps Overview

Step 1: Set Up Your Node.js Application

  • Create a simple Node.js application or use an existing one.

  • Ensure your application is stored in a version control system like Git.

Step 2: Configure a CI/CD Tool

  • Choose a CI/CD tool such as Jenkins, GitHub Actions, or GitLab CI.

  • Set up the tool to monitor your repository for changes.

Step 3: Write Test Scripts

  • Create test scripts to verify your application's functionality.

  • Ensure these scripts run automatically whenever code changes are detected.

Step 4: Automate Deployment

  • Configure your CI/CD tool to deploy your application to a staging or production environment after successful tests.

  • Use tools like Docker or Kubernetes for containerized deployments, if applicable.

Step 5: Monitor and Iterate

  • Continuously monitor the deployment process for any issues.

  • Make improvements to the pipeline as needed to enhance efficiency and reliability.

Steps in Detail: Plugins to Install in Jenkins

  • Node.js Plugin

  • OWASP Dependency Check

  • Docker, Docker Pipeline, CloudBees, Docker BuildStep

Configure Jenkins → Manage Jenkins → Tools

  • Dependency-Check

  • Docker

Successfully Uploaded the image in registry with help of pipeline script

Running the Node js web app:

pipeline {
    agent any

    tools {
        nodejs 'nodejs-10'
    }

    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'main', changelog: false, poll: false, url: 'https://github.com/amitsinghs98/demo_nodejs_webpage.git'
            }
        }

        stage('Install Dependencies') {
            steps {
                sh "npm install"
            }
        }
          stage('OWASP Dependency Check') {
            steps {
                // Run OWASP Dependency-Check scan on the current directory
                dependencyCheck additionalArguments: '--scan ./ ', odcInstallation: 'DP'
                // Publish the Dependency-Check report
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
        stage('Docker Build and Push ') {
            steps {
               script{
                   withDockerRegistry(credentialsId: 'dockerhnew') {

                    sh 'docker run -d --name demo-nodejs -p 8081:8081 amitsinghs98/nodejsdemo1:latest'


                   }
               }
            }
        }
    }
}

Go to URL: localhost:8081

Summary

By following these steps, you can establish a robust CI/CD pipeline for your Node.js application. This setup will help you automate testing and deployment, ensuring that your application is always ready for users. With a real-time CI/CD pipeline, you can focus more on developing features and less on managing deployments, leading to a more efficient development process.