Setting up a Continuous Integration and Continuous Deployment (CI/CD) pipeline with Jenkins and Node.js involves automating the testing and deployment processes for your Node.js applications. Here's a step-by-step guide to help you achieve this:
Prerequisites:
-
Jenkins Installed:
Ensure that Jenkins is installed on your server. You can download Jenkins from the official website: Jenkins Download.
-
Node.js Installed:
Make sure Node.js is installed on the server where Jenkins is running.
-
Install Jenkins Plugins
Jenkins requires specific plugins to work with Node.js and Git. Install the following plugins:
- NodeJS Plugin: Allows you to use Node.js in your build processes.
- Git Plugin: Integrates Jenkins with Git repositories.
Install these plugins from the Jenkins dashboard by navigating to
Manage Jenkins > Manage Plugins > Available
and search for the plugins. -
Set Up Node.js in Jenkins
- Navigate to
Manage Jenkins > Global Tool Configuration
. - Look for the section named
NodeJS
and click onNodeJS Installations....
- Add a new NodeJS installation and provide the necessary details, including the name and the path to the Node.js executable. Save your changes.
- Navigate to
-
Create a New Jenkins Job
- Click on
New Item
on the Jenkins dashboard. - Enter a name for your project and select
Freestyle project
. - Under the
Source Code Management
section, chooseGit
and provide the repository URL. - In the
Build
section, add a build step:- Choose
Invoke NodeJS
as the build step. - Select the Node.js version you configured earlier.
- Add the build script, e.g.,
npm install or yarn install
.
- Choose
- Click on
-
Set Up Testing
Add a testing step to your Jenkins job. Depending on your testing framework (e.g., Jest, Mocha), add the corresponding test command in the build steps. For example:
npm test
-
Set Up Deployment
- In the Jenkins job configuration, add a post-build action.
- Choose
Send build artifacts over SSH
. - Configure the SSH server details where your Node.js application will be deployed.
- Specify the source files to be deployed, e.g.,
**/*.js
or the entire project.
-
Webhook Setup (Optional)
If you want Jenkins to trigger builds automatically whenever you push changes to your Git repository, set up a webhook:
- In your Git repository, go to
Settings > Webhooks
. - Add a new webhook with the Jenkins build trigger URL.
- In your Git repository, go to
-
Save and Build
Save your Jenkins job configuration and manually trigger a build to ensure everything is set up correctly. If the build and tests pass, the application will be deployed automatically.
-
Monitor Builds
Monitor your Jenkins dashboard for build results. If the pipeline is set up correctly, Jenkins will automatically trigger builds on new commits.
Remember that this is a basic setup. Depending on your project's complexity, you may need to customize the pipeline further, possibly integrating additional tools such as Docker for containerization or other testing tools.
Always refer to the Jenkins documentation for detailed information on plugins and configuration options: Jenkins Documentation.