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
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.
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.
Git installed: Git should be installed on your Jenkins server to clone repositories.
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
Login to Jenkins: Open a browser and navigate to your Jenkins instance (
http://localhost:8080
by default).Install Plugins: Go to
Manage Jenkins
>Manage Plugins
. Under theAvailable
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
Go to
Manage Jenkins
>Global Tool Configuration
.Scroll down to the NodeJS section and click
Add NodeJS
.Enter a name, e.g.,
NodeJS 14.x
, and select the installation method (e.g.,Install automatically
and choose a version).Save the configuration.
Step 3: Create a New Jenkins Pipeline Project
On the Jenkins dashboard, click
New Item
.Enter a project name (e.g.,
NodeJS Project Pipeline
), selectPipeline
, and clickOK
.In the project configuration page, under the Pipeline section, choose
Pipeline script from SCM
.Choose
Git
as the SCM and enter the repository URL of your Node.js project. You can also specify credentials if needed.Specify the branch to build (e.g.,
*/main
).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
Save your
Jenkinsfile
and push it to the repository.Go back to your Jenkins project page and click
Build Now
.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
Go to your Git repository (e.g., GitHub) and navigate to the repository settings.
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