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
- Installing Jenkins
- Setting Up a Jenkins Pipeline
- Configuring the Pipeline Steps
- Running the Pipeline
- Conclusion
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A Linux-based operating system (Jenkins runs on Java, so it can be installed on Linux, macOS, and Windows)
- Node.js and npm installed on your system
- Basic knowledge of JavaScript
Installing Jenkins
To install Jenkins, follow these steps:
- Open your terminal and execute the following commands:
sudo apt update sudo apt install openjdk-8-jdk
- 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 -
- 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'
- Update your system and install Jenkins:
sudo apt update sudo apt install jenkins
- Start the Jenkins service:
sudo systemctl start jenkins
- Enable the Jenkins service to start on boot:
sudo systemctl enable jenkins
- 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:
-
In the Jenkins web interface, click on “New Item” to create a new project.
-
Enter a name for your project and select the “Pipeline” option.
-
Scroll down to the “Pipeline” section and choose “Pipeline script from SCM” as the Definition.
-
Select “Git” as the SCM (Source Code Management) and provide the repository URL.
-
Configure any additional SCM options if required (e.g., branch name, credentials).
-
Click on “Save” to create the pipeline.
Configuring the Pipeline Steps
Once the pipeline is created, we need to configure the pipeline steps:
-
In the Jenkins web interface, open your pipeline project.
-
Click on “Configure” to edit the pipeline steps.
- 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. - Click on “Save” to save the pipeline configuration.
Running the Pipeline
To run the pipeline manually:
-
In the Jenkins web interface, open your pipeline project.
-
Click on “Build Now” to trigger a manual build.
-
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: