Git Integration
Most modern pipelines use Git for source control. Jenkins integrates with Git and GitHub to automatically fetch code, trigger builds on push, and display commit information.
Installing Git Plugin
If not already installed, go to "Manage Jenkins" → "Plugins" → "Available" and install the "Git" plugin. The "GitHub" plugin adds extra features like webhook triggers.
Configuring Git in a Freestyle Job
In the job configuration, under "Source Code Management", select "Git". Enter the repository URL (e.g.,
https://github.com/your-username/your-repo.git). Specify the branch (e.g., main). If the repo is private, add credentials (username/password or SSH key).Using Git in a Pipeline (Declarative)
In a Declarative Pipeline, use the
git step or checkout scm.pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-username/your-repo.git'
}
}
}
}To use credentials:git branch: 'main', url: 'https://github.com/your-org/your-repo.git', credentialsId: 'github-token'Triggering Builds on Git Push (Webhook)
1. In GitHub, go to repository → Settings → Webhooks → Add webhook.
2. Payload URL:
http://your-jenkins-url/github-webhook/.3. Content type:
application/json.4. In Jenkins, configure the job/pipeline to be triggered by "GitHub hook trigger for GITScm polling".
Environment Variables from Git
After checkout, Jenkins sets several environment variables:
GIT_COMMIT, GIT_BRANCH, GIT_URL, etc. You can use them in your pipeline.Two Minute Drill
- Install Git plugin for SCM integration.
- Use
gitstep in pipeline or configure SCM in freestyle job. - Set up webhooks for automatic triggering on push.
- Access Git metadata via
GIT_COMMIT,GIT_BRANCH.
Need more clarification?
Drop us an email at career@quipoinfotech.com
