Project: Streamlining Node.js Application Deployment with Jenkins Declarative Pipeline

RizwanRizwan
4 min read

RIZWAN CHAUDHARY Blog

Follow

Rizwan

Project: Streamlining Node.js Application Deployment with Jenkins Declarative Pipeline

In today's fast-paced software development environment, automating the deployment process is not just beneficial but often necessary to maintain efficiency and reliability. Jenkins, a popular automation server, offers a powerful tool called Declarative Pipeline to streamline the deployment of applications. In this guide, we'll walk through the steps to set up a Jenkins Declarative Pipeline for a Node.js application using a practical example.

Prerequisites

Before diving into the setup process, ensure you have:

  • Installed Jenkins on your server. If you haven't done so already, you can follow (docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home: /var/jenkins_home jenkins/jenkins:lts)

Installing Docker

  1. Update Package Index: Before installing Docker, it's a good practice to update the local package index to ensure you have the latest version of available packages. Run the following command:

  2. Install Docker : update the package index again and install the latest version of Docker:

    COPY

      sudo apt update
      sudo apt-get install docker-io -y
    
  3. Verify Docker Installation: After installation, verify that Docker Engine is installed correctly by running the following command:

    COPY

      sudo systemctl status docker
    

    This command will show the Docker service status, indicating whether it's active and running.

Adding Jenkins to the Docker Group

  1. Check Jenkins User: Verify the username of the Jenkins user on your system. You can typically find this by running:

    COPY

      id -u jenkins
    
  2. Add Jenkins to Docker Group: Once you have the Jenkins user's username, add it to the Docker group using the usermod command. Replace <username> with the actual username of your Jenkins user:

    COPY

      sudo usermod -aG docker <username>
    
  3. Verify Group Membership: Confirm that the Jenkins user has been added to the Docker group by running:

    COPY

      groups jenkins
    

    This command should display a list of groups the Jenkins user belongs to, including docker.

  4. Restart Jenkins Service: To apply the group changes, restart the Jenkins service:

    COPY

      sudo systemctl restart jenkins
    

By following these steps, you'll have Docker installed on your system and the Jenkins user added to the Docker group, allowing Jenkins to interact with Docker and perform container-based operations seamlessly.

Setting Up the Pipeline

  1. Create a New Jenkins Project:

    • Log in to your Jenkins dashboard and click on "New Item" to create a new project.

    • Choose "Pipeline" and give your project a descriptive name and optional description.

  1. Configure the Pipeline:

    • In the project configuration, navigate to the "Pipeline" section.

    • Under "Definition," select "Pipeline script from SCM" and choose Git.

    • Provide the URL of your GitHub project: https://github.com/LondheShubham153/node-todo-cicd.

    • Check "GitHub hook trigger for GITscm polling" under "Build Triggers" to enable automatic builds upon code changes.

  2. Define the Pipeline Script:

    COPY

      pipeline {
          agent any
    
          stages {
              stage("code") {
                  steps {
                      git url: "https://github.com/mandgepratik/node-todo-cicd.git", branch: "master"
                      echo "Code cloned successfully"
                  }
              }
    
              stage("build") {
                  steps {
                      sh 'docker build . -t todo-app'
                      echo "Code build successfully"
                  }
              }
    
              stage("deploy") {
                  steps {
                      sh "docker run -p 8000:8000 -d todo-app" 
                      echo "Node.js application deployed successfully"
                  }
              }
          }
      }
    

Understanding the Pipeline Script

  • Agent: Specifies the machine Jenkins will execute the Pipeline on.

  • Stages: Divides the Pipeline into sequential stages.

    • Code: Clones the application code from the GitHub repository.

    • Build: Builds the Docker image for the Node.js application.

    • Deploy: Deploys the application container using Docker.

Executing the Pipeline

  1. Save the Pipeline Configuration.

  2. Click on "Build Now" to trigger the Pipeline.

  3. Jenkins will execute each stage sequentially.

  4. If all stages execute successfully, the deployment will be successful.

Verifying the Deployment

To ensure the deployment is successful:

  1. Enable Port 8000:

    • Ensure the security group of your server allows traffic on port 8000.
  2. Access the Application:

By following these steps, you've successfully set up a Jenkins Declarative Pipeline to automate the deployment process of a Node.js application. This not only saves time but also ensures consistency and reliability in your deployment workflow. Happy coding!

Subscribe to my newsletter

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

SUBSCRIBE

0
Subscribe to my newsletter

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

Written by

Rizwan
Rizwan