Continuous Integration with Git and Jenkins
Introduction
Continuous Integration (CI) is a crucial practice in modern software development, allowing teams to merge code frequently and detect errors early through automated builds and tests. By integrating changes multiple times a day into a shared Git repository, developers ensure better collaboration and improved software quality.
Jenkins, a widely-used open-source automation server, simplifies the CI process by automating code integration, build execution, and testing. In this guide, we will walk you through setting up a CI pipeline using Git and Jenkins, covering installation, configuration, repository setup, job creation, and webhook integration.
Table of Contents
- What is Continuous Integration?
- Why Use Jenkins for CI?
- Prerequisites
- Setting Up Jenkins
- Configuring Jenkins
- Setting Up a Git Repository
- Creating a Jenkins Job
- Setting Up a GitHub Webhook (Optional)
- Conclusion
🔗 Official Documentation:
1. What is Continuous Integration?
Continuous Integration (CI) is a development practice where developers frequently merge their code into a shared Git repository. Each integration triggers an automated process that includes building the application, running tests, and validating the changes.
Benefits of CI:
✔ Early bug detection through automated testing
✔ Faster feedback loops for developers
✔ Reduces integration conflicts
✔ Ensures code quality and consistency
By combining Git for version control and Jenkins for automation, we can build a robust CI pipeline that continuously integrates and tests code changes.
2. Why Use Jenkins for CI?
Jenkins is a flexible and extensible CI tool that helps developers automate the software development process. It integrates seamlessly with GitHub, Bitbucket, and GitLab, making it an ideal choice for managing Continuous Integration pipelines.
Key Features of Jenkins:
- Automated Builds: Run builds automatically on code changes
- Extensive Plugin Support: Supports a wide range of CI/CD tools
- Scalability: Can be configured for distributed builds
- Webhooks Integration: Trigger builds directly from GitHub, GitLab, or Bitbucket
Now, let’s move on to setting up Jenkins for CI.
3. Continuous Integration with Git and Jenkins – Prerequisites
Before setting up Jenkins, ensure that you have:
✅ Git Installed – Download Git
✅ Jenkins Installed – Jenkins Installation Guide
✅ A GitHub Repository – Create a GitHub Repo
✅ Basic Linux or Windows Command Line Knowledge
Once you have these ready, let’s proceed to install Jenkins.
4. Setting Up Jenkins
Step 1: Download and Install Jenkins
Visit the Jenkins official website and download the installer for your operating system. Follow the installation steps according to your OS:
- Windows: Run the
.msiinstaller - Ubuntu/Linux: Run the following commands:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins
Step 2: Start Jenkins
Start Jenkins using:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 3: Access Jenkins
- Open a web browser and navigate to
http://localhost:8080 - Retrieve the admin password from the file:
cat /var/lib/jenkins/secrets/initialAdminPassword - Enter the password and click Unlock Jenkins
5. Configuring Jenkins
Step 1: Install Suggested Plugins
After unlocking Jenkins, you will be prompted to install plugins. Select “Install suggested plugins” to install essential tools like:
✔ Git Plugin
✔ Pipeline Plugin
✔ Build Tools Plugin
Step 2: Create an Admin User
Set up a username and password to manage Jenkins securely.
Step 3: Configure Git in Jenkins
- Navigate to Manage Jenkins → Global Tool Configuration
- Under Git, specify the path to the Git executable (
gitfor Linux/macOS, orC:\Program Files\Git\bin\git.exefor Windows)
6. Setting Up a Git Repository
Step 1: Create a Repository
- Navigate to GitHub and create a new repository.
- Initialize a README.md file and select “Add .gitignore” for your programming language.
Step 2: Clone the Repository and Add Code
git clone https://github.com/your-username/your-repo.git
cd your-repo
echo "# Continuous Integration Demo" > README.md
git add .
git commit -m "Initial commit"
git push origin main
Now, let’s create a Jenkins job to automate the build process.
7. Creating a Jenkins Job
Step 1: Create a New Job
- Open Jenkins and click New Item
- Enter a job name and select Freestyle Project
- Click OK
Step 2: Configure Source Code Management
- Under Source Code Management, select Git
- Enter your GitHub repository URL
- Add credentials if required
Step 3: Set Build Triggers
- Under Build Triggers, select Poll SCM
- Add the schedule:
H/5 * * * *This will check for new commits every 5 minutes.
Step 4: Add Build Steps
- Under Build Steps, select Execute Shell
- Add the following script:
echo "Building the project" ./gradlew build
Step 5: Save and Run the Job
- Click Save
- Click Build Now to trigger a manual build
8. Setting Up a GitHub Webhook (Optional)
A webhook allows Jenkins to trigger builds whenever new code is pushed to GitHub.
Step 1: Create a Webhook in GitHub
- Go to your GitHub repository → Settings → Webhooks
- Click Add webhook
- Set Payload URL to
http://your-jenkins-url/github-webhook/ - Select application/json as Content Type
- Choose “Just the push event”
- Click Add Webhook
9. Conclusion
By following these steps, you have successfully set up a Continuous Integration (CI) pipeline using Git and Jenkins. Now, whenever you push changes to GitHub, Jenkins will automatically build and test your code.
Next Steps:
🚀 Extend your CI pipeline by adding:
✔ Automated Testing (JUnit, Selenium, etc.)
✔ Code Quality Analysis (SonarQube, Checkstyle, etc.)
✔ Continuous Deployment (CD) for automatic deployments
🔗 Further Reading:
