CI/CD

Jenkins Git Integration

Set up Git integration with Jenkins including webhook triggers, Multibranch Pipeline, credential management, and security best practices.

Who This Is For
  • Developers using Git in CI/CD pipelines
  • Readers who want to understand Git operation boundaries in automation
Prerequisites
  • Basic understanding of branch, commit, and push
  • Basic CI/CD concepts
Common Risks
  • 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:

  1. Go to Manage Jenkins → Plugins → Installed plugins
  2. Search for "Git" to confirm
  3. 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:

SettingRecommended
Trigger eventPush / PR merge
Content typeapplication/json
SecretRecommended

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 deploy
  • develop: Auto-build + test environment deploy
  • Feature branches: Build and unit tests only

Troubleshooting

Webhook Not Firing

  1. Check Jenkins logs for 403 errors
  2. Verify webhook URL is reachable
  3. Confirm Secret token matches

Credential Failures

# Test SSH connection from Jenkins node
ssh -T git@github.com

Continue Learning

  1. ci-cd/ci-security-basics — Git security in CI/CD pipelines
  2. ci-cd/github-actions-basics — GitHub Actions integration
  3. ci-cd/gitlab-ci-basics — GitLab CI integration