CI/CD
Jenkins Git Integration
Set up Git integration with Jenkins including webhook triggers, Multibranch Pipeline, credential management, and security best practices.
- Developers using Git in CI/CD pipelines
- Readers who want to understand Git operation boundaries in automation
- Basic understanding of branch, commit, and push
- Basic CI/CD concepts
- Misusing GITHUB_TOKEN causing security issues
- Not understanding the trade-off between shallow and partial clone
Overview
Jenkins is one of the most popular open-source CI/CD tools. With Git integration, you can automatically trigger builds, tests, and deployments on every code push.
Core Integration Methods
1. Git Plugin Setup
Jenkins includes a built-in Git plugin, but verify it's installed:
- Go to Manage Jenkins → Plugins → Installed plugins
- Search for "Git" to confirm
- If missing, install from Available plugins
2. Credential Configuration
// In Jenkins credential manager, add:
// - Username with password (HTTPS)
// - SSH key (SSH)
Use a unique credential ID per repository for better audit and rotation.
3. Webhook Triggers
Add the Jenkins webhook URL in your GitHub/GitLab repository settings:
http://your-jenkins:8080/github-webhook/
http://your-jenkins:8080/gitlab-webhook/
Webhook configuration:
| Setting | Recommended |
|---|---|
| Trigger event | Push / PR merge |
| Content type | application/json |
| Secret | Recommended |
4. Multibranch Pipeline
Multibranch Pipeline scans your repository and creates pipelines for every branch containing a Jenkinsfile.
// Jenkinsfile
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'make build'
}
}
stage('Test') {
steps {
sh 'make test'
}
}
}
}
Best Practices
Shallow Clone
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [
[$class: 'CloneOption', depth: 1, shallow: true]
]
])
Credential Security
// Never hardcode credentials in Jenkinsfile
withCredentials([gitUsernamePassword(credentialsId: 'github-creds')]) {
sh 'git push origin main'
}
Branch Strategy
main: Auto-build + production deploydevelop: Auto-build + test environment deploy- Feature branches: Build and unit tests only
Troubleshooting
Webhook Not Firing
- Check Jenkins logs for 403 errors
- Verify webhook URL is reachable
- Confirm Secret token matches
Credential Failures
# Test SSH connection from Jenkins node
ssh -T git@github.com
Continue Learning
ci-cd/ci-security-basics— Git security in CI/CD pipelinesci-cd/github-actions-basics— GitHub Actions integrationci-cd/gitlab-ci-basics— GitLab CI integration