DEV Community

Sandipkumar Patel for AWS Community Builders

Posted on

How to Integrate Jenkins with Git?

In this project, we will configure a Jenkins build pipeline to build, compile, and package a small java project. The Jenkins pipeline is designed in such a way that whenever a change is pushed from the local repository to the Git repository, Jenkins will detect the change and will automatically start to build the project.

1. Create a sample java program and push it to your Git repository

  • Firstly, as prerequisites setup git in your local machine and create a GitHub repository.
  • Now, you can create any sample program. I have created a sample java program that prints "Jenkins integration with Git".
  • Now, push your code to the remote Git repository.

2. Start your Jenkins Server and install Git

  • Install and setup Jenkins server on Amazon Ec2 instance or your local machine.
    [Refer my previous video – Installation of Jenkins]

  • Next, connect through SSH to your previously created Jenkins instance and start your Jenkins server

sudo service jenkins start
Enter fullscreen mode Exit fullscreen mode
  • Run the following commands to install git.
sudo yum install git -y
git version
Enter fullscreen mode Exit fullscreen mode
  • Access the Jenkins server using the public DNS of the EC2 instance or public IP of the instance on port 8080.
    http://{ec2-public-dns}:8080 or http://{public IP}:8080/

  • Login to Jenkins console using your credentials.

3. Add Git plugins in Jenkins

Now, we will install the required plugins for our project.

  1. In your Jenkins console, go via the Jenkins GUI -> Manage Jenkins -> Manage Plugins.
  2. Select the tab “Available” and select all the Git plugins and then click on Install without Restart. Mine are already installed.
  3. This step is the most important one as it will allow us to access the code from our git repository. Installation of plugins might take a few minutes.

4. Create a Jenkins Job
Let’s now create our pipeline for this project. After the installation of plugins, return to the Dashboard.

  1. Via Jenkins -> New Item you’ll get to a page that will let you specify which kind of job you want to create.
  2. For now, start by creating a Freestyle Project, give a project name, and hit OK.

5. Configure Jenkins job to trigger the pipeline, after each change/commit is pushed to your Git repository

  1. To configure the pipeline, select the GitHub Project option and add your GitHub repository URL.
  2. In Source Code Management select Git and add your Git repository URL.
  3. This will allow Jenkins to access the source code from the git repository.

For more reference

Top comments (0)