Complete Jenkins CI/CD Project with GitHub Integration
#90 Days of DevOps Challenge - Day 24 & 25
What is GitHub webhook
Webhooks allow you to build or set up integrations, such as GitHub Apps or OAuth Apps, which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send an HTTP POST payload to the webhook's configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You're only limited by your imagination.
Webhooks can be installed on an organization, a specific repository, or a GitHub App. Once installed, the webhook will be sent each time one or more subscribed events occur.
You can create up to 20 webhooks for each event on each installation target (specific organization or specific repository).
Advantages of GitHub WebHook
Using a webhook can provide many benefits to a programmer according to Indeed. Some of the benefits are:
Increase work efficiency:- Webhooks are a simple and effective way to send information from one application to another without complex procedures or the risk of missing important data. Unlike other APIs that regularly check for data, webhooks can immediately push data from events into other applications as soon as it's available.
Easier automation:- Webhooks allow for easier automation of data transfer processes and user-defined actions for triggering events in software programs and applications. With webhooks, data can be instantly transferred, making it useful for creating repeated events based on the same triggering event.
Accurate data transfer:- Webhooks in programming provide specificity by allowing direct connections between specific parts of an application, without needing to connect multiple code elements to transfer data. This makes setting up a webhook faster and easier compared to using other APIs or callbacks. It also helps in keeping the code clean and understandable by reducing the amount of complicated code in the program.
Easy setup:-Using webhooks to connect applications often requires less setup time and effort compared to other methods. This is because webhooks use HTTP, which is a widely used internet protocol for transferring documents between web browsers and servers. Since most websites already use HTTP, adding a webhook to an application can be done easily without creating new infrastructure in your code.
Seamless integration:- If you're making a new app and want to link it with other apps using webhooks, it's usually easy because many apps support webhook integration. This is useful if you want to make an app that sends notifications, messages, or events based on existing app activity. You can even make services that let users link and integrate the apps they want and make personalized events and actions for their work or productivity.
Connection Between GitHub & Jenkins
Step 1:- First in the Ubuntu machine need to type ssh-keygen and enter 3 times
Step 2:- Then we need to go to cd.ssh and we can see the RSA public and private key
Step 3:- Now copy the public key
Step 4:- After copying the public key from the Ubuntu machine need to go to GitHub -> Github settings-> SSH and GPG keys-> New SSH key
Step 5:- After clicking on the New SSH key title: Jenkins-Project and paste the ssh public key.
Set up a pipeline for a Node Todo application
Step 1:- First we need to create a new item and need to give the project name as "node-todo-cicd" and choose Freestyle project and click on ok
Step 2:- After clicking ok , Need to give the Description and check the GitHub project and need to give GitHub project URL
Step 3:- In Source Code Management choose Git and need to provide the git hub URL and Credentials need to choose Add->Jenkins
Step 4:- After clicking on Jenkins need to fill below details and click on Add
Kind: SSH Username with private key
ID: github-jenkins
Description: This is for jenkins and github integration
Username: ubuntu
Private Key: Enter directly (cat id_rsa)
Step 5:- Now we need to select Credentials and Branch Specifier should be master
Step 6:- In the Build Step tab, select "Execute Shell" and pass the docker build command Next, pass the docker run command to start a container using the image specified.
echo " $USER Hello i have started my devps journey"
echo "i will build the code using docker"
docker build . -t node_todo_app
echo "i will perfom the test if required"
echo "i will deploy code"
docker run -itd -p 8000-:8000 node_todo_app:latest
Step 7:- Now we need to run the Build now
Step 8:- Once the build is successful, you can go to the console output and check the output of the build.
Step 9:- Now we need to verify whether we can browse the application or not by using Public IPv4 address:prot_number
Setting up Webhook:-
Step 1:- Now go to Jenkins dashboard -> Mange Jenkins -> Manage Plugins
Step 2:- Then go to Available plugins-> search GitHub integration -> click on install without restart or Download new and install after restart
Step 3:- Now go to GitHub repository ->Settings->Webhooks->Add webhook
Step 4:- After clicking on Add Webhook -> add <Jenkins url>/github-webhook/ -> Add webhook and refresh the page it should be check beside the URL
Step 5:- now go to Build Triggers and check the GitHub hook trigger for GITScm polling
Step 6:- Now whenever you changes in any code the Build Triggers automatically pull the code and start building now and when it will be successful we can see the changes
The below screenshot is before changing the code
The below screenshot is after changing the code
Step 7:- Now using the docker-compose, the application is running without any issues
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee