Integrating Jenkins with Source Control (Git)

JenkinsBeginner
Practice Now

Introduction

Welcome to this lab on integrating Jenkins with Git. Source Control Management (SCM) is a cornerstone of modern software development. Integrating your Continuous Integration (CI) server, like Jenkins, with an SCM system, like Git, is a fundamental step in automating your build and test processes.

In this lab, you will learn how to:

  • Verify that the Jenkins Git plugin is available.
  • Create a Freestyle project linked to a local Git repository.
  • Add a build step to execute a script from the repository.
  • Run a build manually to test the configuration.
  • Configure SCM polling to trigger builds automatically on new commits.

A Jenkins instance is already running for this lab. You can access the Jenkins web interface by opening the Firefox browser on the desktop. The browser will automatically open to http://localhost:8080, so you don't need to manually enter the URL. No login is required.

Verify the Git Plugin

Jenkins comes with Git integration built-in. The Git plugin is already installed by default, allowing Jenkins to interact with Git repositories. Let's verify that the Git plugin is available.

  1. Open the Firefox browser from the desktop interface. The browser will automatically open to http://localhost:8080, so you don't need to manually enter the URL. No login is required.
  2. On the Jenkins dashboard, click on the gear icon (settings button) in the top-right corner.
  3. On the settings page, find and click on Plugins.
  4. Select the Installed plugins tab.
  5. In the search box on the right, type Git to filter the installed plugins.
  6. You should see the Git plugin listed among the installed plugins, confirming that Git integration is available.

Jenkins installed plugins page

With the Git plugin confirmed to be available, you can now proceed to the next step to create a project that uses Git.

Create a Project and Configure Git SCM

With the Git plugin installed, you can now create a Jenkins project and configure it to pull source code from a Git repository.

  1. Return to the main Jenkins dashboard by clicking the Dashboard link in the breadcrumb navigation at the top left.
  2. Click on New Item in the left-hand sidebar.
  3. Enter git-integration-project as the item name.
  4. Select Freestyle project.
  5. Click OK.

This will take you to the project's configuration page.

  1. Scroll down to the Source Code Management section.
  2. Select the Git option. If you do not see this option, please verify that the Git plugin was installed correctly in the previous step.
  3. In the Repository URL field, enter the absolute path to the local Git repository prepared for you:
https://github.com/labex-labs/git-playground
  1. Leave the Branch Specifier as the default, */master. This tells Jenkins to use the master branch.
  2. Click the Save button at the bottom of the page.

Source Code Management section in project configuration

You have successfully created a Jenkins job linked to a Git repository. In the next step, you will define what the job should do with the code.

Add a Build Step and Run a Manual Build

Now that Jenkins knows where to get the code, you need to tell it what to do with that code. You will add a build step with shell commands and then run a build manually to test it.

  1. On the project page for git-integration-project, click Configure in the left-hand sidebar.
  2. Scroll down to the Build Steps section.
  3. Click the Add build step dropdown and select Execute shell.
  4. In the Command text area, enter the following build commands:
echo "Building the project..."
date
echo "Listing files in workspace:"
ls -la
echo "Build complete!"

When the job runs, Jenkins will first clone the Git repository into its workspace and then execute these commands from within that workspace.

  1. Click Save.
  2. You are now back on the project page. Click Build Now in the left-hand sidebar to start a build.

A new build will appear in the Build History panel. Wait for it to complete (the status icon will turn blue for success).

  1. Click on the build number (e.g., #1).
  2. Click on Console Output from the build's menu.

Build History

You should see output confirming that Jenkins cloned the repository and executed your commands successfully.

Started by user admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/git-integration-project
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/git-integration-project/.git ## timeout=10
...
[git-integration-project] $ /bin/sh -xe /tmp/jenkins123.sh
+ echo Building the project...
Building the project...
+ date
Mon Jan 1 12:01:00 UTC 2024
+ echo Listing files in workspace:
Listing files in workspace:
+ ls -la
total 24
drwxr-xr-x 3 jenkins jenkins 4096 Aug 21 10:33 .
drwxr-xr-x 3 jenkins jenkins 4096 Aug 21 10:33 ..
drwxr-xr-x 8 jenkins jenkins 4096 Aug 21 10:33 .git
-rw-r--r-- 1 jenkins jenkins   32 Aug 21 10:33 README.md
-rw-r--r-- 1 jenkins jenkins   15 Aug 21 10:33 file1.txt
-rw-r--r-- 1 jenkins jenkins   15 Aug 21 10:33 file2.txt
+ echo Build complete!
Build complete!
Finished: SUCCESS

Enable Automatic Builds with SCM Polling

Running builds manually is useful for testing, but the real power of CI is automation. In this step, you will configure Jenkins to automatically start a build whenever a change is detected in the Git repository.

  1. Navigate back to the configuration page for git-integration-project (Project Page > Configure).
  2. Scroll down to the Build Triggers section.
  3. Check the box next to Poll SCM.
  4. A Schedule text box will appear. This uses cron syntax to define how often Jenkins should check the repository for changes. To poll every minute, enter the following:
* * * * *
  1. Click Save.

Since we are using a remote GitHub repository (https://github.com/labex-labs/git-playground), we cannot directly modify it to test the polling trigger. However, you can observe how SCM polling works:

  1. Return to the Jenkins UI and go to the git-integration-project page.
  2. In the left sidebar, you'll see a new option called Git Polling Log. Click on it.
  3. This page shows the polling activity. You should see entries like:
Started on Aug 21, 2025, 10:36:00 AM
Using strategy: Default
[poll] Last Built Revision: Revision d22f46ba8c2d4e07d773c5126e9c803933eb5898 (refs/remotes/origin/master)
The recommended git tool is: NONE
No credentials specified
 > git --version ## timeout=10
 > git --version ## 'git version 2.30.2'
 > git ls-remote -h -- https://github.com/labex-labs/git-playground ## timeout=10
Found 3 remote heads on https://github.com/labex-labs/git-playground
[poll] Latest remote head revision on refs/heads/master is: d22f46ba8c2d4e07d773c5126e9c803933eb5898 - already built by 1
Done. Took 0.36 sec
No changes

This demonstrates that Jenkins is successfully polling the remote repository for changes every minute. In a real-world scenario, when new commits are pushed to the repository, Jenkins would automatically trigger a new build.

  1. You can also manually trigger another build by clicking Build Now to see how the polling system integrates with manual builds.

Summary

In this lab, you gained hands-on experience with one of the most critical integrations in a CI pipeline: connecting Jenkins to a Git source control repository.

You have successfully:

  • Verified that the essential Git plugin is available to enable Jenkins' Git integration capabilities.
  • Created a Freestyle project and configured it to check out code from a Git repository.
  • Defined a build step to execute a shell script from the checked-out code.
  • Verified the setup by running a manual build.
  • Set up an automated build trigger using Poll SCM, which allows Jenkins to detect and build new commits automatically.

These skills are fundamental for automating your development workflow and are the first step towards building a robust and efficient CI/CD pipeline with Jenkins.