Full Stack Development

Jenkins Build Automation: Complete CI/CD Guide for Node.js

6 November 20268 min

Automating builds increases development efficiency, reduces human error, and enables Continuous Integration (CI). This guide will walk you through setting up Jenkins Pipeline for your Node.js applications.

Overview

Overview

Purpose: Automate the build and deployment process of a Node.js application using Jenkins Pipeline.

What This Guide Covers:

Jenkins plugins installation
Pipeline job creation
GitHub repository integration
Automating dependency installation, build, and deployment

Step 1: Install Jenkins & Required Plugins

First, install Jenkins on your system. Then ensure these essential plugins are installed:

Required Plugins:

Pipeline - Core pipeline functionality
Git - Git repository integration
NodeJS Plugin - Node.js build environment
1Go to Manage Jenkins → Plugins
2Select the required plugins
3Click Install

Step 2: Create a Jenkins Pipeline Job

1Go to Jenkins Dashboard → New Item
2Enter your job name (e.g., "NodeJS-Automation")
3Select Pipeline
4Click OK

The configuration page will open for further setup.


Step 3: Configure GitHub Repository

If you're storing the Jenkinsfile in your repository:

1Under Pipeline → Definition, select Pipeline script from SCM
2Choose Git as the SCM
3Enter your GitHub Repository URL
4Add credentials if the repository is private
5Set the branch (e.g., main or master)
6Set Script Path to: Jenkinsfile

Step 4: Create the Jenkinsfile

Create a Jenkinsfile in your repository root with the following pipeline configuration:

Pipeline Structure:

pipeline - agent any - tools nodejs NodeJS-18

Stages:

1. Checkout Stage

checkout scm
echo Code checked out successfully

2. Install Dependencies Stage

sh npm install
echo Dependencies installed

3. Build Stage

sh npm run build --if-present
echo Build completed

4. Test Stage

sh npm test --if-present
echo Tests completed

5. Deploy Stage

sh nohup node app.js > app.log 2>&1 &
echo Application started

Post Actions:

always: archiveArtifacts artifacts *.log
success: echo Build successful!
failure: echo Build failed!

Step 5: Save & Run the Pipeline

1Click Apply & Save
2Hit Build Now to test
3View logs in Console Output

What This Jenkinsfile Automates

Checkout: Pulls latest code from Git repository

Install Dependencies: Runs npm install

Build: Runs npm run build if script exists

Test: Runs npm test if script exists

Deploy: Starts Node.js application in background

Post Actions: Archives logs and reports status


Best Practices

1Use Environment Variables - Store sensitive data in Jenkins credentials
2Add Notifications - Configure email or Slack notifications for build status
3Implement Webhooks - Set up GitHub webhooks for automatic builds on push
4Use Docker - Consider containerizing your application for consistent environments
5Add Quality Gates - Include linting and code coverage checks

Troubleshooting Common Issues

Build fails at npm install:

Check Node.js version compatibility
Verify package.json exists
Check network connectivity

Permission denied errors:

Ensure Jenkins user has proper permissions
Check file ownership in workspace

Application won't start:

Verify port availability
Check application logs for errors

Conclusion

This Jenkins Pipeline automates the entire build and deployment workflow:

Automatic code checkout from Git
Dependency management with npm
Build automation for production
Automated testing integration
Local deployment with logging
Artifact archiving for debugging

By implementing this CI/CD pipeline, you eliminate manual build steps, reduce errors, and enable faster, more reliable deployments.

Article Info

CategoryFull Stack Development
Read time8 min

Summary

Learn how to automate your Node.js application builds using Jenkins Pipeline. This guide covers plugin installation, pipeline creation, GitHub integration, and deployment automation.

Want to master this topic?

Explore Courses
Back to Blog