Let's make a beautiful CI/CD Pipeline for your Node JS Application 😍
Before starting, the project let's understand a little bit about GitHub webhooks and how we can connect between GitHub and Jenkins using webhooks....
What is GitHub webhook?
A GitHub webhook is a feature provided by GitHub that allows you to configure events and notifications for your repositories. It enables you to receive real-time information about specific activities and updates within your GitHub repository.
When you set up a webhook, GitHub sends HTTP POST requests to a specified URL endpoint whenever the selected events occur. The events could include actions such as code pushes, pull requests, issue creations, comment additions, and more.
The HTTP POST request contains JSON data that provides details about the event that occurred. You can use this data to trigger custom actions or integrations within your own systems, such as automatically building and deploying code, updating external services, or notifying team members about specific events.
To create a webhook, you typically need to specify the URL endpoint to receive the payloads, select the events you want to be notified about, and configure any required secret tokens or authentication mechanisms for secure communication between GitHub and your endpoint.
GitHub webhooks are widely used for various purposes:
Beyond continuous integration and deployment (CI/CD) pipelines,
Used in automated testing, issue tracking, and project management.
Deployment and Release Management, Notification and Chat Integration
Custom Workflows and Integrations, Security and Compliance more...
They provide a flexible and extensible way to integrate GitHub repositories with external systems and automate workflows based on repository activity.
Benefits of Jenkins GitHub Webhooks:
Jenkins GitHub Webhook provides an environment for Continuous Integration (CI) and is widely used in the DevOps process. A few benefits of Jenkins GitHub Webhook are listed below:
Jenkins GitHub Webhook makes the development process faster as it allows Developers to automatically perform a specified action when any commit is done to the source code.
It helps Developers detect issues and resolve them almost right away which keeps the application in a state to deploy safely.
Jenkins GitHub Webhook automates the build, test, and deployment of projects on any platform.
Task1:
Getting Started with Jenkins and GitHub for CI/CD using GitHub Webhooks🚀🚀
Step 1: Fork the Repository
Click on the "Fork" button, to create your own copy of the repository.
Step 2: Create a Freestyle Project in Jenkins
Log in to your Jenkins dashboard.
Click on "New Item" to create a new project.
Enter a name for your project and select "Freestyle project," then click "OK."
Step3: Configure Repository URL
In the project configuration, scroll down to the "Source Code Management" section.
Select "Git" and paste the URL of your forked repository.
Use the secret text option to provide credentials to Jenkins.
Kind: SSH Username with the private key
Private Key: Enter directly (cat id_rsa) and add, Now select the credentials.
Step4: Setting Up a Connection Between GitHub & Jenkins
Generate the SSH keys for integrating your Jenkins project with your Git repository. Use the ssh-keygen command to create public and private keys.
Open the Linux server and type ssh-keygen.
Now, redirect to .ssh & explore the public and private keys, and use the below commands to get a public key.
cd .ssh ls cat id.rsa_pub
Open the GitHub settings page and go to SSH and GPG keys and click on the add new SSH key button.
Step5: Scroll down to Build steps:
Go to build steps and select the execute shell option.
docker build . -t node_todo_app docker run -p 8000:8000 -d node_todo_app
let's see whether the application is running, and add port 8000 in the inbound rules in the EC2 instance security group.
Now test the application deployment, and open the application with
( IP address:8000) in the browser.
Setting up Webhook:-
Go to the Manage Jenkins option from your Jenkins dashboard.
Then choose the manage plugins option --> then install a plugin for GitHub integration with Jenkins.
Now, open the GitHub repository, go to “repository settings”, then to “webhooks” and click on the “Add webhook” button.
In the "Payload URL" field, enter the Jenkins URL followed by
/github-webhook/
. This is where Jenkins will listen for webhook events.In "Which events would you like to trigger this webhook?" select the events that should trigger the webhook. For CI/CD purposes, you might choose the "Just the Push events" option.
Click "Add webhook" to save the configuration.
Step6: Go to build Triggers
Configure the Build Triggers for the GitHub Hook trigger for GITScm pooling.
Step 7: Test the Webhook
To test the webhook, make a small code change in your forked GitHub repository (e.g., edit a file, or add a new line).
Commit and push the change to the repository.
git add . git commit -m "update the changes" git push origin master
Now the application is updated in the browser as well.
With these steps, you've successfully set up Jenkins with GitHub for CI/CD using GitHub webhooks.
Task-02
In the Execute shell run the application using Docker Compose. You will have to make a Docker Compose file for this Project.
Setting Up Jenkins with GitHub Webhook
Follow the steps mentioned in your previous Task 1 to set up Jenkins with GitHub webhook integration. This includes forking the repository, configuring the Jenkins job, and setting up the webhook in your GitHub repository settings.
Configure Docker Compose and Deployment Script
In your Jenkins job configuration, locate the "Execute shell" build step.
Modify the execute shell command to include the Docker Compose command. when "execute shell" commands will run, which stops any existing containers.
# Stop and remove the existing containers docker-compose down docker build -t nodo-todo . docker-compose up -d --name web1
Conclusion:
Jenkins GitHub webhook integration is a critical step in ensuring that your continuous integration and continuous delivery (CI/CD) pipeline is working correctly. By following the steps outlined in this article, you can set up and test a Jenkins GitHub webhook integration to automate the build and testing of your code in response to changes in your GitHub.
In the Next Article, we will explore more about Jenkins......🚀🚀🔎
Thank you for giving your precious time to read this blog📖📖. if any suggestions or improvements are required on my blog, comment below💬. like👍, and share with your friends if you find knowledgeable.
Happy Learning...........😊😊