Integrating Git with Jenkins

Interview Line

"We integrate Git with Jenkins by installing the Git plugin, configuring the repository and credentials, and setting up a trigger like a webhook or Poll SCM. This allows Jenkins to automatically pull the code."

Real Project Flow

To integrate Git with Jenkins:

  • Install the Git Plugin
  • Configure the Git repository
  • Configure Git credentials
  • Configure GitHub Webhooks

This allows Jenkins to automatically pull the latest source code whenever changes occur.

Advertisement

Configuring the Git Plugin (Step by Step)

Step 1: Install the Git Plugin

Navigate to:

  • Manage Jenkins
  • Install the Git Plugin

Step 2: Configure Git in the Jenkins Job

Open the Jenkins job.

Navigate to:

Configure → Source Code Management

Configure:

  • Repository URL
  • Branch (for example, main or develop)
  • Git Credentials (Username/Token or SSH)

Step 3: Configure the Build Trigger

Choose one of the following:

  • GitHub Webhook (Preferred)
  • Poll SCM

GitHub Webhooks provide real-time triggering.


Step 4: Configure Build Steps or Pipeline

Define the activities that execute after source code checkout.

Examples include:

  • Maven Build
  • Automation Testing

What Is a Webhook?

A Webhook allows Git to notify Jenkins automatically whenever a specific event occurs.

Examples include:

  • Code Push
  • Pull Request

When an event occurs, Git immediately sends an HTTP notification to Jenkins.

Jenkins instantly starts the configured job.

This enables real-time CI/CD.

Analogy

Think of a webhook as an instant message sent from Git to Jenkins.

Real-Time Project Flow

  1. Developer pushes code to GitHub.
  2. GitHub sends a webhook notification to Jenkins.
  3. Jenkins immediately triggers the job.
  4. Build, Test, and Deploy stages execute.

Configuring GitHub Webhooks (Step by Step)

Step 1: Enable the Webhook Trigger in Jenkins

Inside the Jenkins job:

Build Triggers

Enable:

GitHub hook trigger for GITScm polling


Step 2: Configure the Webhook in GitHub

Navigate to:

GitHub Repository → Settings → Webhooks

Configure:

Payload URL

 
http://<jenkins-url>/github-webhook/
 

Content Type

 
application/json
 

Configure the webhook to trigger on:

  • Push Event

After configuration, every Git push automatically notifies Jenkins and starts the build.


Webhook vs Poll SCM

Webhook

An event-based trigger.

Characteristics:

  • Git sends an HTTP notification immediately after a push or commit.
  • Real-time triggering.
  • Efficient.
  • No unnecessary repository polling.

Poll SCM

A polling-based trigger.

Characteristics:

  • Jenkins periodically checks Git.
  • Builds execute only when changes are detected.
  • Introduces execution delay.
  • Consumes resources even when no changes exist.

Best used when webhooks are unavailable.

Key Rule

Webhook is better than Poll SCM.

(Complete Poll SCM details are covered in the Jenkins Scheduling cluster.)


Git Credentials, PRs, Pipelines & Multiple Repositories

Handle Git Credentials Securely

Store Git credentials inside Jenkins Credentials.

Supported credential types include:

  • Username & Token
  • SSH Keys

Reference the credentials inside Jenkins jobs or pipelines.

Do not hardcode credentials.

(Credentials management is covered in the Security & Administration cluster.)


Trigger on Pull Request Creation

Configure Jenkins to trigger builds when:

  • Pull Requests are created

using GitHub webhook events.


Integrate Git in a Pipeline

The pipeline Checkout Stage retrieves the source code using the Jenkinsfile.

Source code checkout becomes the first pipeline stage.


Multiple Repositories

Jenkins can retrieve source code from multiple repositories within a single job or pipeline whenever multiple repositories are required.


What Happens If Git Is Down?

If Git becomes unavailable:

  • Jenkins cannot retrieve source code.
  • Checkout fails.
  • Build execution fails or waits until Git becomes available.

FAQs

How Do You Integrate Git with Jenkins?

To integrate Git with Jenkins:

  • Install the Git Plugin
  • Configure Repository URL
  • Configure Branch
  • Configure Credentials
  • Configure a Build Trigger (Webhook or Poll SCM)

Jenkins then automatically retrieves source code.


How Do You Configure a GitHub Webhook?

Inside Jenkins:

Enable:

GitHub hook trigger for GITScm polling

Inside GitHub:

Settings → Webhooks

Configure:

Payload URL

 
http://<jenkins-url>/github-webhook/
 

Content Type

 
application/json
 

Trigger on:

  • Push Events

What Is a Webhook?

A webhook is an event-based HTTP notification sent by Git to Jenkins whenever a Push or Pull Request occurs.

It enables real-time CI/CD execution.


Which Is Better: Webhook or Poll SCM?

Webhook

  • Instant execution
  • Efficient
  • Real-time

Poll SCM

  • Periodic repository checks
  • Delayed execution
  • Higher resource usage

Webhook is preferred whenever available.


How Do You Handle Git Credentials Securely?

Store credentials inside Jenkins Credentials using:

  • Username & Token
  • SSH Keys

Reference them from Jenkins jobs or pipelines instead of hardcoding them.


Can Jenkins Pull Code from Multiple Repositories?

Yes.

A Jenkins job or pipeline can retrieve source code from multiple repositories when required for the build.