Jenkins - Git Setup
Jenkins - Git Integration
Section titled “Jenkins - Git Integration”Integrating Jenkins with Git is essential for most modern CI/CD workflows, allowing Jenkins to automatically check out source code when changes are pushed or on a schedule.
Step 1: Install Git Plugin
Section titled “Step 1: Install Git Plugin”Jenkins needs the ‘Git plugin’ to interact with Git repositories. It’s usually installed as part of the ‘suggested plugins’ during initial setup. If not, or to ensure you have it:
- Navigate to Manage Jenkins → Plugins.
- Click on the ‘Available Plugins’ tab.
- In the filter box, type ‘Git plugin’.
- Select the checkbox next to ‘Git plugin’.
- Click ‘Install without restart’ or ‘Download now and install after restart’. Jenkins will download and install the plugin.
- Restart Jenkins if necessary (though often not required for plugin installation anymore).
The Git plugin often bundles the ‘Git client plugin’, which provides the actual Git executable if not found on the system path.
Step 2: Configure Git Tool (Optional but Recommended)
Section titled “Step 2: Configure Git Tool (Optional but Recommended)”While the Git client plugin provides a Git executable, it’s good practice to explicitly configure Git, especially if you need a specific version or if Git isn’t in the default system PATH for the Jenkins user.
- Navigate to Manage Jenkins → Global Tool Configuration.
- Scroll down to the ‘Git’ section.
- Click ‘Add Git’.
- Provide a ‘Name’ (e.g.,
DefaultorGit-2.x). - Option 1: System Git: If Git is installed on the controller/agents and in the PATH, you can leave ‘Path to Git executable’ empty or explicitly provide the path (e.g.,
/usr/bin/git). - Option 2: Automatic Installation: Check ‘Install automatically’. Choose an installer (e.g., ‘Install from git-scm.com’) and select a version. Jenkins will download and manage this Git installation.
- Click ‘Save’.
Step 3: Configure a Job/Pipeline to Use Git
Section titled “Step 3: Configure a Job/Pipeline to Use Git”Now you can configure a Jenkins item (Pipeline or Freestyle job) to check out code from a Git repository.
Using Git in a Declarative Pipeline (Jenkinsfile)
Section titled “Using Git in a Declarative Pipeline (Jenkinsfile)”The standard way is using the git step or the SCM definition within the pipeline block.
pipeline { agent any stages { stage('Checkout') { steps { echo 'Checking out code from Git...' // Simple checkout from a public repository // git url: 'https://github.com/your-username/your-repo.git', branch: 'main'
// Checkout from a private repository using credentials git url: 'https://github.com/your-username/your-private-repo.git', branch: 'develop', credentialsId: 'github-ssh-key-credential' // ID from Jenkins Credentials } } stage('Build') { steps { // Your build steps here, e.g.: sh 'mvn clean package' } } }}Using Git in a Freestyle Project
Section titled “Using Git in a Freestyle Project”- Create a new ‘Freestyle project’ or configure an existing one.
- In the project configuration page, scroll down to the ‘Source Code Management’ section.
- Select ‘Git’.
- Repository URL: Enter the URL of your Git repository (HTTPS or SSH).
- Credentials: If the repository is private, click ‘Add’ → ‘Jenkins’ to select or add the necessary credentials (e.g., Username/Password, SSH Username with private key, Secret text for API tokens). Choose the appropriate Credential ID from the dropdown.
- Branches to build: Specify which branch(es) to build (e.g.,
*/main,*/develop,*/feature/*).*/mainis common. - Additional Behaviours: Configure advanced options like shallow clone (faster checkout, less history), checkout strategy, sparse checkout, etc., if needed.
- Configure build triggers (e.g., ‘Poll SCM’, ‘GitHub hook trigger for GITScm polling’) and build steps as required.
- Click ‘Save’.
Credentials Management
Section titled “Credentials Management”For private repositories, storing credentials securely is vital.
- Navigate to Manage Jenkins → Credentials → System → Global credentials (unrestricted) or create a specific domain.
- Click ‘Add Credentials’.
- Choose the ‘Kind’ of credential:
-
- Username with password: For HTTPS authentication.
-
- SSH Username with private key: For SSH authentication (recommended for security). You can paste the private key directly or have Jenkins load it from a file on the controller.
-
- Secret text: For personal access tokens (e.g., GitHub PATs).
- Fill in the details (Username, Password/Private Key/Secret, ID, Description). The ‘ID’ is what you’ll use in job configurations.
- Click ‘Create’.
With Git integration configured, Jenkins can automatically fetch your source code, forming the foundation of your CI/CD process.