Day 24 : Deploy a Node JS Application On Docker Using Jenkins and Github Integrations.
Day 23 was all about Jenkins CI/CD, make sure you have done it and understood the concepts. As today You will be doing one Project End to End and adding it to your resume.
In this article, Let’s make a beautiful CI/CD Pipeline for your Node JS Application. In this blog, We will discuss GitHub WebHooks and an end-to-end Jenkins CI/CD project to continuously integrate and deploy a node.js application.
What are GitHub Webhooks?
Git webhooks are a feature of Git version control systems that allow you to trigger custom actions or notifications in response to specific events that occur within a Git repository. When certain events happen, such as pushing code to a repository, creating a new branch, or merging a pull request, a webhook can be set up to automatically send an HTTP POST request to a specified URL.
Git webhooks are typically used to integrate Git repositories with other tools, services, or custom scripts, enabling you to automate various tasks or receive notifications about repository events. Some common use cases of Git webhooks include:
1)Continuous Integration/Continuous Deployment (CI/CD): Webhooks can trigger CI/CD pipelines to automatically build, test, and deploy code whenever changes are pushed to a repository. This allows for streamlined development processes and faster feedback loops.
2) Code review and quality assurance: Webhooks can notify code review tools or quality assurance systems about new changes or pull requests, enabling automated code reviews, static code analysis, or other quality checks.
3) Issue tracking and project management: Webhooks can update issue tracking systems or project management tools when specific events occur in a repository. For example, creating a new issue or updating the status of an existing issue based on pull requests or commit messages.
Task-01
- Fork [this](https://github.com/LondheShubham153/node-todo-cicd.git) repository:
- Create a connection to your Jenkins job and your GitHub Repository via GitHub Integration.
- Read About [GitHub WebHooks](https://betterprogramming.pub/how-too-add-github-webhook-to-a-jenkins-pipeline-62b0be84e006) and make sure you have CICD setup
- Refer [this](https://youtu.be/nplH3BzKHPk) video for the entire project
Step A) Forking the repository from https://github.com/LondheShubham153/node-todo-cicd.git
Step B) Add WebHook
Go to the settings page for your repository > Click on the “Webhooks & Services” tab
In the webhook configuration, you will need to provide the following information:
- Payload URL: This is the URL that will receive the webhook notifications.
- Events: Select the events that you want to be notified about.
- Secret: This is a secret key that will be used to verify the authenticity of the webhook notifications.
Configure GitHub Webhook in the meantime. In the repository settings > Go to Webhooks > Add Webhook
The Payload URL will be in the format: jenkins-URL/github-webhook.
For example, my payload URL will look like this:
http://18.144.37.229:8080/git-webhook
Change content type to application/json.
We were facing error “Invalid error response 403"
To solve this we executed below cmd on ec2
ngrok http 8080
Copy the URL and paste in github payload
For me, Webhook is successfully integrated.
Step C ) Click on “New item” in Jenkins Dashboard.
Scroll down to Source Code Management on the Configure page and paste the Repository URL and the branch your code is one. For me, it’s the master branch.
In Add Build Step Click Execute Shell
sudo docker build . -t todo-node-app
sudo docker run -d — name todo-node-app -p 8000:8000 todo-node-app
Before that, Install Docker and “npm install npm” on your ec2 machine
To automatically trigger the pipeline when something changes in Github Repository
Now go to Jenkins > Dashboard > Job > Configure > Build Triggers > Select “GitHub hook trigger for GITScm polling”.
Step D) To change the code go to repository > Open views > Open todo.js file > Edit line number 17 to “This is Github webhook integration”
The pipeline build will start automatically
Check the status of application.
Task-02
Task 2: Run the application using docker-compose.
- In the Execute shell run the application using Docker compose
— You will have to make a Docker Compose file for this Project (Can be a good open source contribution)
Step A) Add docker-compose file
Step B) In Jenkins go to Dashboard > Configure > Build Steps > And add the following commands:
docker-compose down
docker-compose up — build -d
Try accessing the application.
We can see that the container is created by using the docker ps command:
In this blog, We have completed an end-to-end Jenkins CI/CD project to deploy Node JS using GitHub Integrations. Hope this helps you with your resume and learning. Stay tunned for more DevOps related blogs.