Building a Jenkins Pipeline for Your Node.js Application: A Step-by-Step Guide

Testrig Technologies
6 min readAug 31, 2023

--

In the dynamic world of software development, where agility and reliability reign supreme, harnessing the potential of automation becomes paramount. Meet Jenkins, the virtuoso of automation orchestration, empowering developers worldwide to craft symphonies of continuous integration and continuous delivery for their Node.js applications.

Like a maestro leading a symphony, Jenkins conducts the harmonious collaboration of build, test, and deployment processes, ensuring impeccable code quality and rapid delivery.

About Jenkins

Jenkins is an open-source automation server that facilitates continuous integration and continuous delivery (CI/CD) of software projects. With its user-friendly web interface and extensive plugin support, Jenkins enables developers to automate the build, test, and deployment processes, providing quick feedback on code changes and streamlining application delivery. Its master-slave architecture allows for distributed builds, ensuring scalability and faster execution.

By defining build pipelines in code, Jenkins simplifies collaboration, version control, and reporting, making it a popular choice for teams seeking to improve development efficiency and maintain high code quality throughout the software development lifecycle.

In this blog, we will walk you through the step-by-step process of creating a Jenkins pipeline for your Node.js application.

Prerequisites

Before we start, ensure you have the following prerequisites in place:

A running Jenkins instance: You can install Jenkins locally or use a cloud-based Jenkins service.

Node.js and npm: Make sure Node.js and npm are installed on your Jenkins server.

Steps to Build Jenkins Pipeline for Node.js Application

Step 1: Set Up Node.js Environment

Start by configuring Node.js on your Jenkins server:

Install Node Version Manager (nvm) if your application supports multiple Node.js versions.

Install the required Node.js versions using nvm, e.g., nvm install <node_version>.

Set the default Node.js version with nvm use <node_version>.

Step 2: Install Jenkins Plugins

Ensure that you have the necessary Jenkins plugins installed to support Node.js and pipelines. Key plugins include:

Node.js Plugin: This plugin integrates Node.js with Jenkins.

Pipeline Plugin: It enables the creation of Jenkins pipelines.

Git Plugin: Required to connect to your Git repository.

Step 3: Create a New Pipeline Job

Initiate Jenkins and choose “New Item” to create a new pipeline job from scratch.

Choose the “Pipeline” alternative and assign a significant name to your job.

Create a Personal Access Token for GitHub by following these steps:

  • Open your web browser and log in to your GitHub account.
  • Go to your GitHub settings, then navigate to Developer Settings.

Click on “Generate New Token.”

  • Provide a name for the token and select your preferred expiration duration.
  • Configure Jenkins with the necessary access permissions as follows:

Please click the “Generate” button, and then make sure to take note of the token that is generated.

How To Add Token In Jenkins?

Within the configuration, indicate the URL of your Git repository housing the Node.js application code.

1. Choose “Script from SCM” option.

2. Select for “Git” in the SCM

3. Input the Repository URL

4. Press the “Add” button and choose Jenkins from the dropdown that appears.

Subsequently, provide the requested information.

Now, opt for the recently added username and password from the list of credentials.

5. Input the script’s relative path, such as ./src/index.js.

Step 4: Define Pipeline Stages

A pipeline consists of various stages. Here’s a basic pipeline structure for a Node.js application:

Step 5: Set Up Deployment Phase

At this point, Jenkins will initiate the deployment of the application to your designated cloud platform. You have the freedom to include all the commands that you would typically use for manual deployment. For instance, deploying on Heroku involves:

  • Logging in to Heroku Shell
  • Adding a remote connection to your repository
  • Pushing the code to Heroku

Once you have incorporated these three stages into your Jenkinsfile, the overall structure should resemble something along these lines:

And there you have it — your code has been seamlessly pushed to Heroku! If you haven’t already included a “Procfile” in your codebase, make sure to add one and then trigger the project build through Jenkins. This should initiate the deployment process for your code.

Remember, the “Procfile” must contain the command to launch the application, such as:

web: node server.js

With these steps completed, your code should now be successfully deployed and up and running.

Step 6: Configure Environment-Specific Settings

For environment-specific settings, you can use Jenkins environment variables or Jenkinsfile parameters to manage configurations like database connection strings, API keys, etc.

Step 7: Implement CI/CD

If you want to implement Continuous Integration (CI) and Continuous Deployment (CD), set up triggers to automatically start the pipeline on each code commit. You can use webhook integrations or polling mechanisms.

Step 8: Security and Credentials

Secure sensitive data using Jenkins Credentials or plugins like HashiCorp Vault integration. Avoid storing credentials directly in the Jenkinsfile.

Step 9: Notifications

Set up notifications to keep the team informed about the pipeline status. Configure email notifications or integrate with messaging platforms like Slack.

Step 10: Error Handling and Cleanup

Implement error handling and cleanup steps in your pipeline to ensure it doesn’t leave resources in an inconsistent state.

Step 11: Test the Pipeline

Test the pipeline by making changes to your Node.js application code and verifying that the pipeline executes as expected.

Conclusion

By following this step-by-step guide, you have successfully created a Jenkins pipeline for your Node.js application. The pipeline will automatically build, test, and deploy your application, streamlining your development process and ensuring a higher level of application quality. Embrace automation, and let Jenkins do the heavy lifting while you focus on crafting amazing Node.js applications.

Embracing automation testing proves indispensable in tackling the challenges of quality assurance. By adhering to meticulous best practices and a systematic approach, organizations can guarantee that their applications remain contemporary, trustworthy, and finely tuned. Nonetheless, adeptly orchestrating automation testing can pose difficulties for those lacking the adeptness or resources. This is where Testrig Technologies steps in.

With a wealth of experience and a proficient workforce, we furnish businesses with adept methodologies and apt resources for the seamless execution of automation testing. Through collaboration with us, you stand to economize time and resources, elevate your application’s functionality and efficiency, and ensure unwavering consistency and dependability.

Seeking assistance? Connect with the forefront Automation Testing Company to delve into optimal practices and the manifold advantages of integrating automation testing.

--

--

Testrig Technologies
Testrig Technologies

Written by Testrig Technologies

As an independent software testing company, we provide modern quality assurance and software testing services to global clients.

Responses (1)