Let's create a CI/CD Pipeline for Node JS Application.
Fork the repository:
https://github.com/LondheShubham153/node-todo-cicd
Create a connection to your Jenkins job and your GitHub Repository via GitHub Integration.
Start Jenkins on your machine. If Jenkins is not installed, download it from here.
Create Freestyle project [todo_node_app].
Set the project configurations as follows.
Set Description as you like.
Check the Check-box of GitHub project and add Project url.
In Source Code Management, set the following.
Since we are integrating with GitHub, we will set the Credentials parameters as follows.
For the private key, we will create an SSH key-pair.
ssh-keygen
Now it's time to give the public key to GitHub.
Go to your GitHub profile settings.
Go to SSH & GPG keys, Click-on New SSH Key.
Add title and paste your public SSH key, and click on Add SSH Key.
Add your private key here.
Use the newly created credentials and click save.
Wow, Jenkins and GitHub integration is complete. ๐
Click-on Build now.
Code is successfully cloned onto your Jenkins workspace.
Now it's time to make sure our Node application runs. To do that, we will perform some manual tasks that we will automate later.
Manually run the project.
Install the project dependencies.
Follow the README.md file for installation.
After installation, run node app.js to check if the application is running.
As everything is working fine, let's take it further. ๐
Dockerize the application.
Create a
Dockerfile
in the root directory of your project, or you can use theDockerfile
that is already there.Add the following content to the
Dockerfile
:FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8000 CMD ["node", "app.js"]
Build the Docker image by running the following command in your terminal:
docker build -t todo_node_app .
Run the Docker container:
docker run -p 8000:8000 todo_node_app
Your application should now be running inside a Docker container.
Time for some automation.
Remember, we created a free style project [todo_node_app] earlier.
Go to its configuration, under Build Steps -> Add Build step -> Execute shell.
Click save.
Run Build again.
Under Console Output, you should see SUCCESS, and your application will be running.
You may be wondering ๐ค, we still have to click on Build Now to trigger the CI/CD pipeline. This is not fully automated yet. To make it completely automated, we will create a webhook to trigger the build for us.
Create a webhook.
Go to Manage Jenkins ->Plugins -> Available Plugins and search github integration and installed it.
Now let's configure webhook.
Go to the node-todo-cicd project Settings -> Webhooks and configure the following.
Set the Payload URL and Content type, then click on Add Webhook.
Go back to Jenkins and check GitHub hook trigger for GITScm polling under Build Triggers and save it.
Now everything is set up. To trigger the build automatically, we need to commit changes in node-todo-cicd on GitHub.
You can update node-todo-cicd/views/todo.ejs file.
After committing changes, you will see the build automatically triggered in Jenkins.
Thank you for reading๐.