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.
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,
mainordevelop) - 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
- Developer pushes code to GitHub.
- GitHub sends a webhook notification to Jenkins.
- Jenkins immediately triggers the job.
- Build, Test, and Deploy stages execute.
Configuring GitHub Webhooks (Step by Step)
Step 1: Enable the Webhook Trigger in Jenkins
Inside the Jenkins job:
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.
Git Checkout in a Jenkinsfile
In a Pipeline script from SCM or multibranch job, checkout scm checks out exactly the repository and commit that triggered the build. To check out another repository — for example, a shared test-data repo — use the git step with stored credentials:
stage('Checkout') {
steps {
checkout scm // the repo containing this Jenkinsfile
dir('test-data') {
git url: 'https://github.com/your-org/test-data.git',
branch: 'main',
credentialsId: 'github-token' // Username + personal access token
}
}
}
HTTPS token or SSH key?
| HTTPS + personal access token | SSH key | |
|---|---|---|
| Jenkins credential type | Username with password (password = token) | SSH Username with private key |
| Repository URL | https://github.com/org/repo.git | git@github.com:org/repo.git |
| Works through proxies | Usually yes | Needs port 22 (or 443 via ssh.github.com) |
| Notes | GitHub no longer accepts account passwords for Git — a token is required | Agents must trust GitHub's host key |
Use a dedicated bot account or a read-only deploy key rather than a person's own credentials, so builds don't break when someone leaves.
Troubleshooting: Is It Git, Credentials or the Network?
When checkout fails, run git ls-remote from the agent — it contacts the repository without cloning anything. We tried it against a public repository and a private one without credentials:
$ git ls-remote https://github.com/SeleniumHQ/selenium.git HEAD
<commit-id> HEAD
$ GIT_TERMINAL_PROMPT=0 git ls-remote https://github.com/your-org/private-tests.git
fatal: could not read Username for 'https://github.com': terminal prompts disabled
- A commit ID back — the network and URL are fine; if Jenkins still fails, the problem is its credential configuration.
- "could not read Username" — the repository needs authentication (or doesn't exist); Jenkins must be given a credential.
- A timeout or "could not resolve host" — a network, proxy or firewall problem between the agent and Git.
For webhooks, check GitHub's Settings → Webhooks → Recent Deliveries: a 200 means Jenkins received it, anything else shows why not. Agents that run browser tests after checkout are often Docker containers — see the Docker + Selenium Grid Guide. Scheduling alternatives: CRON, Poll SCM & Webhooks.
From Real Projects
In my automation roles I maintained test scripts that several people reviewed and extended — POM classes, the business library and data files all changing at once. That's exactly the situation version control is built for: every change reviewed, traceable and easy to undo. On Canolog, the many forms and screens across sales, inventory, finance and service are where keeping page details in POM classes and reusable steps in a business library paid off. git ls-remote tells you quickly whether a checkout problem is credentials or network.
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.