Setting up a CI/CD pipeline with Jenkins and JavaScript

In today’s software development landscape, continuous integration and continuous deployment (CI/CD) are crucial for ensuring the quality and timely delivery of code. Jenkins, an open-source automation server, is widely used for building and deploying software projects. In this blog post, we will guide you through the process of setting up a CI/CD pipeline using Jenkins and JavaScript.

Table of Contents

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

Installing Jenkins

To install Jenkins, follow these steps:

  1. Open your terminal and execute the following commands:
    sudo apt update
    sudo apt install openjdk-8-jdk
    
  2. Next, add the Jenkins repository key to your system:
    wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
    
  3. Add the Jenkins repository to your system:
    sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    
  4. Update your system and install Jenkins:
    sudo apt update
    sudo apt install jenkins
    
  5. Start the Jenkins service:
    sudo systemctl start jenkins
    
  6. Enable the Jenkins service to start on boot:
    sudo systemctl enable jenkins
    
  7. To access the Jenkins web interface, open your favorite web browser and go to http://localhost:8080.

Setting Up a Jenkins Pipeline

Now that Jenkins is installed, let’s set up a pipeline:

  1. In the Jenkins web interface, click on “New Item” to create a new project.

  2. Enter a name for your project and select the “Pipeline” option.

  3. Scroll down to the “Pipeline” section and choose “Pipeline script from SCM” as the Definition.

  4. Select “Git” as the SCM (Source Code Management) and provide the repository URL.

  5. Configure any additional SCM options if required (e.g., branch name, credentials).

  6. Click on “Save” to create the pipeline.

Configuring the Pipeline Steps

Once the pipeline is created, we need to configure the pipeline steps:

  1. In the Jenkins web interface, open your pipeline project.

  2. Click on “Configure” to edit the pipeline steps.

  3. In the “Pipeline” section, define the stages and steps for your CI/CD pipeline. For example:
    pipeline {
      agent any
      stages {
     stage('Build') {
       steps {
         sh 'npm install'
       }
     }
     stage('Test') {
       steps {
         sh 'npm test'
       }
     }
     stage('Deploy') {
       steps {
         sh 'npm run deploy'
       }
     }
      }
    }
    

    In this example, we have three stages: “Build”, “Test”, and “Deploy”. Each stage consists of a single step that executes a shell command using the sh step.

  4. Click on “Save” to save the pipeline configuration.

Running the Pipeline

To run the pipeline manually:

  1. In the Jenkins web interface, open your pipeline project.

  2. Click on “Build Now” to trigger a manual build.

  3. Jenkins will start running the pipeline, executing each stage and step sequentially.

Conclusion

Congratulations! You have successfully set up a CI/CD pipeline using Jenkins and JavaScript. Jenkins provides a powerful and flexible platform for automating the build, test, and deployment processes of your JavaScript projects. By incorporating CI/CD into your development workflow, you can ensure faster and more reliable software delivery. Happy coding!

References: