Complete Jenkins CI/CD Project

Complete Jenkins CI/CD Project

ยท

8 min read

What is GitHub Webhook

As of my last update in September 2021, a GitHub webhook is a mechanism provided by GitHub to allow external systems to be notified when certain events occur within a GitHub repository. Webhooks are commonly used for automating tasks and integrating with other services, such as continuous integration (CI) and continuous deployment (CD) pipelines, project management systems, chat applications, and more.

Here's how GitHub webhooks work:

  1. Setting up a Webhook: To create a webhook, you need to go to your GitHub repository's settings and find the "Webhooks" or "Hooks" section. From there, you can add a new webhook and specify the URL of the external service that should be notified.

  2. Events: You can choose which events should trigger the webhook. GitHub supports a wide range of events, including:

    • Push events: Triggered when code is pushed to the repository.

    • Pull request events: Triggered when pull requests are created, updated, or merged.

    • Issue events: Triggered when issues are created, updated, or closed.

    • Release events: Triggered when a new release is published.

    • And more...

  3. Payload: When the selected event occurs (e.g., someone pushes code to the repository), GitHub sends a POST request to the specified URL (webhook endpoint). This request contains a JSON payload with relevant information about the event.

  4. Processing the Payload: The external service that owns the webhook endpoint can receive the payload, parse the information, and take appropriate actions based on the event. For instance, a CI/CD system might use the push event payload to trigger an automated build and deployment process.

By using GitHub webhooks, you can streamline your development workflow, automate repetitive tasks, and enhance collaboration between GitHub and other tools in your development ecosystem. Keep in mind that the specific features and functionalities of GitHub may change over time, so it's a good idea to consult GitHub's official documentation for the latest information on webhooks.

Advantages of GitHub WebHook

  1. Real-time Automation: Webhooks enable real-time notifications of events in your GitHub repository. This means that when someone pushes code, opens a pull request, creates an issue, or performs any other specified action, your webhook will immediately trigger an action in another system or service. This allows for seamless automation and integration with various tools and processes.

  2. Automated Continuous Integration and Deployment (CI/CD): By using webhooks with your CI/CD pipeline, you can automate the process of building, testing, and deploying code changes whenever new code is pushed to the repository. This results in faster and more reliable releases with reduced manual intervention.

  3. Streamlined Code Review and Collaboration: Webhooks can integrate with project management and collaboration tools, enabling better communication and collaboration among team members. For example, a webhook can automatically update a project board, notify team members in a chat application, or trigger code review processes when pull requests are opened.

  4. Error Monitoring and Reporting: With webhooks, you can connect your repository to error monitoring tools or logging services. When an error occurs in your application, the webhook can immediately notify the appropriate team members or create an issue in GitHub, ensuring that issues are addressed promptly.

  5. Automated Testing and Quality Assurance: Webhooks can be used to automatically trigger tests and quality assurance processes when new code is pushed. This helps catch bugs and issues early in the development process, leading to higher code quality.

  6. Custom Workflow Integration: GitHub webhooks are highly flexible and customizable. You can tailor them to fit your specific development workflow and integrate with your preferred set of tools and services. This allows you to create a cohesive ecosystem of tools that work seamlessly together.

  7. Event-Driven Architecture: Webhooks promote an event-driven architecture, where different systems respond to specific events rather than constantly polling for changes. This reduces unnecessary API calls and improves the efficiency of the integration.

  8. Extensive Event Support: GitHub provides a wide range of events for which you can set up webhooks, allowing you to track various activities and take appropriate actions accordingly. This versatility enables you to build sophisticated automation scenarios.

Overall, GitHub webhooks play a crucial role in streamlining development processes, improving collaboration, and automating repetitive tasks. They enhance the efficiency and effectiveness of your development team, leading to faster, more reliable software delivery.

How to create Webhooks?

  1. Access Repository Settings: First, navigate to the GitHub repository where you want to set up the webhook. You need to have administrative access to the repository to create a webhook.

  2. Go to Webhooks Section: Once you are in the repository, click on the "Settings" tab located in the right-hand corner of the repository's navigation menu. Then, on the left sidebar, click on "Webhooks" or "Hooks" (depending on GitHub's interface updates).

  3. Create a New Webhook: On the "Webhooks" page, you'll see a button to "Add webhook" or "Add a webhook." Click on it to start creating your new webhook.

  4. Configure the Webhook:

    • Payload URL: Provide the URL of the external service that will receive the webhook payload. This service should be able to handle the incoming POST requests from GitHub.

    • Content type: Choose the content type for the webhook payload. It's usually JSON.

    • Which events would you like to trigger this webhook?: Select the specific GitHub events that will trigger the webhook. For example, you can choose "Push" to trigger the webhook whenever code is pushed, or "Pull Request" to trigger it when a pull request is created or updated.

    • Secret (Optional): You can provide a secret key to further secure the communication between GitHub and your webhook endpoint. The secret is used to create a hash signature for each payload, which the receiving service can use to verify the authenticity of the request.

    • SSL Verification (Optional): GitHub recommends enabling SSL verification to ensure secure communication with your webhook endpoint. If your endpoint supports HTTPS (which is highly recommended), you should enable this option.

    • Active: Ensure the webhook is marked as "Active" so that it can trigger events.

  5. Add the Webhook: After configuring the webhook settings, click on the "Add webhook" or "Create webhook" button to save your webhook configuration.

  6. Test the Webhook (Optional): Some services allow you to test the webhook to ensure that it is set up correctly. If your external service provides this option, you can use it to verify that the webhook is working as expected.

And that's it! Your webhook is now set up, and GitHub will send POST requests to the specified URL whenever the selected events occur in the repository.

Jenkins CI/CD Project

Project Explanation: I am going to continuously integrate and deploy a Node JS application on Docker using Jenkins and GitHub Integration.

The repository link used for this project is : github.com/ahirebn2/node-todo-cicd

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

Task 1: Run the project using dockerfile

  1. Log in to the AWS Console and connect to the Jenkins Instance.

  2. Install Docker, Docker-Compose, Java, and Jenkins in your system.

    In my previous blogs, I have explained how to install these.

  3. Once Jenkins is installed, Open port 8080 to your system in the AWS console. Log in to the Jenkins dashboard and create a freestyle Pipeline named "todo-node-app".

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-app" 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:- Now we need to select Credentials and Branch Specifier should be master

Step 7:- 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.

Step 8:- Now we need to run the Build now

Step 9:- Once the build is successful, you can go to the console output and check the output of the build.

Step 10:- 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 > Then go to Available plugins-> search GitHub integration -> click on install without restart

Step 2:- Now go to GitHub repository ->Settings->Webhooks->Add webhook

Step 3:- After clicking on Add Webhook -> add <Jenkins url>/github-webhook/ -> Add webhook and refresh the page it should be check beside the URL

Step 4:- now go to Jenkins > todo-node-app > Configure > Build triggers - select GitHub hook trigger for SCM pooling > Save

Step 5:- 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:

ย