Jenkins Pipeline for Automating the Deployment of a Node.js Application

Jenkins Pipeline for Automating the Deployment of a Node.js Application

Scenario Overview

Project Description: You have a Node.js application hosted in a Git repository. The goal is to set up a Jenkins pipeline that will automatically build, test, and deploy the application every time there is a push to the main branch.

Prerequisites

  1. Jenkins installed on Windows: You should have Jenkins installed and running on a Windows machine. If not, you can download and install it from the official Jenkins website.

  2. Node.js and npm installed: Ensure Node.js and npm are installed on your Jenkins server. You can download it from the official Node.js website.

  3. Git installed: Git should be installed on your Jenkins server to clone repositories.

  4. A Node.js project repository: This tutorial assumes you have a Node.js project hosted in a Git repository (e.g., GitHub, GitLab, Bitbucket).

Note: You can download the NodeJs application and Jenkinsfile from my simple-nodejs-app GitHub repo.

Step 1: Install Required Jenkins Plugins

  1. Login to Jenkins: Open a browser and navigate to your Jenkins instance (http://localhost:8080 by default).

  2. Install Plugins: Go to Manage Jenkins > Manage Plugins. Under the Available tab, search for and install the following plugins:

    • NodeJS Plugin: This allows Jenkins to run Node.js commands.

      Carry out the same step to install Git Plugin and Pipeline Plugin, if you don't have it installed already.

    • Git Plugin: This allows Jenkins to interact with Git repositories.

    • Pipeline Plugin: This enables Jenkins to use pipeline scripts.

Step 2: Configure NodeJS in Jenkins

  1. Go to Manage Jenkins > Global Tool Configuration.

  2. Scroll down to the NodeJS section and click Add NodeJS.

  3. Enter a name, e.g., NodeJS 14.x, and select the installation method (e.g., Install automatically and choose a version).

  4. Save the configuration.

Step 3: Create a New Jenkins Pipeline Project

  1. On the Jenkins dashboard, click New Item.

  2. Enter a project name (e.g., NodeJS Project Pipeline), select Pipeline, and click OK.

  3. In the project configuration page, under the Pipeline section, choose Pipeline script from SCM.

  4. Choose Git as the SCM and enter the repository URL of your Node.js project. You can also specify credentials if needed.

  5. Specify the branch to build (e.g., */main).

  6. In the Script Path field, enter the path to your Jenkinsfile (e.g., Jenkinsfile).

Step 4: Create a Jenkinsfile in Your Project

In your Node.js project repository, create a file named Jenkinsfile in the root directory. This file will define the stages of your pipeline. Below is a sample Jenkinsfile:

pipeline {
    agent any

    tools {
        nodejs 'NodeJS 14.x' // Use the NodeJS configuration from Jenkins
    }

    environment {
        CI = 'true'
    }

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/blessador/simple-nodejs-app.git'
            }
        }

        stage('Install Dependencies') {
            steps {
                script {
                    bat 'npm install'
                }
            }
        }

        stage('Run Tests') {
            steps {
                script {
                    bat 'npm test'
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    bat 'npm run build'
                }
            }
        }

        stage('Deploy') {
            steps {
                script {
                    // Deploy script or commands can be added here
                    bat 'echo Deploying the application...'
                }
            }
        }
    }

    post {
        always {
            echo 'Cleaning up...'
            cleanWs()
        }
        success {
            echo 'Pipeline completed successfully.'
        }
        failure {
            echo 'Pipeline failed.'
        }
    }
}

Step 5: Run Your Jenkins Pipeline

  1. Save your Jenkinsfile and push it to the repository.

  2. Go back to your Jenkins project page and click Build Now.

  3. The pipeline will start executing the defined stages:

    • Checkout: Clones the main branch of your repository.

    • Install Dependencies: Runs npm install to install Node.js dependencies.

    • Run Tests: Executes npm test to run the test suite.

    • Build: Runs the npm run build command to build the application.

    • Deploy: Placeholder for deployment commands (you can customize this step to deploy to a server or cloud environment).

Step 6: Viewing the Results

  • Click on the build number to see the console output and results of each stage.

  • Jenkins will show the pipeline graph with the status of each stage (success, failure).

Step 7: (Optional) Set Up Webhooks for Continuous Integration

  1. Go to your Git repository (e.g., GitHub) and navigate to the repository settings.

  2. Under Webhooks, add a new webhook with the following details:

    • Payload URL: http://your-jenkins-url/github-webhook/

    • Content type: application/json

    • Choose events: Just the push event.

This webhook will trigger the Jenkins pipeline automatically whenever there is a push to the main branch.

Final Thought

You’ve successfully set up a Jenkins pipeline to automate the build, test, and deployment of a Node.js application on a Windows environment. You can further customize the Jenkinsfile to include more stages or steps as per your project requirements, such as code quality checks, integration tests, or different deployment strategies.

I appreciate you reading this article, and I hope it has been insightful.

:Valentine Stephen