Skip to main content
Jenkins Pipeline
CHAPTER 07

Git and GitHub Integration

Updated: May 15, 2026
20 min read

# CHAPTER 7

Git and GitHub Integration

1. Introduction

A CI/CD pipeline is only truly "Continuous" if it runs automatically. The magic of DevOps happens when a developer types git push and, without touching a single button, Jenkins detects the new code, downloads it, and starts the pipeline. This automation is achieved by tightly integrating Jenkins with Source Control Management (SCM) platforms like GitHub, GitLab, or Bitbucket. In this chapter, we will learn how to configure Jenkins to pull code from Git repositories and how to set up GitHub Webhooks to trigger builds instantly.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the role of Git in the CI/CD lifecycle.
  • Configure a Jenkins Pipeline job to pull a Jenkinsfile from an SCM.
  • Define what a Webhook is and how it functions.
  • Configure a GitHub Webhook to trigger Jenkins automatically.
  • Understand the concept of Branch Builds (e.g., building main vs. dev).

3. Beginner-Friendly Explanation

Imagine a newspaper delivery system.
  • The Old Way (Polling): Jenkins is a paperboy. Every 5 minutes, Jenkins rides a bicycle to the printing press (GitHub) and asks, "Do you have a new paper?" Most of the time, the answer is no. This wastes energy and delays the delivery.
  • The DevOps Way (Webhooks): Jenkins stays home and drinks coffee. When the printing press (GitHub) finishes printing a new paper, the printing press *calls Jenkins on the phone* (The Webhook) and says, "The paper is ready, come get it." Jenkins instantly jumps up and goes to work.

4. Fetching the Jenkinsfile from SCM

Until now, we typed our pipeline code directly into the Jenkins UI. This is bad practice. The Jenkinsfile should live in your GitHub repository alongside your code. To configure this in Jenkins:
  1. 1. Create a Pipeline Job.
  1. 2. Under the "Pipeline" section, change the "Definition" drop-down from *Pipeline script* to Pipeline script from SCM.
  1. 3. Select Git.
  1. 4. Enter your GitHub Repository URL (e.g., https://github.com/yourname/my-app.git).
  1. 5. Specify the branch to build (e.g., */main).
  1. 6. Click Save. Now, when the job runs, Jenkins will download your GitHub repo, find the Jenkinsfile, and execute the instructions inside it.

5. Triggering Builds: Polling vs. Webhooks

How does Jenkins know *when* to run the job?
  • Poll SCM (Not Recommended): You tell Jenkins to check GitHub every 5 minutes (Cron syntax: H/5 * * * *). This creates unnecessary network traffic and means your build could be delayed by up to 5 minutes.
  • GitHub Webhooks (Best Practice): You tell GitHub: "Whenever a developer pushes code, send an HTTP POST request (a digital phone call) to my Jenkins server URL." Jenkins receives the ping and starts the build immediately.

6. Mini Project: Auto-Build App via Webhooks

Let's configure a true CI/CD trigger. *(Note: Your Jenkins server must be accessible from the internet for GitHub to reach it. If running locally, you can use a tool like ngrok to expose your local Jenkins to the web).*

Step-by-Step Walkthrough:

  1. 1. In Jenkins: Open your Pipeline Job -> Configure.
  1. 2. Under "Build Triggers", check the box for GitHub hook trigger for GITScm polling. Save.
  1. 3. In GitHub: Go to your repository -> Settings -> Webhooks.
  1. 4. Click Add webhook.
  1. 5. In the Payload URL, enter your Jenkins URL followed by /github-webhook/.
*(Example: http://my-jenkins-server.com:8080/github-webhook/)*
  1. 6. Set the Content type to application/json.
  1. 7. Select "Just the push event."
  1. 8. Click Add webhook.
  1. 9. The Test: On your computer, modify a file, commit it, and run git push origin main.
  1. 10. Watch your Jenkins dashboard. The job will start automatically within seconds!

7. Real-World Scenarios

A development team used "SCM Polling" set to run every 1 minute. They had 200 microservice repositories. This meant Jenkins was asking GitHub "Got new code?" 200 times a minute, 24/7. GitHub's automated security systems detected this as a Denial of Service (DoS) attack and temporarily banned the company's IP address, completely freezing all deployments. By switching to Webhooks, Jenkins made zero outgoing requests, waiting silently for GitHub to push notifications, completely resolving the rate-limiting ban.

8. Best Practices

  • Multibranch Pipelines: Instead of creating a separate Jenkins Job for your main branch, dev branch, and feature branches, use the Multibranch Pipeline project type. You point it at your GitHub repo once, and Jenkins automatically discovers every branch that contains a Jenkinsfile and creates a separate pipeline for it automatically.

9. Security Recommendations

  • Webhook Secrets: Anyone on the internet who knows your Jenkins URL can send a fake Webhook payload to trigger a build. To prevent this, configure a "Secret token" in the GitHub Webhook settings and configure the Jenkins GitHub plugin to validate that token, ensuring only genuine GitHub requests are accepted.

10. Troubleshooting Tips

  • The Missing Jenkinsfile: If Jenkins pulls your code but fails immediately with ERROR: Expected to find Jenkinsfile, ensure your file is named exactly Jenkinsfile (capital J, no file extension like .txt) and is located in the root (top-level) directory of your Git repository.

11. Exercises

  1. 1. Explain the architectural difference between SCM Polling and Webhooks. Why are Webhooks the industry standard?
  1. 2. What is the operational benefit of checking your Jenkinsfile into Git rather than typing it into the Jenkins UI?

12. FAQs

Q: Do I need a GitHub plugin in Jenkins? A: Yes. If you chose "Install suggested plugins" during setup (Chapter 2), the "Git" and "GitHub" plugins were installed automatically. These plugins provide the SCM dropdown and the Webhook listener endpoint.

13. Interview Questions

  • Q: Describe the communication flow between GitHub and Jenkins when utilizing Webhooks for CI/CD automation. What specific endpoint does the Jenkins GitHub plugin expose to listen for these events?
  • Q: You are migrating a team from single-branch deployments to a feature-branch workflow. Explain how you would configure Jenkins to automatically build and test code pushed to a new branch without requiring manual job creation.

14. Summary

In Chapter 7, we unlocked the true power of automation. We connected Jenkins to its primary data source: Git. We moved our Jenkinsfile out of the fragile web UI and into resilient, version-controlled source code. Most importantly, we replaced inefficient polling with event-driven Webhooks, creating a system where a single git push on a developer's laptop instantly and automatically triggers a full deployment pipeline on the server.

15. Next Chapter Recommendation

We have the code, and we have the automated trigger. Now, what exactly should that code do? Proceed to Chapter 8: CI/CD Workflows with Jenkins.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·